Last active
October 10, 2018 16:27
-
-
Save Akiyamka/c4fb001c5d928695116b3a4c816989ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { ApolloServer, gql } = require('apollo-server'); | |
| const { libraryAPI, bankAPI } = require('./mocks'); | |
| const typeDefs = gql` | |
| type Book { | |
| title: String | |
| author: Author | |
| } | |
| type Author { | |
| name: String | |
| books: [Book] | |
| } | |
| ### Queries ### | |
| type Query {, | |
| books: [Book], | |
| authors: [Author], | |
| } | |
| `; | |
| const resolvers = { | |
| Query: { | |
| // Я получаю книги в формате типа Book. Они все имеют поле автора | |
| books: () => libraryAPI.getAllBooks(), | |
| // Теперь я хочу получить список книг по авторам | |
| authors(obj, args, context, info) { | |
| const authorName = args.name; | |
| // Увы для этого мне нужно снова дернуть ту-же апи во второй раз получить те-же данные | |
| // но на этот раз обработать их | |
| Array.from( | |
| libraryAPI.getAllBooks().reduce((authors, book) => { | |
| authors.add(book.author); | |
| return authors; | |
| }, new Set()) | |
| ).map(name => ( | |
| { name } | |
| )); | |
| }, | |
| Author: { | |
| books(author) { | |
| // Здесь я вынужден снова дублировать запрос в ту-же API! | |
| // На этот раз запросов может быть столько же сколько авторов мы хотим получить | |
| return libraryAPI.getAllBooks().filter(book => book.author === author.name); | |
| }, | |
| }, | |
| }; | |
| const server = new ApolloServer({ typeDefs, resolvers }); | |
| server.listen().then(({ url }) => { | |
| console.log(`🚀 Server ready at ${url}`); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.apollographql.com/docs/graphql-tools/connectors.html#connectors