6

I have the following schema:

type Post {
  id: ID!
  text: String
}

I am using autogenerated mutations from neo4j-graphql.js, so I have access to the following mutation:

UpdatePost(
id: ID!
text: String
): Post

The issue:

When I'm using the following query:

mutation update($id: String, $text: String) {
  UpdatePost(id: $id, text: $text) {
    id
    text
  }
}

With the following parameters:

{
  "id": "a19289b3-a191-46e2-9912-5a3d1b067cb2",
  "text": "text"
}

I get the following error:

{
  "error": {
    "errors": [
      {
        "message": "Variable \"$id\" of type \"String\" used in position expecting type \"ID!\".",
        "locations": [
          {
            "line": 1,
            "column": 17
          },
          {
            "line": 2,
            "column": 18
          }
        ],
        "extensions": {
          "code": "GRAPHQL_VALIDATION_FAILED"
        }
      }
    ]
  }
}

Is there a way to convert my string ID to the actual ID type? Or circumvent this error altogether?

3kt
  • 2,543
  • 1
  • 17
  • 29

3 Answers3

6

The error you're seeing is related to the type of your variables as specified in your query's variable definitions. It has nothing to do with the values of the variables (which appear to be correct). Based on the error message, the type of your $id variable is String, not ID as shown in the query you pasted.

Regardless, because the type of the id argument (where the $id variable is being used) is ID!, not ID, then your variable's type should also be ID!.

The ! indicates a non-null (required) type. In other words, the id argument must always be provided and it cannot be given the value null. Any variable passed the argument must also be non-null. If the variable's type is ID instead of ID!, we're telling GraphQL the variable might be omitted or have a null value, which would be incompatible with the argument where it's being used.

Note that the opposite is not true: if the argument's type was ID, then either an ID or ID! variable would be valid.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • 1
    I understand that, but how do you make such string an ID then? I use the exact value returned when querying for the field. – 3kt Mar 11 '20 at 13:14
  • You don't "convert" a string to an ID. An ID scalar simply accepts strings (and integers) as valid values as outlined in the [spec](http://spec.graphql.org/June2018/#sec-ID). – Daniel Rearden Mar 11 '20 at 13:49
  • If you are still seeing an error after fixing your variable definitions, recheck that it is in fact the same error. The error you pasted doesn't match up to the query you pasted. – Daniel Rearden Mar 11 '20 at 13:55
  • 1
    The error message does. I've purged irrelevant fields from my schema. > If you are still seeing an error after fixing your variable definitions That is exactly the point of my question... How do I fix my variable definition? The value in my question is exactly what is returned when querying for object I want to modify. – 3kt Mar 11 '20 at 15:49
  • And when I'm not using a parametrized query, this works without issue – 3kt Mar 11 '20 at 15:50
  • Like I said, it needs to be `$id: ID!` instead of `$id: ID`. But that's not relevant to the error you're seeing. The error implies that whatever query is actually hitting your server has this as a definition: `$id: String`. – Daniel Rearden Mar 11 '20 at 15:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209465/discussion-between-daniel-rearden-and-3kt). – Daniel Rearden Mar 11 '20 at 16:00
  • yup just convert String -> ID! – Prince Agrawal Jun 26 '22 at 22:55
2

For anyone else encountering this issue, in the mutation definition you have:

mutation update($id: String, $text: String) {
  UpdatePost(id: $id, text: $text) {
    id
    text
  }
}

where you're explicitly saying the $id is a String, you need to change it to ID like so:

mutation update($id: ID, $text: String) {
  UpdatePost(id: $id, text: $text) {
    id
    text
  }
}

This is why you're seeing the error, because your query to update it's saying explicitly that ID is a type String hence the error.

0

In case someone else is looking for an answer, here's few example how to use ID variable in C#:

var request = new GraphQLRequest
            {
                Query = @"
                query getToken($tokenId: ID!){{
                    tokens(where: {{ id: $tokenId}} orderBy: decimals, orderDirection: desc) {{
                        id
                        symbol
                        name
                      }}
                }}", Variables = new
                {
                    tokenId = "<stringValue>"
                }
            };

And for using lists:

query getToken($tokenIds: [ID!]){{
                    tokens(where: {{ id_in: $tokenIds}} orderBy: decimals, orderDirection: desc) {{
                        id
                        symbol
                        name
                      }}
                }}", Variables = new 
                {
                    tokenIds = new ArrayList(<yourStringArrayListValue>)
                }
Dharman
  • 30,962
  • 25
  • 85
  • 135
ecif
  • 311
  • 2
  • 12