Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutation with variables not working #85

Closed
rhymes opened this issue Apr 10, 2018 · 5 comments
Closed

Mutation with variables not working #85

rhymes opened this issue Apr 10, 2018 · 5 comments

Comments

@rhymes
Copy link

rhymes commented Apr 10, 2018

Expected Behaviour

I would expect the following mutation to be working:

{
  "query": "mutation createApp($app: AppInput!) {\\n  createApp(app: $app) {\\n}\\n}\\n",
  "variables": {
    "appID": "foobar"
  }
}

This is the body of the JSON mutation sent to gqlgen.

Unfortunately the actual "model" in the resolver has that field empty.

If the mutation is sent as:

mutation createApp {
  createApp(app: {appID: "foobar"}) {
  }
}

then it works.

I debugged a bit and I noticed that in the first case the variables are in the context of the mutation:

func (r *Resolver) Mutation_createApp(ctx context.Context, app models.App) (*models.App, error) { // nolint: golint
	reqContext := graphql.GetRequestContext(ctx)
	fmt.Println(reqContext.Variables)
	runtime.Breakpoint()

       # .....
}

this prints all the variables I sent using the client but the actual model struct is empty. With the second method there are no variables and the model struct is filled with the values.

Am I doing something wrong?

Actual Behavior

I would expect the mutation to receive the struct filled with the variables sent on the wire. In the meantime I'm filling the model struct manually from the request context.

Minimal graphql.schema and models to reproduce

type Mutation {
    createApp(app: AppInput!): App
}

type App {
    appID: ID!
}

input AppInput {
    appID: ID!
}

and the model:

type App struct {
	AppID                     string    `sql:",notnull" json:"appID"`
}
@vektah
Copy link
Collaborator

vektah commented Apr 19, 2018

Your query is using $app, but your passing on appID as the variable

Try "app": {"appID":"foobar"}

@vektah
Copy link
Collaborator

vektah commented Apr 19, 2018

I wonder if adding an error for unused variables would break people's code.

@Teddy-Schmitz
Copy link

I dont think throwing an error for unused variables here is the way to go, unless its part of the graphql spec. This is more like user error.

@vektah
Copy link
Collaborator

vektah commented Apr 19, 2018

I don't see anything in the spec, but maybe it should be.

There is a bit of chatter in graphql/graphql-spec#235 regarding input field properties which is pretty close. Variables don't have the same issues regarding removing fields.

Either way it would probably need to be gated behind a strict mode flag.

@rhymes
Copy link
Author

rhymes commented Apr 19, 2018

It works, thank you! I had to expand the uint32 unmarshaler because in interface{} ints are float64 but it works!

@rhymes rhymes closed this as completed Apr 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants