Node.js 如何解决GraphQL循环引用?
我正在尝试将我的类型分离到单独的文件中。通过这样做,我发现任何引用usertype的类型定义都会出现以下错误。 用户: 许可证:Node.js 如何解决GraphQL循环引用?,node.js,graphql,Node.js,Graphql,我正在尝试将我的类型分离到单独的文件中。通过这样做,我发现任何引用usertype的类型定义都会出现以下错误。 用户: 许可证: const { GraphQLObjectType, GraphQLInt, GraphQLString, GraphQLBoolean, GraphQLID, } = require("graphql"); const UserType = require("../types/userType"); const UserController =
const {
GraphQLObjectType,
GraphQLInt,
GraphQLString,
GraphQLBoolean,
GraphQLID,
} = require("graphql");
const UserType = require("../types/userType");
const UserController = require("../../controllers/userController");
const LicenseType = new GraphQLObjectType({
name: "License",
fields: () => ({
_id: { type: GraphQLID },
token: { type: GraphQLString },
creationDate: { type: GraphQLString },
expirationDate: { type: GraphQLString },
btcAddress: { type: GraphQLString },
sessions: { type: GraphQLInt },
active: { type: GraphQLBoolean },
user_id: { type: GraphQLID },
user: {
type: UserType,
async resolve(parent, args) {
return await UserController.getSingleUser({ id: parent.user_id });
},
},
}),
});
module.exports = LicenseType;
错误:
Error: One of the provided types for building the Schema is missing a name.
我已尝试将类型/控制器定义移动到类型定义的上方和下方,但未做任何更改。如何从许可证类型中提供用户数据?您可以将
require
调用移动到字段
函数中——这样,在构建模式时实际调用函数之前,不会对它们进行评估
const LicenseType = new GraphQLObjectType({
name: "License",
fields: () => {
const UserType = require("../types/userType");
const UserController = require("../../controllers/userController");
return {
_id: { type: GraphQLID },
token: { type: GraphQLString },
creationDate: { type: GraphQLString },
expirationDate: { type: GraphQLString },
btcAddress: { type: GraphQLString },
sessions: { type: GraphQLInt },
active: { type: GraphQLBoolean },
user_id: { type: GraphQLID },
user: {
type: UserType,
async resolve(parent, args) {
return await UserController.getSingleUser({ id: parent.user_id });
},
},
}
},
});
您可以将
require
调用移动到字段
函数中——这样,在构建模式时实际调用函数之前,不会对调用进行计算
const LicenseType = new GraphQLObjectType({
name: "License",
fields: () => {
const UserType = require("../types/userType");
const UserController = require("../../controllers/userController");
return {
_id: { type: GraphQLID },
token: { type: GraphQLString },
creationDate: { type: GraphQLString },
expirationDate: { type: GraphQLString },
btcAddress: { type: GraphQLString },
sessions: { type: GraphQLInt },
active: { type: GraphQLBoolean },
user_id: { type: GraphQLID },
user: {
type: UserType,
async resolve(parent, args) {
return await UserController.getSingleUser({ id: parent.user_id });
},
},
}
},
});