2016-07-03 6 views
0

I require私のモデルの一つで特定のモデルと私が得るError: Offer.user field type must be Output Type but got: [object Object].graphqlに複雑なオブジェクトのフィールドタイプのエラーを取得

オファー:

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; 

IをUserスキームからoffers,requestsおよびcommentsのフィールドを削除すると正常に機能しています。
Userで定義されているすべての依存関係が必要でしたが、同じエラーが表示されます。
私は何が欠けていますか?

答えて

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 = somethingexports.something = something

+0

に変更するNB:ES6モジュールを使用している場合、その構文を使用してモジュールエクスポートオブジェクトを上書きすることはできないため、この問題は解決できません。 –

+0

実際には、エラーを 'index.js'ファイル(クエリーオブジェクトが定義されている)に移動しました。私はES6に移りたいと思う。ありがとう! – itaied

+0

更新:ES6はトリックをしました。 – itaied

関連する問題