1
如何从Graphql的多个集合中返回数据?如何从Graphql的多个集合中返回数据?
const jobsCollection = db.collection('jobs');
const companysCollection = db.collection('company');
import {
GraphQLList,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
GraphQLInt,
GraphQLFloat,
GraphQLEnumType,
GraphQLNonNull
} from 'graphql';
const Company = new GraphQLObjectType({
name: 'Company Name',
description: 'test',
fields:() => ({
_id: {type: GraphQLString},
name: {type: GraphQLString},
address : {type: GraphQLString}
}
})
});
通过下面的查询我也想查询公司收集。我怎样才能做到这一点?
const Job = new GraphQLObjectType({
name: 'job',
description: 'test',
fields:() => ({
_id: {type: GraphQLString},
name: {type: GraphQLString},
skill_set : {type: GraphQLString},
company: {type: Company}
}
})
});
##通过下面的查询我也想查询公司收集。我怎样才能做到这一点? ##
const Query = new GraphQLObjectType({
name: "Queries",
fields: {
jobs: {
type: new GraphQLList(Job),
resolve: function(rootValue, args, info) {
let fields = {};
let fieldASTs = info.fieldASTs;
fieldASTs[0].selectionSet.selections.map(function(selection) {
fields[selection.name.value] = 1;
});
return jobsCollection.find({}, fields).toArray();
}
}
}
});
resolve(job,args,context,info){ return companyCollection.findOne({_id:job.companyId}); } 这里你没有使用rootValue作为解析函数中的一个参数。什么是rootValue? – chyangba
我刚刚将rootValue变量重命名为job,这就是它的原因。我在答案结尾处发布的链接解释了根值的来源。 – helfer