Graphql 为嵌套类型定义编写解析程序

Graphql 为嵌套类型定义编写解析程序,graphql,graphql-js,apollo-server,express-graphql,graphql-tools,Graphql,Graphql Js,Apollo Server,Express Graphql,Graphql Tools,假设我的GraphQL API有以下类型定义: const typeDef = ` type Book { title: String author: Author likes: Int } type Author { id: String name: String age: Int books: [Book] } type Q

假设我的GraphQL API有以下类型定义:

const typeDef = `
    type Book {
         title: String
         author: Author
         likes: Int
    }

    type Author {
         id: String
         name: String
         age: Int
         books: [Book]
    }

    type Query{
         books(authorid: String!): Book
    }
`
那么,我需要多少解析程序来完成这个任务?我应该只使用一个解析程序
books
处理此查询请求并返回所有图书和作者信息,还是应该创建多个解析程序,例如
query->books
Book->author
author->books
?我不确定模块化模式和解析器如何协同工作。

无论您需要提供多少类型(书籍、作者等)或输入

const schema = ` 
    type Mutation {
        mutatePost(postId:Int) :Int
    }
    type Query {
        hello: String
        posts: [String]
        books(authorId: String!): Book
    }
  `
您需要使用和在查询中定义的名称相同的名称,在解析器中必须相同

   const resolvers = {
        Query: {
        async hello() {
            return 'Hello';
        },
        async posts() {
            return ['Hello', 'World];
        },
        async books(_, { authorId }) {
            //Return data which you is defined in type Book
            //return Book
        }
        },
        Mutation: {
            async mutatePost(_, {
            postId
            }, context) {
            //return Integer
            }
        },
    }
无论您需要提供多少类型(书籍、作者等)或输入,每个查询和变异都只需要queryResolver和mutationResolver

const schema = ` 
    type Mutation {
        mutatePost(postId:Int) :Int
    }
    type Query {
        hello: String
        posts: [String]
        books(authorId: String!): Book
    }
  `
您需要使用和在查询中定义的名称相同的名称,在解析器中必须相同

   const resolvers = {
        Query: {
        async hello() {
            return 'Hello';
        },
        async posts() {
            return ['Hello', 'World];
        },
        async books(_, { authorId }) {
            //Return data which you is defined in type Book
            //return Book
        }
        },
        Mutation: {
            async mutatePost(_, {
            postId
            }, context) {
            //return Integer
            }
        },
    }

每个查询和变异只需要queryResolver和mutationResolver

我们不是要为类型
Book->author
编写一个解析器吗?在查询或变异中使用它们之前,不需要为它们编写解析器。我在查询返回类型中也使用
类型Book
类型author
书籍:书籍或作者:作者那么你也需要为书籍和作者编写解析器。我们不是要为类型
Book->Author
编写解析器吗?在你在查询或变异中使用它们之前,不需要为它们编写解析器。我在查询返回类型中也使用
Type Book
Type Author
mean books:Book或Authors:Author然后您也需要为books和authers编写解析器。