Graphql: Variables do not work

Created on 29 Jan 2019  路  1Comment  路  Source: graphql-go/graphql

I've spent significant time confirming this is an issue before posting, including reviewing all relevant issues (open and closed). When sending variables, whether using GraphiQL, Postman, or a Go GraphQL client, they will not be picked up in the resolver. However, when inlining instead, it will work:

Inline

mutation {
  create(myvar: "value") {
    id
  }
}

Variables

Variable myvar is set in GraphiQL, in POST Request body JSON, and in Go GraphQL client appropriately.

mutation($myvar: String) {
  create(myvar: $myvar) {
    id
  }
}

Go

input, ok := params.Args["myvar"]
log.Print(input, ok) // prints `<nil> false` when using variables, prints `value true` when inlined

I am using proper NewInputObject, InputObjectConfigFieldMap and InputObjectFieldConfig structs, which I can confirm because it does work when inlining. Can anyone confirm variables indeed do not work, or has anyone been able to get them to work?

Most helpful comment

Upon further investigation, the issue seemed to be not setting VariableValues on the graphql.Params type passed to graphql.Do(). Some extra work needs to be done to pull variables off of the request body and decode, but once this is done and added Resolvers will have access to params passed via variables and not just inlined.

res := graphql.Do(graphql.Params{
    Schema:         s,
    RequestString:  req.Query,
    Context:        ctx,
    VariableValues: vars, // Variables from request body need to be passed manually here
})

>All comments

Upon further investigation, the issue seemed to be not setting VariableValues on the graphql.Params type passed to graphql.Do(). Some extra work needs to be done to pull variables off of the request body and decode, but once this is done and added Resolvers will have access to params passed via variables and not just inlined.

res := graphql.Do(graphql.Params{
    Schema:         s,
    RequestString:  req.Query,
    Context:        ctx,
    VariableValues: vars, // Variables from request body need to be passed manually here
})

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yhc44 picture yhc44  路  5Comments

michelalbers picture michelalbers  路  3Comments

rubberviscous picture rubberviscous  路  4Comments

Fruchtgummi picture Fruchtgummi  路  4Comments

conord33 picture conord33  路  6Comments