GraphQL:在GraphQLObjectType中使用自定义类型定义GraphQLList时出错

时间:2018-04-13 14:39:07

标签: javascript node.js graphql graphql-js

我想学习GraphQL,我正在编写一个代表书店的例子,其中用户可以成为多本书的作者。

我的问题是我无法定义类型列表" BookType"在 UserType (GraphQLObjectType)中。奇怪的是,当BookType有作者时,它不会抛出任何错误。

user.js(UserType):

const {
    GraphQLList,
    GraphQLID, 
    GraphQLString, 
    GraphQLObjectType,
    GraphQLNonNull,
    GraphQLInt,
     } = require('graphql');

const BookType = require('./book');
const BookSchema = require('../db/models/book');

console.log(BookType);

module.exports = new GraphQLObjectType({
    name: 'User',
    fields: () => ({
        id: { type: new GraphQLNonNull(GraphQLID) },
        name: { type: new GraphQLNonNull(GraphQLString) },
        books: { 
            type: new GraphQLList(BookType),
            resolve(parent, args){
                return BookSchema.find({_id: parent.id});
            }
         }
    })
});

book.js(BookType):

const {
    GraphQLID, 
    GraphQLString, 
    GraphQLObjectType,
    GraphQLNonNull,
    GraphQLInt } = require('graphql');

const UserType = require('./user');
const UserSchema = require('../db/models/user');

console.log(UserType);


module.exports = new GraphQLObjectType({
    name: 'Book',
    fields: () => ({
        id: { type: new GraphQLNonNull(GraphQLID) },
        author: { 
            type: UserType,
            resolve(parent, args){
                return UserSchema.findOne({_id: parent.author});
            }
         },
        title: { type: new GraphQLNonNull(GraphQLString) },
        sites: { type: new GraphQLNonNull(GraphQLInt) }
    })
});

如您所见,我记录了 BookType UserType 。奇怪的是,当我记录 BookType 时,我得到一个空对象。

输出:

console.log(BookType) --> {}
console.log(UserType) --> User

这是错误:

Error: Expected [object Object] to be a GraphQL type.
    at invariant (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/jsutils/invariant.js:19:11)
    at assertType (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/definition.js:88:43)
    at new GraphQLList (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/definition.js:258:19)
    at fields (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/src/types/user.js:21:19)
    at resolveThunk (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/definition.js:370:40)
    at defineFieldMap (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/definition.js:532:18)
    at GraphQLObjectType.getFields (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/definition.js:506:44)
    at typeMapReducer (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:232:38)
    at /media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:239:20
    at Array.forEach (<anonymous>)
    at typeMapReducer (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:232:51)
    at /media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:239:20
    at Array.forEach (<anonymous>)
    at typeMapReducer (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:232:51)
    at Array.reduce (<anonymous>)
    at new GraphQLSchema (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:122:28)

1 个答案:

答案 0 :(得分:0)

你在这里重复了很多代码。我会开始创建一个文件,称之为cffi

开发schema.js,这是一个可以提供帮助的示例:

RootQuery

导出,然后为UserType和其他人创建函数。这是一个例子:

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    user: {
      type: UserType,
      args: { id: { type: GraphQLString } },
      resolve(parentValue, args) {
        return axios
          .get(`http://localhost:3000/users/${args.id}`)
          .then(res => res.data);
      }
    },
    company: {
      type: CompanyType,
      args: { id: { type: GraphQLString } },
      resolve(parentValue, args) {
        return axios
          .get(`http://localhost:3000/companies/${args.id}`)
          .then(res => res.data);
      }
    }
  }
});

因此,您可以只导出一次

,而不是为每个导出const UserType = new GraphQLObjectType({ name: 'User', fields: () => ({ id: { type: GraphQLString }, firstName: { type: GraphQLString }, age: { type: GraphQLInt }, company: { type: CompanyType, resolve(parentValue, args) { return axios .get(`http://localhost:3000/companies/${parentValue.companyId}`) .then(res => res.data); } } }) });
GraphQLObjectType