[英]GraphQL: One of the provided types for building the Schema is missing a name
I'm learning GraphQL so I got a strange issue我正在学习 GraphQL 所以我遇到了一个奇怪的问题
I have this code on one file Schema.js:我在一个文件 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
})
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此代码成功运行,但是当我尝试将它们分成多个文件时:
const StatusType & UserType
如下例所示:StatusType 在 StatusType.js 文件上,而 UserType 在 UserType.js 文件上
StatuType.js file: StatuType.js 文件:
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 file: UserType.js 文件:
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;
And on the Schema.js file i include these 2 like that:在 Schema.js 文件中,我像这样包含这两个:
const StatusType = require('./StatusType');
const UserType = require('./UserType');
so instead of putting the all code on the same file, i putted the StatusType and UserType on respective files.因此,我没有将所有代码放在同一个文件中,而是将 StatusType 和 UserType 放在各自的文件中。
but when i run this code, i got this error:但是当我运行这段代码时,我得到了这个错误:
So i don't know what the problem here:/所以我不知道这里有什么问题:/
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但是当我尝试 console.log 时,我得到了
User
作为响应:o 就像它在const UserType = require('./UserType')
上的相同代码上一样
You are facing a problem in the way nodeJs handle require
.您在 nodeJs 处理
require
的方式上遇到了问题。 See http://nodejs.org/api/modules.html#modules_cycles for how require
is handled in node.请参阅http://nodejs.org/api/modules.html#modules_cycles了解如何在节点中处理
require
。
Specifically in your case, when you do:特别是在您的情况下,当您这样做时:
const StatusType = require('./StatusType');
const UserType = require('./UserType');
StatusType
is loaded from const StatusType = require('./StatusType');
const StatusType = require('./StatusType');
StatusType
StatusTypeUserType
from const UserType = require('./UserType')
const UserType = require('./UserType')
加载UserType
StatusType
but nodeJs prevent this to avoid infinite loop. StatusType
但 nodeJs 会阻止这种情况以避免无限循环。 As a result, it executes next linesUserType
is initialized as new GraphQLObjectType(...)
and defined fields
as a function. UserType
初始化为new GraphQLObjectType(...)
并将fields
定义为 function。 The function closure hand a variable StatusType
not yet initialized. StatusType
。 It's just an empty exported module {}
{}
You can verify that adding console.log(StatusType);
您可以验证添加
console.log(StatusType);
when creating UserType
fields:创建
UserType
字段时:
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) {
}
},
});
}
});
You'll get:你会得到:
{} //instead of 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.要解决这个问题,您必须在同一级别上定义
UserType
和StatusType
并注入它们。 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;
You could do with some cleaning up here and here's how I'd resolve these situations:您可以在这里进行一些清理,这是我解决这些情况的方法:
[..]
// import GraphQLNonNull from the graphql lib
// In your case, I'd use GraphQLID instead of GraphQLString
userSome: {
type: new GraphQLList(require('../path/to/UserType')),
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
resolve: async (parentValue, args) => {
// No need for the if statement as we'd sure to have an id.
// return await filter users by id.
}
},
[..]
And as always, keep your fields
as functions: fields: () => ({})
和往常一样,将您的
fields
保留为函数: fields: () => ({})
Injection solves this error注入解决了这个错误
// 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;
Create a types.js file in your directory.在您的目录中创建一个 types.js 文件。 Here you can add all your types that integrate with other types and inject the required types.
在这里,您可以添加与其他类型集成的所有类型并注入所需的类型。
const UserTypeInject = require("./UserType"); const StatusTypeInject = require("./Author"); const types = {}; types.UserType = UserTypeInject(types); types.StatusType = StatusTypeInject(types); const UserType = types.UserType; const StatusType = types.StatusType; module.exports = { UserType, StatusType };
Whenever you need the type either in Mutations or Queries, you import it from the types.js files as such;每当您需要 Mutations 或 Queries 中的类型时,您都可以从 types.js 文件中导入它;
const { UserType } = require("../types");
https://i.stack.imgur.com/ORvCG.png https://i.stack.imgur.com/ORvCG.png
Please see the above Image and code line: 5. I was inserting data into my client collection using Mutation GraphQL and used a single quote for ClientType ('ClientType') and got the same error.请参阅上面的图像和代码行: 5. 我正在使用Mutation GraphQL 将数据插入到我的客户端集合中,并对 ClientType ('ClientType') 使用单引号并得到相同的错误。 I have just removed a single quote as the type wants the client GraphQLObjectType and I was providing ClientType as String.
我刚刚删除了一个单引号,因为该类型需要客户端 GraphQLObjectType,并且我将 ClientType 作为字符串提供。
I searched for it for 2 days.我搜索了2天。 Hope it will help anyone.
希望它会帮助任何人。
You are importing UserType inside StatusType before declaration.您在声明之前在 StatusType 中导入 UserType。
const StatusType = require('./StatusType');
const UserType = require('./UserType');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.