How to set null to field in graphql?
Asked Answered
G

2

6

We use graphql on our project and use graphql-java 2.3.0 (we plan to update but it is not possible at the moment).

I'm trying to perform such mutation:

  mutation {
    UPDATE_User(

      id:3,
      type: null
    ) {id}
  }

Response:

{
  "errors": [
    {
      "validationErrorType": "WrongType",
      "message": "Validation error of type WrongType: argument value EnumValue{name='null'} has wrong type",
      "locations": [
        {
          "line": 5,
          "column": 7
        }
      ],
      "errorType": "ValidationError"
    }
  ],
  "data": null
}
Gittle answered 28/7, 2017 at 13:45 Comment(0)
E
3

Null keyword is a relatively new addition to the GraphQL spec, and the graphql-java version you're using doesn't yet have it implemented. In fact, even 3.0.0 still doesn't have it. The soon-to-be-released 4.0 will have support for the null keyword.
And the reason it is treated as an enum is because unquoted strings are normally enums, null being the only exception. This is also explained in this issue.

Environmentalist answered 10/8, 2017 at 21:17 Comment(2)
I see. Thank you. But what is the reccomended workaround? I don't think that null was not used by graphql users until now.Gittle
@Gittle Null was only added months ago. Prior to that, all you could do was not send type at all, but then you can't distinguish update to null and leave unchanged cases. If this is important to you, you'll have to set a special marker value to type e.g. type: "NONE" and handle that appropriately in the backend.Environmentalist
D
0

In your schema, is 'type' parameter required ?

If not then simply omit it ! On your backend when you will retrieve the value of typethen it will be null

Deed answered 1/2, 2018 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.