question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Insert multiple objects at once with variables

See original GitHub issue

Hey, thx for the awesome project!

I’m a hasura / graphql beginner and like to ask, if it is possible to insert multiple objects at once with graphql-request using variables and how it should look like.

I was able to successful insert a single entity like that:

  import { request } from 'graphql-request'
  const variables = { id: 123, name: "test" }
  const gql = `mutation insert_test(
    $id: Int!
    $name: String! 
  ) {
    insert_test(
      objects: {
        id: $id,
        name: $name
      }
    ) {
      returning {
        id
      }
    }
  }`
  const res = await request(HASURA_URL_GRAPHQL, gql, variables)

But when changing the variables to an array i got an error.

  import { request } from 'graphql-request'
  const variables = [{ id: 123, name: "test" }, { id: 456, name: "test1" }]
  const gql = `mutation insert_test(
    $id: Int!
    $name: String! 
  ) {
    insert_test(
      objects: {
        id: $id,
        name: $name
      }
    ) {
      returning {
        id
      }
    }
  }`
  const res = await request(HASURA_URL_GRAPHQL, gql, variables)

Can someone give me a hint into the right direction here? Thx in advance!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
coco98commented, Oct 28, 2019

@divramod You can change your variables input to be the object type and it should be fine after that:

 import { request } from 'graphql-request'
  const variables = [{ id: 123, name: "test" }, { id: 456, name: "test1" }]
  const gql = `mutation insert_test($objects: [test_insert_input!]!)
  ) {
    insert_test(
      objects: $objects
    ) {
      returning {
        id
      }
    }
  }`
  const res = await request(HASURA_URL_GRAPHQL, gql, variables)

To get the exact type name there (test_insert_input) you can use the “docs” tab on the hasura graphiql console 😃

4reactions
iamsahucommented, Jun 14, 2020

Sure.

Here is a piece of code that let me achieve it.

The query: const INSERT_TAGS = gql mutation MyMutation($objects: [tag_insert_input!]!) { insert_tag(objects: $objects) { returning { id name user_id } } } ;

The code to run the query: function InsertNewTags(tags, curator_id) { return client .mutate({ mutation: INSERT_TAGS, variables: { objects: tags, }, }) .catch((error) => { console.log(error); }); }

In the tags parameter of the above function an array of objects is passed. tags = [{ name: values.selTags[a], user_id: values.curator_id },{ name: values.selTags[a], user_id: values.curator_id }]

I hope this helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

insert multiple object with same key-value in array based on ...
You should declare the data variable inside the loop because otherwise you are always changing its value at each iteration and that object...
Read more >
Insert multiple objects's data in one controller
Insert multiple objects's data in one controller. I created a visualforce page in which it has form for create new contact,
Read more >
Is it possible to insert multiple objects at once in Excel 2007?
I've used macros to do that for inserting hyperlinks to docs, but can't figure out one for this or find an answer to...
Read more >
Insert a multiple-selection list box - Microsoft Support
Depending on how you design the multiple-selection list box, users may also be able to type their own list item next to one...
Read more >
Flow Basics | Bulkify Flows to Insert/Create Multiple Records
The tricky part is Creating/Inserting Multiple Records for a particular object at once. At least, it proved a bit tricky for me when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found