タイトルは私の問題では適切ではないかもしれませんが、私のシナリオを説明してください。 私はGraphql schema.Hereで働いている私の最初のschema.jsがhttps://github.com/sany2k8/graphql-udemy/blob/master/schema/schema.jsGraphqlはGraphProjectType内外のモジュールを必要としています
を提出して、それがうまく働いた後、私は別の小さなファイルに分割することを決めたroot_query_type.js、mutation.js、を例えばuser_type.jsとcompany_type.js。すべてのファイルはモジュールとしてエクスポートされ、循環的に必要です。例えば - 私はこのようなファイルの先頭にconst CompanyType = require('./company_type');
を使用
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私user_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;
ファイル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')
で動作していませんさ。私は単純な論理的な問題になる可能性がありますが見つかりませんでした。私を助けてください。
はい私は今それを得ました。リンクリファレンスをありがとう。 –