在graphql中获取复杂对象的字段类型错误

时间:2016-07-03 18:17:49

标签: node.js sequelize.js graphql

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;

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

1 个答案:

答案 0 :(得分:1)

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

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

我相信你可以通过维护现有的Module Export对象来解决这个问题。用module.exports = something

替换exports.something = something