GraphQL:提供的用于构建模式的类型之一缺少名称 [英] GraphQL: One of the provided types for building the Schema is missing a name
问题描述
我正在学习GraphQL,所以遇到了一个奇怪的问题
I'm learning GraphQL so I got a strange issue
我在一个文件Schema.js上有以下代码:
I have this code on one file Schema.js:
const graphQL = require('graphql');
const lodash = require('lodash')
const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLID, GraphQLSchema, GraphQLList } = graphQL;
const StatusType = new GraphQLObjectType({
name: 'Status',
fields: () => ({
id: { type: GraphQLInt },
statusName: { type: GraphQLString },
user: {
type: new GraphQLList(UserType),
resolve(parentValue, args){
}
}
})
});
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: GraphQLString },
username: { type: GraphQLString },
mail: { type: GraphQLString },
password: { type: GraphQLString },
status: {
type: StatusType,
resolve(parentValue, args){
}
},
})
});
const RouteQuery = new GraphQLObjectType({
name: 'RouteQuery',
user: {
type: UserType,
args: { id: { type: GraphQLString } },
resolve(parentValue, args){
//return lodash.find(users, { id: args.id })
}
},
userSome: {
type: new GraphQLList(UserType),
args: { id: { type: GraphQLString } },
resolve(parentValue, args){
if (args.id) {
//return users.filter(user => user.id === args.id);
}
//return users;
}
},
userAll: {
type: new GraphQLList(UserType),
resolve(parentValue){
//return users
}
},
status:{
type: StatusType,
args: { id: { type: GraphQLInt } },
resolve(parentValue, args){
//return lodash.find(status, { id: args.id })
}
},
statusAll: {
type: new GraphQLList(StatusType),
resolve(parentValue){
//return users
}
}
}
});
module.exports = new GraphQLSchema({
query: RouteQuery
})
此代码成功运行,但是当我尝试将它们分成多个文件时: const StatusType&UserType
如下所示:StatusType位于StatusType.js文件上,而UserType位于UserType.js文件上
This code run succesfully but when i try to separate these into multiple files: the const StatusType & UserType
like the following case:
the StatusType is on StatusType.js file and the UserType is on UserType.js file
StatuType.js文件:
StatuType.js file:
const graphQL = require('graphql');
const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLID, GraphQLSchema, GraphQLList } = graphQL;
const UserType = require('./UserType')
const StatusType = new GraphQLObjectType({
name: 'Status',
fields: () => ({
id: { type: GraphQLInt },
statusName: { type: GraphQLString },
user: {
type: new GraphQLList(UserType),
resolve(parentValue, args){
//return users.filter(user => user.status === parentValue.id);
}
}
})
});
module.exports = StatusType;
UserType.js文件:
UserType.js file:
const graphQL = require('graphql');
const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLID, GraphQLSchema, GraphQLList } = graphQL;
const StatusType = require('./StatusType')
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: GraphQLString },
username: { type: GraphQLString },
mail: { type: GraphQLString },
password: { type: GraphQLString },
status: {
type: StatusType,
resolve(parentValue, args){
//return lodash.find(status, { id: parentValue.status })
}
},
})
});
module.exports = UserType;
在Schema.js文件中,我包括了以下两个代码:
And on the Schema.js file i include these 2 like that:
const StatusType = require('./StatusType');
const UserType = require('./UserType');
因此,我没有将所有代码都放在同一文件中,而是将StatusType和UserType放在了相应文件中.
so instead of putting the all code on the same file, i putted the StatusType and UserType on respective files.
但是当我运行此代码时,出现此错误:
but when i run this code, i got this error:
所以我不知道这里的问题是什么:/
So i don't know what the problem here :/
但是当我尝试进入console.log时, const UserType = require('./UserType')
我得到了 User
作为响应:在Schema.js的相同代码上
But when i'm tring to console.log the const UserType = require('./UserType')
i got User
as response :o like when it was on the same code on Schema.js
推荐答案
您遇到了nodeJs处理 require
的方式的问题.有关如何操作,请参见 http://nodejs.org/api/modules.html#modules_cycles require
在节点中处理.
You are facing a problem in the way nodeJs handle require
. See http://nodejs.org/api/modules.html#modules_cycles for how require
is handled in node.
具体针对您的情况,当您这样做:
Specifically in your case, when you do:
const StatusType = require('./StatusType');
const UserType = require('./UserType');
-
StatusType
是从const StatusType = require('./StatusType');
加载的 - StatusType.js 从
const UserType = require('./UserType')
加载 - UserType.js 应该要求
StatusType
,但是nodeJs阻止了这种情况以避免无限循环.结果,它执行了下一行 -
UserType
初始化为new GraphQLObjectType(...)
,并定义了fields
作为函数.函数关闭将尚未初始化的变量StatusType
传递给它.只是一个空的导出模块{}
UserType
StatusType
is loaded fromconst StatusType = require('./StatusType');
- StatusType.js loads
UserType
fromconst UserType = require('./UserType')
- UserType.js should require
StatusType
but nodeJs prevent this to avoid infinite loop. As a result, it executes next lines UserType
is initialized asnew GraphQLObjectType(...)
and definedfields
as a function. The function closure hand a variableStatusType
not yet initialized. It's just an empty exported module{}
您可以在创建 UserType
字段时验证是否添加了 console.log(StatusType);
:
You can verify that adding console.log(StatusType);
when creating UserType
fields:
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => {
console.log(StatusType);
return ({
id: { type: GraphQLString },
username: { type: GraphQLString },
mail: { type: GraphQLString },
password: { type: GraphQLString },
status: {
type: StatusType,
resolve(parentValue, args) {
}
},
});
}
});
您将获得:
{} //instead of StatusType
当所有内容都在同一个文件中时,您不会遇到此问题,因为 UserType
和 StatusType
都在同一个闭包中定义,而在彼此之前都定义了.
You didn't encounter this problem when everything was in the same file because both UserType
and StatusType
are defined within the same closure and now each others.
要解决这个问题,您必须在同一级别上定义 UserType
和 StatusType
并注入它们.可以在此处找到一个很好的示例.就您而言:
To resolve that you had to define UserType
and StatusType
on the same level and inject them. A good example of how to do it can be found here. In your case:
// StatusType.js
const StatusType = (types) => new GraphQLObjectType({
name: 'Status',
fields: () => {
console.log(types.UserType);
return ({
id: { type: GraphQLInt },
statusName: { type: GraphQLString },
user: {
type: new GraphQLList(types.UserType),
resolve(parentValue, args) {
}
}
});
}
});
module.exports = StatusType;
// UserType.js
const UserType = (types) => new GraphQLObjectType({
name: 'User',
fields: () => {
console.log(types.StatusType);
return ({
id: { type: GraphQLString },
username: { type: GraphQLString },
mail: { type: GraphQLString },
password: { type: GraphQLString },
status: {
type: types.StatusType,
resolve(parentValue, args) {
}
},
});
}
});
module.exports = UserType;
// Schema.js
const StatusTypeInject = require('./StatusType');
const UserTypeInject = require('./UserType');
const types = {};
types.StatusType = StatusTypeInject(types);
types.UserType = UserTypeInject(types);
const StatusType = types.StatusType;
const UserType = types.UserType;
这篇关于GraphQL:提供的用于构建模式的类型之一缺少名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!