【问题标题】:Prisma GraphQl how to correctly update fieldsPrisma GraphQl 如何正确更新字段
【发布时间】:2020-09-23 11:19:34
【问题描述】:

我第一次使用 GraphQl 和 Prisma,我试图在我的解析器突变文件中创建一个更新功能,但我一直在努力解决它。我做了一些研究,但我发现的东西不适用于我的代码。我总是以错误告终。有人可以帮我解决这个问题吗?

架构:

type Service {
    id: ID!
    createdAt: DateTime! 
    updatedAt: DateTime! 
    name: String!
    cost: Float!
    description:String
    hours: Int!
    minutes: Int!
    postedBy: User!
}

type Mutation {
    postService(name: String!, cost:Float!,description:String!,hours: Int!, minutes: Int! ): Service!
    deleteService(id: ID!): Service!
    updateService(name: String!, cost:Float!,description:String!,hours: Int!, minutes: Int!): Service!

}

变异解析器:

function updateService(parent, args, context, info) {
    return context.prisma.mutation.updateService({
        cost: args.cost,
        name: args.name,
        description: args.description,
        hours: args.hours,
        minutes: args.minutes,
        postedBy: { connect: { id: userId } },
        where: {
            id: args.where.id
        }
    }, info)
}

在 prisma 游乐场中查询:

mutation{ 
updateService( 
   name: "Update test"
    where:{
    id :  "ckaz82x01aqq50a42fkrz3uxu"
        }
){id
 name 
 hours 
 minutes}}

【问题讨论】:

    标签: sql-update prisma mutation prisma-graphql


    【解决方案1】:

    在您发布的上述突变中,您只传递了name,而不是所有变量。

    在下面的架构中,costdescriptionhoursminutes 是必需的。

    updateService(name: String!, cost:Float!,description:String!,hours: Int!, minutes: Int!): Service!
    

    您能检查一下这是否是您收到的错误吗?如果是这样,请传递所有参数并再次运行突变。

    【讨论】:

    • 是的,我更改了它,但我收到此错误"message": "Unknown argument \"where\" on field \"updateService\" of type \"Mutation\"."
    • 突变updateService(name: String!, cost:Float!,description:String!,hours: Int!, minutes: Int!): Service! 不包含任何where 变量,这就是错误的原因。您还必须将 where 添加到 schema.graphql 中的突变中。
    • 嗨 Ryan,我已经按照您的建议将其中的位置放在了 updateService(name: String, cost:Float,description:String,hours: Int, minutes: Int, where:ID!): Service! 但现在我收到了这个错误 --> "message": "Cannot read property 'updateService' of undefined", 但我不明白这个错误。感谢您的回复
    猜你喜欢
    • 2021-04-16
    • 2019-03-05
    • 2021-08-22
    • 2022-09-24
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2011-12-21
    相关资源
    最近更新 更多