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!
@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 :)
@divramod I'm closing this issue. Feel free to re-open if you'd like to add anything to it 🙂
Hello @marionschleifer.
I am following your guidance to perform the following insert
mutation insertUserTags ($ user_tags_insert_input: [user_tags_insert_input!]!) {
insert_user_tags (objects: [$ user_tags_insert_input]) {
affected_rows
}
}
with the variable
{"user_tags_insert_input":
[{"name": "Study", "userId": "a"}, {"name": "Study", "userId": "a"}]
}
but I get the error
variable user_tags_insert_input of type [user_tags_insert_input!]! is used in position expecting user_tags_insert_input! "
Can you help me, please?
Hi @evertonfoz
You have to remove array brackets around $user_tags_insert_input.
If a variable is declared as an array then brackets aren't required while passing the value to objects
mutation insertUserTags ($ user_tags_insert_input: [user_tags_insert_input!]!)
Hi @hafiztahajamil
Thanks for the feedback. When I use
mutation insert User Tags ($ objects: user tags_insert_input) {insert_user_tags (objects: $ objects) { affected_rows } }
with the variables
{ "objects": [{ "name": "Study", "userId": "a"}, {"name": "Study", "userId": "a"}]}
I get the error
{ "errors": [ { "extensions": { "path": "$ .variableValues.objects", "code": "validation-failed" }, "message": "expecting a JSON object" } ] }
And already in the fields where I inform the mutation and variables, there is an error about sending an unexpected array in the mutation.
Hi,
You shouldn't change this: mutation insert User Tags ($ objects: user
tags_insert_input)
This should remain: mutation insert User Tags ($ objects: [user
tags_insert_input!]!)
Final Mutation:
mutation insert User Tags ($ objects: [user tags_insert_input!]!) {
insert_user_tags (objects: $objects) { affected_rows } }
Let me know if it works for you :)
On Mon, Apr 27, 2020 at 2:37 PM Everton Coimbra de Araújo <
[email protected]> wrote:
Hi @hafiztahajamil https://github.com/hafiztahajamil
Thanks for the feedback. When I use
mutation insert User Tags ($ objects: user tags_insert_input) {
insert_user_tags (objects: $ objects) { affected_rows } }with the variables
{ "objects": [{ "name": "Study", "userId": "a"},
{"name": "Study", "userId": "a"} ] }I get the error
{ "errors": [ { "extensions": { "path": "$
.variableValues.objects", "code": "validation-failed" },
"message": "expecting a JSON object" } ] }And already in the fields where I inform the mutation and variables, there
is an error about sending an unexpected array in the mutation.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/hasura/graphql-engine/issues/3254#issuecomment-619860576,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AC2ASTOQYU24FPJAQCO76HTROVG7DANCNFSM4JFQHP6Q
.
It Works. Thanks a lot :-)
👍
On Mon, Apr 27, 2020 at 4:49 PM Everton Coimbra de Araújo <
[email protected]> wrote:
It Works. Thanks a lot :-)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/hasura/graphql-engine/issues/3254#issuecomment-619932827,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AC2ASTIU775QPWHCEIQGV4LROVWNNANCNFSM4JFQHP6Q
.
I am getting the following error
Error: GraphQL error: 0 is not valid GraphQL name
I am passing the following:
[{tag: "test1", curator: "26b4e98c-b5dc-4810-97b9-909ddc74c4f0"},{tag: "test2", curator: "26b4e98c-b5dc-4810-97b9-909ddc74c4f0"}]
Here is my query:
export const INSERT_TAGS2 = gql`
mutation MyMutation($objects: [tag_insert_input!]!) {
insert_tag(objects: $objects) {
returning {
name
}
}
}
`
Here is how I am trying to call it:
const [createTag,{errorTag}] = useMutation(INSERT_TAGS2,{
variables:[{tag: "test1", curator: "26b4e98c-b5dc-4810-97b9-909ddc74c4f0"},{tag: "test2", curator: "26b4e98c-b5dc-4810-97b9-909ddc74c4f0"}],
onError:(error,variables)=>{
console.log(error)
// console.log(variables)
}
I am getting same error as @iamsahu and help would be greately appreciated.
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.
Thank you.. Right. I figured it out finally. I was inputting into variables directly an array of my stuff to insert; while I should have give it an object with key "objects" and value that array. Like:
insertCompanyCategories({
variables: {
objects: [
{
category_id: "9d854204-ace4-11ea-8e47-02420aff0066",
company_id: "5b675516-8baa-11ea-97ad-02420aff0004",
},
{
category_id: "d287c566-aafb-11ea-b094-02420aff0004",
company_id: "5b675516-8baa-11ea-97ad-02420aff0004",
},
],
},
});
@iamsahu Awesome, it works for me. @justdvl thanks you too.
Most helpful comment
@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 :)