我最近才刚刚开始学习GraphQL,并决定在基于反应的轮询应用程序中实现它,用户可以在该应用程序中创建和投票。 我创建了一个看起来像https://github.com/luckyrose89/Voting-App/blob/master/backend/models/poll.js的猫鼬模型。 我在编写Graphql突变时遇到将upvotes添加到民意调查选项的问题。到目前为止,我的模式如下:
const AnswerType = new GraphQLObjectType({
name: "Answer",
fields: () => ({
id: { type: GraphQLID },
option: { type: GraphQLString },
votes: { type: GraphQLInt }
})
});
const QuestionType = new GraphQLObjectType({
name: "Question",
fields: () => ({
id: { type: new GraphQLNonNull(GraphQLID) },
question: { type: GraphQLString },
answer: { type: GraphQLList(AnswerType) }
})
});
const AnswerTypeInput = new GraphQLInputObjectType({
name: "AnswerInput",
fields: () => ({
option: { type: GraphQLString },
votes: { type: GraphQLInt }
})
});
const QuestionTypeInput = new GraphQLInputObjectType({
name: "QuestionInput",
fields: () => ({
question: { type: new GraphQLNonNull(GraphQLString) },
answer: { type: new GraphQLNonNull(GraphQLList(AnswerTypeInput)) }
})
});
const Mutation = new GraphQLObjectType({
name: "Mutation",
fields: {
addPoll: {
\\\\ code here
},
deletePoll: {
\\\\\ code here
},
upvotePoll: {
type: QuestionType,
args: { id: { type: new GraphQLNonNull(GraphQLID) } },
resolve(parent, args) {}
}
}
});
因此,我已经定义了类型,可以添加和删除民意测验并访问单个民意测验(我在这里跳过了查询部分)。但是我不理解如何在不检索不必要的数据的情况下访问单个民意测验的AnswerType对象,并使用它来编写我的upVote变异。 我希望有人可以指导我