Coh*_*ent 1 apollo graphql apollo-server
如何在graphQL阿波罗语法中添加字段解析器?使用graphql语法,如果我的讲座有问题,我可以这样做:
const LectureType = new GraphQLObjectType({
name: 'LectureType',
fields: () => ({
id: { type: GraphQLID },
questions: {
type: new GraphQLList(QuestionType),
resolve: ({ _id }, args, { models }) => models.Lecture.findQuestions(_id),
},
}),
});
Run Code Online (Sandbox Code Playgroud)
使用apollo graphQL的等效语法是什么?我认为我可以使用此解析器进行此类型定义:
type QuestionType {
id: String,
}
type LectureType {
id: String,
questions: [QuestionType],
}
const getOne = async ({ args, context }) => {
const { lecture, question } = constructIds(args.id);
const oneLecture = await model.get({ type: process.env.lecture, id: lecture });
oneLecture.questions = await model
.query('type')
.eq(process.env.question)
.where('id')
.beginsWith(lecture)
.exec();
return oneLecture;
};
Run Code Online (Sandbox Code Playgroud)
问题是我是在每个解析器而不是架构级别上手动填充问题。这意味着我的查询将仅填充我指定的固定深度,而不是基于请求的实际查询返回参数。(我知道这里没有1:1的基础,因为我从mongo切换到dynamo,但看来此解析器部分应该是独立的。)
如果以编程方式定义的resolve函数按预期工作,则可以在解析器对象中按原样使用它:
const typeDefs = `
type QuestionType {
id: String,
}
type LectureType {
id: String,
questions: [QuestionType],
}
# And the rest of your schema...`
const resolvers = {
LectureType: {
questions: ({ _id }, args, { models }) => {
return models.Lecture.findQuestions(_id)
}
},
Query: {
// your queries...
}
// Mutations or other types you need field resolvers for
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2041 次 |
| 最近记录: |