[GH-ISSUE #49] mutation - variable with null value #28

Closed
opened 2026-03-03 00:02:51 +03:00 by kerem · 1 comment
Owner

Originally created by @honzahk on GitHub (Sep 16, 2020).
Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/49

consider this code server-side

type Mutation {
  updateMessage(id: Number!, text: String): Boolean
}

and this code client-side

const mutation = gql.mutation({
    operation: 'updateMessage',
    variables: {
        id: {value: 1, required: true},
        text: null
  }
})

now if you print out the mutation, text variable has Object type

console.log(mutation);

//query
mutation ($id: Int!, $text: Object) {
  updateMessage (id: $id, text: $text) 
}

//variables
{
    id: 1,
    text: null
}

the reason is obvious, gql-query-builder cannot determine the type when just null is provided - it might be any type.


if you send this mutation to the server-side, in response you get error

message: "Unknown type "Object"."

server-side does not know type Object; moreover the type coming from client-side (infered by gql-query-builder) has to correspond with the type defined on server-side (in graphql schema)


by now, i use workaround by specifing the variable type explicitly

const mutation = gql.mutation({
    operation: 'updateMessage',
    variables: {
        id: {value: 1, required: true},
        text: {value: null, type: "String"}
  }
})

is this workaround alright or is there a better way?
i cannot think of any other ways, as it is impossible to know the intended type when just null is provided.

thanks in advance!

Originally created by @honzahk on GitHub (Sep 16, 2020). Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/49 consider this code server-side ``` type Mutation { updateMessage(id: Number!, text: String): Boolean } ``` and this code client-side ``` const mutation = gql.mutation({ operation: 'updateMessage', variables: { id: {value: 1, required: true}, text: null } }) ``` --- now if you print out the mutation, `text` variable has `Object` type ``` console.log(mutation); //query mutation ($id: Int!, $text: Object) { updateMessage (id: $id, text: $text) } //variables { id: 1, text: null } ``` the reason is obvious, `gql-query-builder` cannot determine the type when just `null` is provided - it might be any type. --- if you send this mutation to the server-side, in response you get error ``` message: "Unknown type "Object"." ``` server-side does not know type `Object`; moreover the type coming from client-side (infered by `gql-query-builder`) has to correspond with the type defined on server-side (in graphql schema) --- by now, i use workaround by specifing the variable type explicitly ``` const mutation = gql.mutation({ operation: 'updateMessage', variables: { id: {value: 1, required: true}, text: {value: null, type: "String"} } }) ``` **is this workaround alright or is there a better way?** i cannot think of any other ways, as it is impossible to know the intended type when just `null` is provided. thanks in advance!
kerem 2026-03-03 00:02:51 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@Devorein commented on GitHub (Sep 24, 2020):

Yes that is the only way to accurately say what the type of that variable is in a query

<!-- gh-comment-id:698343676 --> @Devorein commented on GitHub (Sep 24, 2020): Yes that is the only way to accurately say what the type of that variable is in a query
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/gql-query-builder#28
No description provided.