简体   繁体   English

使用Graphql突变更新Postgres表中的列值

[英]Update a column value in a Postgres table using Graphql mutation

Basic info : using Postgres in the backend and GraphQL in the front end using Postgraphile. Basic info :使用Postgraphile在后端使用Postgres,在前端使用GraphQL。

Need : Use a GraphQL mutation to update a row in the Postgres DB. Need :使用GraphQL突变来更新Postgres DB中的一行。

Code : Suppose I have a library_account schema which has book table in Postgres which has fields like id, title, borrower_id and is_available. Code :假设我有一个library_account模式,该模式在Postgres中具有book表,该表具有id,title,rower_id和is_available之类的字段。

Scenario : It is getting borrowed for the very first time. Scenario :这是第一次借用。

Proposed flow : make a GraphQL mutation to update the Postgres table with the borrower_id. Proposed flow :进行GraphQL更改,以使用借款人ID更新Postgres表。

Code I am currently using: 我当前正在使用的代码:

mutation BookUpdates(
  $title: String,
  $borrower_id: Int!, #not really an Int but for the sake of ease.
  $author_id: Int!,
  $isle_id: Int!,
  $is_available: Boolean
) {
  updateBookByAuthorIdAndIsleId(input: {
    isle_id: $isle_id,
    is_available: $is_available,
    borrower_id: $borrower_id,
    author_id: $author_id
}) {
  bookEdge {
    node {
      author_id
    }
  }
}

Here, I am getting an error for borrower_id which says that borrower_id is not defined by type updateBookByAuthorIdAndIsleId . 在这里,我得到了一个错误borrower_id它说,borrower_id不受类型定义的updateBookByAuthorIdAndIsleId

Any suggestions?? 有什么建议么??

When constructing queries and mutations in GraphQL it's often wise to use a client such as GraphiQL which will help you by providing the documentation, giving you auto-complete capabilities, and highlighting where errors have occurred. 在GraphQL中构造查询和变异时,通常明智的做法是使用诸如GraphiQL之类的客户端,该客户端将通过提供文档,提供自动完成功能并突出显示发生错误的地方来为您提供帮助。 PostGraphile has GraphiQL built in; PostGraphile具有内置的GraphiQL; it's enabled by default on the CLI but if you're using it in library mode you must pass graphiql: true to enable it. 默认情况下,它在CLI上是启用的,但是如果您以库模式使用它,则必须传递graphiql: true才能启用它。 Either way I recommend you use the --enhance-graphiql / enhanceGraphiql: true flags. 无论哪种方式,我都建议您使用--enhance-graphiql / enhanceGraphiql: true标志。 If you take your mutation and put it into GraphiQL it should tell you where it went wrong, and may even suggest how to fix it! 如果您将突变放入GraphiQL中,它应该告诉您哪里出错了,甚至可能建议如何解决!

It looks to me like your mutation is slightly the wrong shape. 在我看来,您的突变形状略有错误。 PostGraphile follows the Relay Input Object Mutations Specification, which means we nest all of the mutation inputs under the input argument, as you have done. PostGraphile遵循中继输入对象变异规范,这意味着我们像您所做的那样,将所有变异输入嵌套在输入参数下。 However, we also group together the details about the record so that it's easier to separate the "what" from the "how" when doing updates - eg "what": authorId: 27, isleId: 93 , "how": patch: {text: $text} - this also allows you to update the keys (eg if you wanted to change the isleId) which wouldn't be possible if all the columns were in together. 但是,我们还将记录的详细信息归为一组,以便在进行更新时更容易将“内容”与“方式”分开-例如,“内容”: authorId: 27, isleId: 93 ;“ how”: patch: {text: $text} -这还允许您更新键(例如,如果您要更改isleId),如果所有列都放在一起,则不可能。 This is the part you're missing, I believe. 我相信,这是您缺少的部分。

I suspect that your mutation should look more like: 我怀疑您的突变看起来应该更像:

mutation BookUpdates(
  $borrower_id: Int!, #not really an Int but for the sake of ease.
  $author_id: Int!,
  $isle_id: Int!,
  $is_available: Boolean
) {
  updateBookByAuthorIdAndIsleId(input: {
    isleId: $isle_id,
    authorId: $author_id
    bookPatch: {
      isAvailable: $is_available,
      borrowerId: $borrower_id,
    }
}) {
  bookEdge {
    node {
      authorId
    }
  }
}

I've also camelCase'd your field names, but if you've loaded a custom inflector this might not be necessary/desirable. 我也骆驼了您的字段名称,但如果您已加载一个自定义的偏转器,这可能不是必要/不希望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM