This exercise is part of the React GraphQL Academy learning material. The goal of the exercise is to help you get started transitioning from REST to GraphQL.
- Understand the main functionalities and responsibilities of a GraphQL server
- Learn how to migrate an existent REST API to GraphQL and start “thinking in graphs”
- Start identifying potential problems when running real-world GraphQL APIs
https://rickandmortyapi.com/graphql/
- Query a list with all the character names
- Query how many characters are in the system
- Query a single characther by id (try id equals 1) and get its name
- How many types do we have in the system?
We are going to create our own GraphQL API on top of this Rick and Morty API
git clone [email protected]:reactgraphqlacademy/rest-to-graphql-workshop.gitcd rest-to-graphql-workshopyarn installornpm installyarn startornpm start
-
1. Create a
Charactertype in your schema. Use the documentation of the character endpoint to define the shape of theCharactertype.- 1.1. Add a
charactersfield to theQuerytype. You can replace thebooksfield from Query type on line 32 withcharacterssince we won't use books. Thecharactersfield in theQuerytype should return an array of [Character]. - 1.2. Add a
charactersresolver to the Query's resolvers. You can replace thebooksfield from Query type on line 40 withcharacterssince we won't use books. You can return the mock characters array (which is in the scope and defined at the bottom of the file index.js) in the resolver function. - 1.3 You should be able to manually test the
charactersquery in Playground at http://localhost:4000/
- 1.1. Add a
-
2. Create an
Episodetype in your schema. Use the documentation of the episode endpoint to define the shape of theEpisodetype. Here you are practicing what you've learned on the previous task (1).- 2.1. Add an
episodesfield to theQuerytype. Theepisodesfield should return an array of [Episode] - 2.2. Add an
episodesresolver to the Query's resolvers. You can return the mock episodes array (which is in the scope and defined at the bottom of the file index.js) in the resolver function. - 2.3 You should be able to manually test the
episodesquery in Playground at http://localhost:4000/
- 2.1. Add an
-
3. Replace the mock data using https://rickandmortyapi.com/documentation/#rest.
- You can use the
fetchEpisodesandfetchCharactersdefined at the bottom of this filesrc/index.js - You'll need to replace mock data in 2 different places:
- Query characters
- Query episodes
- You can use the
-
4. Create a relationship between Episode type and Character type in your schema. Please have a look at the documentation of the episode endpoint to see how to get the episodes of a given character (heads up! we are calling the field in our Characters
episodesbut the REST API is calling the field that returns an array of episodes asepisode- singular!). Hints:- You need to add a
Characterkey in the resolvers object and an object with anepisodeskey inCharacter. Similar to the Author type and books field in the Apollo documentation. Hint: The first argument of the resolver is the 'parent' type, in this case, the parent of theepisodesfield is theCharacter. parent.episode gives you the array of episodes returned from the REST API. - You can use the helper fetch functions defined at the bottom of this file
src/index.js.
- You need to add a
-
5. Create a query that returns a single Character given an id. You need to fetch the character using
https://rickandmortyapi.com/documentation/#get-a-single-character. Hint, you need to use arguments
- Create the types and resolvers so the following query works:
query episode {
episode(id: 1) {
name
characters {
name
}
}
}
- Once implemented, do you see any vulnerability issues on that query?
- http://graphql.org/learn/
- http://graphql.org/learn/thinking-in-graphs/
- https://dev-blog.apollodata.com/graphql-vs-rest-5d425123e34b
- https://dev-blog.apollodata.com/graphql-explained-5844742f195e
- https://facebook.github.io/relay/docs/thinking-in-graphql.html
- https://dev-blog.apollodata.com/the-anatomy-of-a-graphql-query-6dffa9e9e747
- https://github.com/apollographql/apollo-server
- https://www.youtube.com/watch?v=PHabPhgRUuU
- https://facebook.github.io/relay/graphql/connections.htm
- https://dev-blog.apollodata.com/introducing-launchpad-the-graphql-server-demo-platform-cc4e7481fcba
- https://dev-blog.apollodata.com/
- http://dev.apollodata.com
- https://astexplorer.net/
This material is available for private, non-commercial use under the GPL version 3.