【发布时间】:2018-02-08 13:23:04
【问题描述】:
可能是标题不适合我的问题,但让我解释一下我的情况。 我正在使用 Graphql 架构。这是我的初始 schema.js 文件https://github.com/sany2k8/graphql-udemy/blob/master/schema/schema.js
它运行良好,然后我决定将其拆分为不同的小文件,例如 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 它在错误消息下方显示我
错误:User.company 字段类型必须是输出类型,但得到:[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') 一起使用。我可能是一个简单的逻辑问题,但它无法找到。请帮帮我。
【问题讨论】:
标签: javascript node.js graphql graphql-js