1

假设我的 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 -> authorAuthor -> books?我不确定模块化架构和解析器如何协同工作。

4

1 回答 1

0

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

const schema = ` 
    type Mutation {
        mutatePost(postId:Int) :Int
    }
    type Query {
        hello: String
        posts: [String]
        books(authorId: String!): Book
    }
  `

您需要使用与您在 Query must be same in resolver 中定义的名称相同的名称

   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
            }
        },
    }

只有每个 Query 和 Mutation 需要 queryResolver 和 mutationResolver

于 2019-04-12T15:00:47.723 回答