2016-07-03 47 views
0

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; 

當我從User架構刪除offers,requestscomments字段,它正在工作。
我試過要求在User中定義的所有依賴關係,但我仍然得到相同的錯誤。
我錯過了什麼?

回答

1

這實際上是你如何構建你的模塊的問題,任何與GraphQL都不是問題。事實上,通過用簡單對象和console.log()來代替GraphQL結構來說明相同的問題。

由於user.js和offer.js需要彼此,因此require()返回的Module Exports對象在那個時刻不完整。您可以通過在require()的結果上調用console.log()然後在以後運行module.exports = anything來替代模塊導出對象,而不是改變現有模塊導出對象。由於之前(空)的Exports對象已經是必需的(),所以這個新的導出從不使用。

我相信你可以通過維護現有的模塊導出對象來解決這個問題。用exports.something = something代替module.exports = something

+0

注意:如果您使用ES6模塊,則無法使用該語法覆蓋Module Export對象,因此無法解決此問題。 –

+0

它實際上將錯誤移到了'index.js'文件(查詢對象被定義的地方)。我想我會轉向ES6。謝謝! – itaied

+0

更新:ES6做到了。 – itaied

相關問題