【问题标题】:How to solve GraphQL circular references?如何解决 GraphQL 循环引用?
【发布时间】:2020-06-15 07:02:37
【问题描述】:

我正在尝试将我的类型分成单独的文件。通过这样做,我发现任何引用用户类型的类型定义都会出现以下错误。 用户:

const {
  GraphQLObjectType,
  GraphQLList,
  GraphQLString,
  GraphQLBoolean,
  GraphQLID,
} = require("graphql");

const UserType = new GraphQLObjectType({
  name: "User",
  fields: () => ({
    _id: { type: GraphQLID },
    name: { type: GraphQLString },
    password: { type: GraphQLString },
    email: { type: GraphQLString },
    active: { type: GraphQLBoolean },
    emailConfirmed: { type: GraphQLBoolean },
    licenses: {
      type: new GraphQLList(LicenseType),
      async resolve(parent, args) {
        return await LicenseController.getUserLicenses({ id: parent._id });
      },
    },
    services: {
      type: new GraphQLList(ServiceType),
      async resolve(parent, args) {
        return await ServiceController.getUserServices({ id: parent._id });
      },
    },
  }),
});

const LicenseType = require("../types/licenseType");
const ServiceType = require("../types/serviceType");

const LicenseController = require("../../controllers/licenseController");
const ServiceController = require("../../controllers/serviceController");

module.exports = UserType;

许可证:

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.

我尝试将类型/控制器定义移动到类型定义的上方和下方,而没有任何变化。我将如何提供许可类型的用户数据?

【问题讨论】:

    标签: node.js graphql


    【解决方案1】:

    您可以将require 调用移动到fields 函数内——这样在构建架构时实际调用函数之前,它们不会被评估。

    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 });
            },
          },
        }
      },
    });
    

    【讨论】:

      猜你喜欢
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 2019-09-12
      • 2016-07-10
      相关资源
      最近更新 更多