Commit 631182f5 authored by alexlbr's avatar alexlbr

updates readme and src/index.js

parent 2f8a3a90
...@@ -52,7 +52,7 @@ We are going to create our own GraphQL API on top of this [Rick and Morty API](h ...@@ -52,7 +52,7 @@ We are going to create our own GraphQL API on top of this [Rick and Morty API](h
- [ ] 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: - [ ] 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:
- 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. - 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.
- You can filter the episodes in our case using the `mapEpisodeUrlToEpisode` defined at the bottom of this file `src/index.js`. - You can use the helper fetch functions defined at the bottom of this file `src/index.js`.
- [ ] 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/) - [ ] 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/)
......
...@@ -86,14 +86,6 @@ const episodes = [ ...@@ -86,14 +86,6 @@ const episodes = [
} }
]; ];
function getEpisodeIdFromUrl(url) {
return url && url.split("/").pop();
}
function mapEpisodeUrlToEpisode(episodes, episodeUrl) {
return episodes.find(e => e.id == getEpisodeIdFromUrl(episodeUrl));
}
function fetchEpisodes() { function fetchEpisodes() {
// More info about the fetch function? https://github.com/bitinn/node-fetch#json // More info about the fetch function? https://github.com/bitinn/node-fetch#json
return fetch("https://rickandmortyapi.com/api/episode/") return fetch("https://rickandmortyapi.com/api/episode/")
...@@ -101,6 +93,18 @@ function fetchEpisodes() { ...@@ -101,6 +93,18 @@ function fetchEpisodes() {
.then(json => json.results); .then(json => json.results);
} }
function fetchEpisodeById(id) {
return fetch("https://rickandmortyapi.com/api/episode/" + id)
.then(res => res.json())
.then(json => json);
}
function fetchEpisodeByUrl(url) {
return fetch(url)
.then(res => res.json())
.then(json => json);
}
function fetchCharacters() { function fetchCharacters() {
// More info about the fetch function? https://github.com/bitinn/node-fetch#json // More info about the fetch function? https://github.com/bitinn/node-fetch#json
return fetch("https://rickandmortyapi.com/api/character/") return fetch("https://rickandmortyapi.com/api/character/")
...@@ -108,9 +112,16 @@ function fetchCharacters() { ...@@ -108,9 +112,16 @@ function fetchCharacters() {
.then(json => json.results); .then(json => json.results);
} }
function fetchCharacter(id) { function fetchCharacterById(id) {
// More info about the fetch function? https://github.com/bitinn/node-fetch#json // More info about the fetch function? https://github.com/bitinn/node-fetch#json
return fetch("https://rickandmortyapi.com/api/character/" + id) return fetch("https://rickandmortyapi.com/api/character/" + id)
.then(res => res.json()) .then(res => res.json())
.then(json => json); .then(json => json);
} }
function fetchCharacterByUrl(url) {
// More info about the fetch function? https://github.com/bitinn/node-fetch#json
return fetch(url)
.then(res => res.json())
.then(json => json);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment