简体   繁体   中英

Null value in bulk insert Hasura GraphQL

I am trying to do a multiple insert using a REST API exposed by Hasura (written in GraphQL). This is my mutation:

mutation insertMultipleMeasurements2($payload: [measurements_insert_input!]!) {
  insert_measurements(objects: $payload) {
    returning {
      time
    } 
  }
}

and this is the definition of measurements_insert_input in the Actions tab:

input measurements_insert_input {
  device: String!
  value: numeric!
  variable: String!
}

(which btw is the only way I found to insert an object of numeric type in a bulk insert).

When I try to insert something like:

[
    {
        "device": "TEST",
        "value": 5630,
        "variable": "Length"
    },
        {
        "device": "TEST",
        "value": 5631,
        "variable": "Length"
    }
]

I get:

{
    "path": "$",
    "error": "expecting a value for non-nullable variable: \"payload\"",
    "code": "validation-failed"
}

I tried many different ways to rewrite my mutation but looks like none worked. Thanks in advance

You've most likely forgotten to wrap your objects with variables , like so:

yourMutation({
      variables: { objects, on_conflict }
})

It's not straightforward if you rely on TS IntelliSense, better check the doc:)

Based on the error message

"expecting a value for non-nullable variable: "payload""

you should be passing the array via an object

{
  payload: [
    {
    "device": "TEST",
    "value": 5630,
    "variable": "Length"
    },
    {
    "device": "TEST",
    "value": 5631,
    "variable": "Length"
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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