Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何解决GraphQL循环引用?_Node.js_Graphql - Fatal编程技术网

Node.js 如何解决GraphQL循环引用?

Node.js 如何解决GraphQL循环引用?,node.js,graphql,Node.js,Graphql,我正在尝试将我的类型分离到单独的文件中。通过这样做,我发现任何引用usertype的类型定义都会出现以下错误。 用户: 许可证: const { GraphQLObjectType, GraphQLInt, GraphQLString, GraphQLBoolean, GraphQLID, } = require("graphql"); const UserType = require("../types/userType"); const UserController =

我正在尝试将我的类型分离到单独的文件中。通过这样做,我发现任何引用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.

我已尝试将类型/控制器定义移动到类型定义的上方和下方,但未做任何更改。如何从许可证类型中提供用户数据?

您可以将
require
调用移动到
字段
函数中——这样,在构建模式时实际调用函数之前,不会对它们进行评估

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

您可以将
require
调用移动到
字段
函数中——这样,在构建模式时实际调用函数之前,不会对调用进行计算

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