【发布时间】:2018-11-15 04:02:59
【问题描述】:
我正在尝试获取模型列表。
首先我创建了一个模型查询 API。现在我想通过提供ids 来获取列表。
我知道我可以在getModelById 上使用Promise.all 并对我再次获得的每个结果进行一些处理。
但是有没有办法重用单个模型查询 API?谢谢
const ModelType = new GraphQLObjectType({
name: 'Model',
fields: {
id: { type: GraphQLString },
// ...
}
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: () => ({
model: {
type: ModelType,
args: {
id: { type: GraphQLString }
},
resolve(parentValue, args) {
const { id } = args;
return getModelById(id).then(model => doSomeProcessing(model));
}
},
models: {
type: new GraphQLList(ModelType),
args: {
ids: { type: new GraphQLList(GraphQLString) }
},
resolve(parentValue, args) {
const { ids } = args;
// here I know I can use Promise.all on getModelById and do my doSomeProcessing again for each result I got,
// but is there a way to reuse the model query above? Thanks
}
}
})
});
【问题讨论】:
标签: javascript node.js graphql