gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:50:38 24 4
gpt4 key购买 nike

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

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

user.js(用户类型):

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(图书类型):

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) }
})
});

如您所见,我记录了 BookTypeUserType。奇怪的是,当我记录 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com