|
| 1 | +# Thinking in GraphQL exercise |
| 2 | + |
| 3 | +This exercise is part of the [React GraphQL Academy](http://reactgraphql.academy) learning material. The goal of the exercise is to help you get started transitioning from REST to GraphQL. |
| 4 | + |
| 5 | +## Learning objectives |
| 6 | + |
| 7 | +- Thinking in Graphs |
| 8 | +- Learn how to connect resolvers to a REST API |
| 9 | +- Understand Schema Design principles |
| 10 | + |
| 11 | +## Exercise part 1 |
| 12 | + |
| 13 | +[https://rickandmortyapi.com/graphql/](https://rickandmortyapi.com/graphql/) |
| 14 | + |
| 15 | +- Query a list with all the character names |
| 16 | +- Query how many characters are in the system |
| 17 | +- Query a single characther by id (try id equals 1) and get its name |
| 18 | +- How many types do we have in the system? |
| 19 | + |
| 20 | +## Exercise part 2 |
| 21 | + |
| 22 | +### To get started |
| 23 | + |
| 24 | +We are going to create our own GraphQL API on top of this [Rick and Morty API](https://rickandmortyapi.com/documentation/#rest) |
| 25 | + |
| 26 | +- `git clone [email protected]:reactgraphqlacademy/rest-to-graphql-workshop.git` |
| 27 | +- `cd rest-to-graphql-workshop` |
| 28 | +- `yarn install` or `npm install` |
| 29 | +- `yarn start` or `npm start` |
| 30 | + |
| 31 | +### Tasks |
| 32 | + |
| 33 | +- [ ] 1. Create a `Character` type in your schema. Use the [documentation of the character endpoint](https://rickandmortyapi.com/documentation/#character-schema) to define the shape of the `Character` type. |
| 34 | + |
| 35 | + - [ ] 1.1. Add a `characters` field to the `Query` type. You can replace the `books` field from Query type on line 32 with `characters` since we won't use books. The `characters` field in the `Query` type should return an array of [Character]. |
| 36 | + - [ ] 1.2. Add a `characters` resolver to the Query's resolvers. You can replace the `books` field from Query type on line 40 with `characters` since 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. |
| 37 | + - [ ] 1.3 You should be able to manually test the `characters` query in Playground at [http://localhost:4000/](http://localhost:4000/) |
| 38 | + |
| 39 | +- [ ] 2. Create an `Episode` type in your schema. Use the [documentation of the episode endpoint](https://rickandmortyapi.com/documentation/#episode-schema) to define the shape of the `Episode` type. Here you are practicing what you've learned on the previous task (1). |
| 40 | + |
| 41 | + - [ ] 2.1. Add an `episodes` field to the `Query` type. The `episodes` field should return an array of [Episode] |
| 42 | + - [ ] 2.2. Add an `episodes` resolver 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. |
| 43 | + - [ ] 2.3 You should be able to manually test the `episodes` query in Playground at [http://localhost:4000/](http://localhost:4000/) |
| 44 | + |
| 45 | +- [ ] 3. Replace the mock data using https://rickandmortyapi.com/documentation/#rest. |
| 46 | + |
| 47 | + - You can use the `fetchEpisodes` and `fetchCharacters` defined at the bottom of this file `src/index.js` |
| 48 | + - You'll need to replace mock data in 2 different places: |
| 49 | + - Query characters |
| 50 | + - Query episodes |
| 51 | + |
| 52 | +- [ ] 4. Create a relationship between Episode type and Character type in your schema. Please have a look at the [documentation of the episode endpoint](https://rickandmortyapi.com/documentation/#episode-schema) to see how to get the episodes of a given character (heads up! we are calling the field in our Characters `episodes` but the REST API is calling the field that returns an array of episodes as `episode` - singular!). Hints: |
| 53 | + |
| 54 | + - You need to add a `Character` key in the resolvers object and an object with an `episodes` key in `Character`. Similar to the Author type and books field in the [Apollo documentation](https://www.apollographql.com/docs/apollo-server/essentials/data#resolver-map). Hint: The first argument of the resolver is the 'parent' type, in this case, the parent of the `episodes` field is the `Character`. parent.episode gives you the array of episodes returned from the REST API. |
| 55 | + - You can use the helper fetch functions defined at the bottom of this file `src/index.js`. |
| 56 | + |
| 57 | +- [ ] 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](https://graphql.org/graphql-js/passing-arguments/) |
| 58 | + |
| 59 | +### Bonus |
| 60 | + |
| 61 | +- Create the types and resolvers so the following query works: |
| 62 | + |
| 63 | +``` |
| 64 | +query episode { |
| 65 | + episode(id: 1) { |
| 66 | + name |
| 67 | + characters { |
| 68 | + name |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +- Once implemented, do you see any vulnerability issues on that query? |
| 75 | + |
| 76 | +## Articles and links |
| 77 | + |
| 78 | +- http://graphql.org/learn/ |
| 79 | +- http://graphql.org/learn/thinking-in-graphs/ |
| 80 | +- https://dev-blog.apollodata.com/graphql-vs-rest-5d425123e34b |
| 81 | +- https://dev-blog.apollodata.com/graphql-explained-5844742f195e |
| 82 | +- https://facebook.github.io/relay/docs/thinking-in-graphql.html |
| 83 | +- https://dev-blog.apollodata.com/the-anatomy-of-a-graphql-query-6dffa9e9e747 |
| 84 | +- https://github.com/apollographql/apollo-server |
| 85 | +- https://www.youtube.com/watch?v=PHabPhgRUuU |
| 86 | +- https://facebook.github.io/relay/graphql/connections.htm |
| 87 | +- https://dev-blog.apollodata.com/introducing-launchpad-the-graphql-server-demo-platform-cc4e7481fcba |
| 88 | +- https://dev-blog.apollodata.com/ |
| 89 | +- http://dev.apollodata.com |
| 90 | +- https://astexplorer.net/ |
| 91 | + |
| 92 | +## License |
| 93 | + |
| 94 | +This material is available for private, non-commercial use under the [GPL version 3](http://www.gnu.org/licenses/gpl-3.0-standalone.html). |
0 commit comments