如何解决GraphQL循环引用?

阅读:1118 评论:0

如何解决GraphQL循环引用?

如何解决GraphQL循环引用?

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

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 UserLicenses({ id: parent._id });
      },
    },
    services: {
      type: new GraphQLList(ServiceType),
      async resolve(parent, args) {
        return UserServices({ id: parent._id });
      },
    },
  }),
});

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

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

ports = 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 SingleUser({ id: parent.user_id });
      },
    },
  }),
});

ports = LicenseType;

错误:

Error: One of the provided types for building the Schema is missing a name.

我已经尝试过将类型/控制器定义在类型定义的上方和下方移动,而无需进行更改。如何从许可证类型中提供用户数据?

回答如下:

您可以在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 SingleUser({ id: parent.user_id });
        },
      },
    }
  },
});

本文发布于:2024-05-13 11:04:17,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/1715575462253683.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:如何解决   GraphQL
留言与评论(共有 0 条评论)
昵称:
匿名发表 登录账号
         
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23