我想学习GraphQL,并且正在编写一个代表Book-Shop的示例,其中用户可以是多本书的作者。
我的问题是我无法在UserType(GraphQLObjectType)中定义类型为“ BookType”的列表。奇怪的是,当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)
最佳答案
您在这里重复了很多代码。我将开始创建一个文件,将其称为schema.js
。
正在开发您的RootQuery
,这是一个可以帮助您的示例:
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);
}
}
}
});
导出,然后为UserType和其他用户创建函数。这是一个例子:
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
,而只需导出一次module.exports = new GraphQLSchema({ query: RootQuery });
关于javascript - GraphQL:在GraphQLObjectType中用自定义类型定义GraphQLList时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49819507/