“预期未定义为 GraphQL 类型”的含义?
Posted
技术标签:
【中文标题】“预期未定义为 GraphQL 类型”的含义?【英文标题】:Meaning of "Expected undefined to be a GraphQL type"? 【发布时间】:2021-03-17 17:03:14 【问题描述】:我正在使用 graphql 和 graphql-express 设置我的第一个 GraphQL 端点。我的 MongoDB 架构有两种类型:User 和 Card,其中一个 User 可以拥有许多 Card。我设置了根类型和解析器,没有任何问题。我可以查询和改变数据库中的用户和卡片。
当我尝试在用户中嵌套卡片时出现问题,我得到Error: Expected undefined to be a GraphQL type
。该错误仅在我将UserType.fields.cards.type
设置为graphql.GraphQLList
的实例时出现,但我在QueryType.js 中有一个相同的field
值,并且工作正常。
查看 Stack Overflow 上的其他问题,看来我的问题可能是我的 User 和 Card 类型之间的循环引用,但我正在努力寻找任何问题在我的代码中。我这里有什么遗漏吗?
完整的堆栈跟踪是:
Error: Expected undefined to be a GraphQL type.
at assertType (/Users/QuestionMark/Documents/flash-cards/node_modules/graphql/type/definition.js:97:11)
at new GraphQLList (/Users/QuestionMark/Documents/flash-cards/node_modules/graphql/type/definition.js:323:19)
at GraphQLList (/Users/QuestionMark/Documents/flash-cards/node_modules/graphql/type/definition.js:325:12)
at fields (/Users/QuestionMark/Documents/flash-cards/server/schema/types/UserType.js:16:13)
at resolveThunk (/Users/QuestionMark/Documents/flash-cards/node_modules/graphql/type/definition.js:480:40)
at defineFieldMap (/Users/QuestionMark/Documents/flash-cards/node_modules/graphql/type/definition.js:692:18)
at GraphQLObjectType.getFields (/Users/QuestionMark/Documents/flash-cards/node_modules/graphql/type/definition.js:633:27)
at collectReferencedTypes (/Users/QuestionMark/Documents/flash-cards/node_modules/graphql/type/schema.js:366:81)
at collectReferencedTypes (/Users/QuestionMark/Documents/flash-cards/node_modules/graphql/type/schema.js:368:9)
at new GraphQLSchema (/Users/QuestionMark/Documents/flash-cards/node_modules/graphql/type/schema.js:153:7)
at Object.<anonymous> (/Users/QuestionMark/Documents/flash-cards/server/schema/schema.js:5:16)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
这是我的相关代码供参考:
// ./UserType.js
const
GraphQLObjectType, GraphQLList, GraphQLID, GraphQLString
= require('graphql');
const CardType = require('./CardType');
const cardsResolver = require('../resolvers/userResolvers');
const UserType = new GraphQLObjectType(
name: 'User',
fields: () => (
_id: type: GraphQLID ,
username: type: GraphQLString ,
password: type: GraphQLString ,
firstname: type: GraphQLString ,
lastname: type: GraphQLString ,
cards:
type: GraphQLList(CardType),
resolve: cardsResolver
)
);
module.exports = UserType;
// ./CardType.js
const GraphQLObjectType, GraphQLID, GraphQLString = require('graphql');
const CardType = new GraphQLObjectType(
name: 'Card',
fields:
_id: type: GraphQLID ,
title: type: GraphQLString ,
body: type: GraphQLString ,
group: type: GraphQLString
);
module.exports = CardType;
// ./root/QueryType
const
GraphQLObjectType,
GraphQLList,
GraphQLID,
GraphQLString
= require('graphql');
const UserType = require('../UserType');
const CardType = require('../CardType');
const
userResolver,
usersResolver,
cardResolver,
cardsResolver
= require('../../resolvers/root/queryResolvers');
const QueryType = new GraphQLObjectType(
name: 'Query',
fields:
user:
type: UserType,
args:
_id: type: GraphQLID ,
username: type: GraphQLString
,
resolve: userResolver
,
users:
type: GraphQLList(UserType),
resolve: usersResolver
,
card:
type: CardType,
args:
_id: type: GraphQLID
,
resolve: cardResolver
,
cards:
type: GraphQLList(CardType),
resolve: cardsResolver
);
module.exports = QueryType;
【问题讨论】:
【参考方案1】:在 UserType.js 中,我解构了 require('./CardType')
的输出,导致 CardType
未定义。 GraphQLList
期望 GraphQLObjectType 的实例,但收到 undefined,因此出现错误。
const CardType = require('./CardType'); // old code
const CardType = require('./CardType'); // new code
【讨论】:
以上是关于“预期未定义为 GraphQL 类型”的含义?的主要内容,如果未能解决你的问题,请参考以下文章