可能標題不適合我的問題,但讓我解釋我的情況。 我與Graphql schema.Here工作是我最初的schema.js文件https://github.com/sany2k8/graphql-udemy/blob/master/schema/schema.jsGraphql需要模塊外部vs內部GraphQLObjectType
和它工作得很好,那麼我決定把它分割成不同的小文件,例如root_query_type.js,mutation.js,user_type.js和company_type.js。所有文件都作爲模塊導出並循環使用。例如 -
user_type.js
const graphql = require('graphql');
const axios = require('axios');
const { GraphQLObjectType, GraphQLString, GraphQLInt } = graphql;
//const CompanyType = require('./company_type'); // *this line causing error*
const UserType = new GraphQLObjectType({
name: "User",
fields:() => ({
id:{ type: GraphQLString},
firstName:{ type: GraphQLString},
age:{ type: GraphQLInt},
company :{
type: require('./company_type'), // *this line fix the error*
resolve(parentValue, args){
return axios.get(`http://localhost:3000/companies/${parentValue.companyId}`)
.then(res => res.data)
}
}
})
});
module.exports = UserType;
company_type.js
const graphql = require('graphql');
const axios = require('axios');
const { GraphQLObjectType, GraphQLString, GraphQLList } = graphql;
const UserType = require('./user_type');
const CompanyType = new GraphQLObjectType({
name: "Company",
fields:()=> ({
id: { type: GraphQLString},
name: { type: GraphQLString},
description: { type: GraphQLString},
users:{
type: new GraphQLList(UserType),
resolve(parentValue, args){
return axios.get(`http://localhost:3000/companies/${parentValue.id}/users`)
.then(res => res.data)
}
}
})
});
module.exports = CompanyType;
在我user_type.js文件,當我使用const CompanyType = require('./company_type');
在文件中這樣的頂const UserType它向我顯示以下錯誤消息
Error: User.company field type must be Output Type but got: [object Object].
但是如果我註釋掉那條線並直接放它然後它就會起作用。
company :{
type: require('./company_type'),
resolve(parentValue, args){
return axios.get(`http://localhost:3000/companies/${parentValue.companyId}`)
.then(res => res.data)
}
}
所以基本上我的問題是,爲什麼它不與const CompanyType = require('./company_type');
工作,但type: require('./company_type')
工作。我可能是一個簡單的邏輯問題,但它無法找到。請幫助我。
是的,我現在明白了。感謝您的鏈接參考。 –