如何拆分RootQuery?

时间:2019-05-14 14:23:02

标签: graphql

我的问题很简单:如何拆分代码? 或对其进行重构,因为如果我有30个查询,则无法读取... 我想为每个查询创建一个文件,但是我做不到。 有什么主意吗?

const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    book: {
      type: BookType,
      args: {
        id: {
          type: GraphQLID
        }
      },
      resolve(parent, args) {
        return _.find(books, { id: args.id })
      }
    },
    author: {
      type: AuthorType,
      args : {
        id: {
          type: GraphQLID
        }
      },
      resolve(parent, args) {
        return _.find(authors, { id: args.id })
      }
    },
    authors: {
      type: new GraphQLList(AuthorType),
      resolve(parent, args) {
        return authors
      }
    },
    books: {
      type: new GraphQLList(BookType),
      resolve(parent, args) {
        return books
      }
    },
  }
})

1 个答案:

答案 0 :(得分:0)

有变量吗?

const book = {
  type: BookType,
  args: {
    id: {
      type: GraphQLID
    }
  },
  resolve(parent, args) {
    return _.find(books, { id: args.id })
  }
}

const author = {
  // ...
}

const authors = {
  // ...
}

const books = {
  // ...
}

const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: { book, author, authors, books }
})