"Expected undefined to be a GraphQL type"的含义?

Meaning of "Expected undefined to be a GraphQL type"?

我正在使用 graphql 和 graphql-express 设置我的第一个 GraphQL 端点。我的 MongoDB 架构有两种类型:UserCard,其中一个用户可以有很多张卡片。我毫无问题地设置了根类型和解析器。我可以查询和改变数据库中的用户和卡片。

当我尝试在用户中嵌套卡片时出现问题,我得到 Error: Expected undefined to be a GraphQL type。只有当我将 UserType.fields.cards.type 设置为 graphql.GraphQLList 的实例时才会出现错误,但我在 QueryType.js 中有相同的 field 值, 效果很好。

查看 Stack Overflow 上的其他问题,看来我的问题可能是我的 UserCard 类型之间的循环引用,但我很难在我的代码中找到任何问题。我在这里遗漏了什么吗?

完整的堆栈跟踪是:

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;

UserType.js 中,我解构了 require('./CardType') 的输出,导致 CardTypeundefined. GraphQLList 需要一个 GraphQLObjectType 的实例,但收到的却是 undefined,因此出现错误。

    const { CardType } = require('./CardType'); // old code
    const CardType = require('./CardType'); // new code