我如何 return 来自 Graphql 的多个集合的数据?
How can I return the data from multiple collections from Graphql?
如何 return 来自 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();
}
}
}
});
GraphQL 中的架构和查询不关心您的数据是在一个、两个还是十个集合中。您甚至可以在不同的数据库中的许多不同的服务器上拥有数据。 GraphQL 服务器通过遵循您在架构中定义的关系进行连接(即,将来自不同集合的数据组合起来),然后 运行 响应中每个字段的所谓解析函数以获取实际数据。
因此您的查询将如下所示:
query {
jobs {
_id
company {
name
}
}
}
您已经有了工作的解析函数,现在您只需为公司定义另一个函数。大概您的工作集合在其文档中包含公司(和名称),或者它包含公司的 _id,因此您在公司解析功能中所要做的就是这样:
resolve(job, args, context, info){
return companyCollection.findOne({ _id: job.companyId });
}
我写了一篇更长的文章 post 更详细地解释了 GraphQL 的执行。你可以找到它 here.
如何 return 来自 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();
}
}
}
});
GraphQL 中的架构和查询不关心您的数据是在一个、两个还是十个集合中。您甚至可以在不同的数据库中的许多不同的服务器上拥有数据。 GraphQL 服务器通过遵循您在架构中定义的关系进行连接(即,将来自不同集合的数据组合起来),然后 运行 响应中每个字段的所谓解析函数以获取实际数据。
因此您的查询将如下所示:
query {
jobs {
_id
company {
name
}
}
}
您已经有了工作的解析函数,现在您只需为公司定义另一个函数。大概您的工作集合在其文档中包含公司(和名称),或者它包含公司的 _id,因此您在公司解析功能中所要做的就是这样:
resolve(job, args, context, info){
return companyCollection.findOne({ _id: job.companyId });
}
我写了一篇更长的文章 post 更详细地解释了 GraphQL 的执行。你可以找到它 here.