graphQl-在db中全部返回

时间:2019-08-31 10:25:37

标签: graphql

我对graphQL真的很陌生

我在这里有一个简单的graphQL模式

const graphql = require('graphql');
const _ = require('lodash');

const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLSchema,
  GraphQLID
} = graphql

const books = [
  { name: "book 1", genre: "book-1", id: "1" },
  { name: "book 2", genre: "book-2", id: "2" },
  { name: "book 3", genre: "book-3", id: "3" }
]

const BookType = new GraphQLObjectType({
  name: 'Book',
  fields: () => ({
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    genre: { type: GraphQLString }
  })
})

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

module.exports = new GraphQLSchema({
  query: RootQuery
})

我可以使用id返回一本书

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

我在想像什么?

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    book: {
      type: BookType,
      args: {},
      resolve(parent, args) {
        return _.find(books, {})
      }
    }
  }
})  

1 个答案:

答案 0 :(得分:1)

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    book: {
      type: BookType,
      args: { displayall: { type: GraphQLID } },
      resolve(parent, args) {
        return _.find(books, { displayall: args.displayall })
      }
    }
    books:{
        type: new GraphQLList(BookType),
          resolve(parent, args) {
            return books
          }
        }
  }
})