Odyssey

Growing your GraphQL API with Java & DGS
deprecated

Project setupAdding the Artist typeThe Artist data sourceUpdating modelsThe track datafetcherWrapping up
6. Wrapping up
3m

It's the moment of truth!

🎯 Your goal for this step:

Run a query that retrieves a playlist, its tracks, and their artist data all at once

A mockup of an artist view, showing name, followers, genres and a link URI

Use the following playlist ID when completing these tasks.

A playlist ID
6LB6g7S5nc1uVVfj00Kh6Z
Sandbox tasks

Solution: Wrapping up

query GetPlaylist($playlistId: ID!) {
playlist(id: $playlistId) {
id
name
tracks {
id
name
durationMs
artist {
name
followers
}
}
}
}

The error:

@DgsData in /TrackDataFetcher on field artist references object type `Track`
it has no field named `artist`

How to fix it:

Return to your schema file, and make sure that your Track type contains an artist field of type Artist.

The error:

JSON parse error: Cannot invoke "com.fasterxml.jackson.databind.JsonNode.asText()"
because the return value of "com.fasterxml.jackson.databind.JsonNode.get(String)" is null

How to fix it:

The way your MappedTrack model is accessing artist information might be incorrect. Jump back into models/MappedTrack and make sure that you are calling this.setArtistId with the first artist in the artists' arary the track receives.

this.setArtistId(trackObject.get("artists").get(0).get("id").asText());

The error:

404 Not Found

How to fix it:

Double check that you've used the correct endpoint in your artist request to the Spotify REST API.

return client
.get()
.uri("/artists/{artist_id}", artistId)
.retrieve()
.body(MappedArtist.class);

Also check that your SpotifyClient is configured with the following SPOTIFY_API_URL value.

private static final String SPOTIFY_API_URL = "https://spotify-demo-api-fe224840a08c.herokuapp.com/v1";

Still having trouble? Visit the Odyssey forums to get help.

Who is the artist for track 'Lemon Tree' in the query response?

You did it!

Fantastic job completing this server-side lab. You've tackled from start to finish the task of bringing in a new feature to our GraphQL API. From data sources to datafetchers, the Artist type is now part of our music catalog app.

But what better way to end a solo mission than with another challenge? Check out the implementation of the Track.artist datafetcher, and see if you notice something in need of a performance tune-up...

Then, check out the next course in the DGS series: Data Loaders with Java & DGS. The journey continues!

Previous

Share your questions and comments about this lesson

Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              GraphQL

              An open-source query language and specification for APIs that enables clients to request specific data, promoting efficiency and flexibility in data retrieval.

              NEW COURSE ALERT

              Introducing Apollo Connectors

              Connectors are the new and easy way to get started with GraphQL, using existing REST APIs.

              Say goodbye to GraphQL servers and resolvers—now, everything happens in the schema!

              Take the course