javascript – GraphQL:更新数组
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – GraphQL:更新数组,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1820字,纯文字阅读大概需要3分钟。
内容图文
![javascript – GraphQL:更新数组](/upload/InfoBanner/zyjiaocheng/789/934c095672c7489387b2c186d1ce5fb3.jpg)
我在解析器中更新数组时遇到了一些问题.我正在使用打字稿进行构建.
描述
我在pramma的datamodel.graphql中:
type Service @model {
id: ID! @unique
title: String
content: String
createdAt: DateTime!
updatedAt: DateTime!
comments: [Comment!]! // Line to be seen here
author: User!
offer: Offer
isPublished: Boolean! @default(value: "false")
type: [ServiceType!]!
}
type Comment @model {
id: ID! @unique
author: User! @relation(name: "WRITER")
service: Service!
message: String!
}
Prisma连接到GraphQl服务器,在这一个中,我定义了变异:
commentService(id: String!, comment: String!): Service!
因此,为实现给定突变的解析器的时间到了,我这样做:
async commentService(parent, {id, comment}, ctx: Context, info) {
const userId = getUserId(ctx);
const service = await ctx.db.query.service({
where: {id}
});
if (!service) {
throw new Error(`Service not found or you're not the author`)
}
const userComment = await ctx.db.mutation.createComment({
data: {
message: comment,
service: {
connect: {id}
},
author: {
connect: {id:userId}
},
}
});
return ctx.db.mutation.updateService({
where: {id},
data: {
comments: {
connect: {id: userComment.id}
}
}
})
}
问题 :
查询操场时我唯一收到的是null,而不是我给出的评论.
感谢阅读,直到目前为止.
解决方法:
你可以分享你暴露突变解析器的代码吗?如果您忘记在突变解析程序对象中包含commentService解析程序,则可能会返回null.
除此之外,我在代码中再看到一个问题.由于您在服务和注释之间存在关联,因此您可以使用单个突变来创建注释并将其添加到服务中.您不需要编写两个单独的突变来实现这一目标.您的解析器可以更改为如下所示:
async commentService(parent, {id, comment}, ctx: Context, info) {
const userId = getUserId(ctx);
return ctx.db.mutation.updateService({
where: {id},
data: {
comments: {
create: {
message: comment,
author: {
connect: {id:userId}
}
}
}
}
})
}
请注意,在执行更新之前,我还删除了查询以检查服务是否存在.原因是,updateService绑定调用将抛出错误,如果它不存在,我们不需要显式检查.
内容总结
以上是互联网集市为您收集整理的javascript – GraphQL:更新数组全部内容,希望文章能够帮你解决javascript – GraphQL:更新数组所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。