[Question] Insert multiple objects at once with variables
See original GitHub issueHey, 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:
- Created 4 years ago
- Reactions:2
- Comments:14 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@divramod You can change your variables input to be the object type and it should be fine after that:
To get the exact type name there (test_insert_input) you can use the “docs” tab on the hasura graphiql console 😃
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.