在深入研究代码之前,下面是对我的问题的高级解释:
在我的
GraphQL
模式,我有两个根类型:
开发人员
和
项目
.我试图找到所有属于给定项目的开发人员。查询可能如下所示:
{
project(id:2) {
title
developers {
firstName
lastName
}
}
}
目前,我正在
null
价值观
开发人员
.
虚拟数据
const developers = [
{
id: '1',
firstName: 'Brent',
lastName: 'Journeyman',
projectIds: ['1', '2']
},
{
id: '2',
firstName: 'Laura',
lastName: 'Peterson',
projectIds: ['2']
}
]
const projects = [
{
id: '1',
title: 'Experimental Drug Bonanza',
company: 'Pfizer',
duration: 20,
},
{
id: '2',
title: 'Terrible Coffee Holiday Sale',
company: 'Starbucks',
duration: 45,
}
]
因此,布伦特在这两个项目上都工作过。劳拉从事第二个项目。我的问题在
resolve
功能在
ProjectType
. 我试过很多问题,但似乎都不管用。
项目类型
const ProjectType = new GraphQLObjectType({
name: 'Project',
fields: () => ({
id: { type: GraphQLID },
title: { type: GraphQLString },
company: { type: GraphQLString },
duration: { type: GraphQLInt },
developers: {
type: GraphQLList(DeveloperType),
resolve(parent, args) {
///////////////////////
// HERE IS THE ISSUE //
//////////////////////
return _.find(developers, { id: ? });
}
}
})
})
显影类型
const DeveloperType = new GraphQLObjectType({
name: 'Developer',
fields: () => ({
id: { type: GraphQLID },
firstName: { type: GraphQLString },
lastName: { type: GraphQLString }
})
})