简体   繁体   中英

Getting field type error on complex object in graphql

I require a specific model in one of my models and I get Error: Offer.user field type must be Output Type but got: [object Object].

Offer:

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;

User:

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;

When I remove the offers , requests and comments fields from the User schema, it is working.
I tried requiring all the dependencies defined in User but I still get the same error.
What am I missing?

This is actually an issue with how you've build your modules, any not really an issue with GraphQL. In fact you can illustrate the same issue by replacing the GraphQL structures with simple objects and console.log() to illustrate.

Because user.js and offer.js require each other, the Module Exports object returned by require() is incomplete at that moment in time. You can see this for yourself by calling console.log() on the result of require() then later when you run module.exports = anything You're replacing the Module Exports object rather than mutating the existing one. Since the previous (empty) Exports object was already required(), this new export is never used.

I believe you can fix this by maintaining the existing Module Export objects. Replacing module.exports = something with exports.something = something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM