【问题标题】:Getting field type error on complex object in graphql在graphql中获取复杂对象的字段类型错误
【发布时间】:2016-11-05 11:50:16
【问题描述】:

require我的一个模型中的一个特定模型,我得到Error: Offer.user field type must be Output Type but got: [object Object].

报价:

var graphql = require('graphql'),
  GraphQLBoolean = graphql.GraphQLBoolean,
  GraphQLObjectType = graphql.GraphQLObjectType,
  GraphQLInt = graphql.GraphQLInt,
  GraphQLString = graphql.GraphQLString;
var Game = require('./game');
var Post = require('./post');
var User = require('./user');

var Offer = new GraphQLObjectType({
  name: 'Offer',
  description: 'This object represents the offers for the specified user.',
  fields: () => ({
    id: {
      description: 'The id of the offer.',
      type: GraphQLInt
    },
    createdAt: {
      description: 'The creation date of the offer.',
      type: GraphQLString
    },
    exchange: {
      description: 'Specifies whether this offer is exchangeable.',
      type: GraphQLBoolean
    },
    price: {
      description: 'The price for the specified offer.',
      type: GraphQLInt
    },
    game: {
      description: 'The corresponding game of the offer.',
      type: Game,
      resolve: offer => offer.getGame()
    },
    post: {
      description: 'The corresponding post which expand this offer.',
      type: Post,
      resolve: offer => offer.getPost()
    },
    user: {
      description: 'The user who created this offer.',
      type: User,
      resolve: offer => offer.getUser()
    }
  })
});

module.exports = Offer;

用户:

var graphql = require('graphql'),
  GraphQLString = graphql.GraphQLString,
  GraphQLObjectType = graphql.GraphQLObjectType,
  GraphQLList = graphql.GraphQLList;
var Offer = require('./offer');
var Request = require('./request');
var Comment = require('./comment');

var User = new GraphQLObjectType({
  name: 'User',
  description: 'This object represents the registered users.',
  fields: () => ({
    id: {
      description: 'The id of the user.',
      type: GraphQLString
    },
    name: {
      description: 'The full name of the user.',
      type: GraphQLString
    },
    email: {
      description: 'The email of the user.',
      type: GraphQLString
    },
    phone: {
      description: 'The phone number of the user.',
      type: GraphQLString
    },
    createdAt: {
      description: 'The creation date of the user.',
      type: GraphQLString
    },
    offers: {
      description: 'The offers created by the user.',
      type: new GraphQLList(Offer),
      resolve: user => user.getOffers()
    },
    requests: {
      description: 'The requests created by the user.',
      type: new GraphQLList(Request),
      resolve: user => user.getRequests()
    },
    comments: {
      description: 'The comments this users wrote.',
      type: new GraphQLList(Comment),
      resolve: user => user.getComments()
    }
  })
});

module.exports = User;

当我从 User 架构中删除 offersrequestscomments 字段时,它正在工作。
我尝试要求 User 中定义的所有依赖项,但我仍然遇到相同的错误。
我错过了什么?

【问题讨论】:

    标签: node.js sequelize.js graphql


    【解决方案1】:

    这实际上是您如何构建模块的问题,而 GraphQL 并不是真正的问题。实际上,您可以通过用简单的对象和 console.log() 替换 GraphQL 结构来说明相同的问题。

    因为 user.js 和 offer.js 互相需要,所以此时 require() 返回的 Module Exports 对象是不完整的。您可以通过在 require() 的结果上调用 console.log() 来亲自看到这一点,然后当您运行 module.exports = anything 时,您正在替换 Module Exports 对象,而不是改变现有的对象。由于之前的(空)Exports 对象已经是 required(),因此这个新的 export 永远不会被使用。

    我相信您可以通过维护现有的模块导出对象来解决此问题。用exports.something = something替换module.exports = something

    【讨论】:

    • 注意:如果您使用 ES6 模块,则不会出现此问题,因为您无法使用该语法覆盖 Module Export 对象。
    • 它实际上将错误移动到index.js 文件(其中定义了查询对象)。我想我会转向 ES6。谢谢!
    猜你喜欢
    • 2018-08-15
    • 2018-04-07
    • 2019-12-30
    • 1970-01-01
    • 2018-06-22
    • 2020-05-29
    • 2022-06-12
    • 2020-06-26
    • 2021-06-22
    相关资源
    最近更新 更多