Skip to content

`post_graphql` needs to serialize variables correctly

In our graphql helpers, we provide a function post_graphql to send GraphQL queries to the correct API endpoint. Unfortunately, it is difficult to use correctly at the moment, since it does not serialize the variables correctly.

Example Query

If I have the following query:

query($path: ID!, $n: Int) {
  project(fullPath: $path) {
    issues(first: $n) { nodes { id } }
  }
}

Then I would expect that the following call to post_graphql would return 2 issues:

post_graphql(q, current_user: current_user, variables: { path: p.full_path, n: 2 })

Instead I get:

{
  "errors":[
    { "message":"Variable $n of type Int was provided invalid value"
    , "locations": [{"line":1,"column":19}]
    , "extensions": {"value":"2"
                    ,"problems":[{"path":[],"explanation":"Could not coerce value \"2\" to Int"}]
                    }
    }
  ]
}

However if I instead call this as:

post_graphql(q, current_user: current_user, variables: { path: p.full_path, n: 2 }.to_json)

Then I get the expected correct result.

Suggested action:

Call #to_json on the variables.