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

Generated code does not compile with input types as mutation arguments #92

Closed
jonstaryuk opened this issue Apr 25, 2018 · 1 comment
Closed

Comments

@jonstaryuk
Copy link
Contributor

jonstaryuk commented Apr 25, 2018

First, thanks for all your work on this. It's a very helpful package.

For some context, I originally wanted to do this:

schema.graphql

schema {
    query: Query
    mutation: Mutation
}

type Query {}

type Mutation {
    addBookmark(b: Bookmarkable!): Boolean!
}

type Item {}

union Bookmarkable = Item

But it doesn't compile:

$ gqlgen
$ go build
./generated.go:117:16: (&arg0).UnmarshalGQL undefined (type *Bookmarkable is pointer to interface, not interface)

models_gen.go

// This file was generated by github.com/vektah/gqlgen, DO NOT EDIT

package tmp

type Bookmarkable interface{}
type Item struct {
}

generated.go

// ...
func (ec *executionContext) _Mutation_addBookmark(ctx context.Context, field graphql.CollectedField) graphql.Marshaler {
	args := map[string]interface{}{}
	var arg0 Bookmarkable
	if tmp, ok := field.Args["b"]; ok {
		var err error
		err = (&arg0).UnmarshalGQL(tmp)  // <-- the problem
		if err != nil {
			ec.Error(err)
			return graphql.Null
		}
	}
// ...

There are lengthy discussions about this at graphql/graphql-js#207 and graphql/graphql-spec#395. It sounds like there's no official solution yet. So I tried this:

schema {
    query: Query
    mutation: Mutation
}

type Query {}

type Mutation {
    addBookmark(b: BookmarkableInput!): Boolean!
}

type Item {}

input BookmarkableInput {
    item: Item
}
$ gqlgen
unable to gofmt: generated.go:821:9: expected selector or type assertion, found '='

It creates a malformed generated.go

//...
	func UnmarshalBookmarkableInput(v interface{}) (BookmarkableInput, error) {
		var it BookmarkableInput

		for k, v := range v.(map[string]interface{}) {
			switch k {
			case "item":
				var err error
				var ptr1 Item
			if v != nil {
				err = (&ptr1).UnmarshalGQL(v)
				it. = &ptr1} // <-- the problem
		
				if err != nil {
					return it, err
				}
			}
		}

		return it, nil
	}
// ...
@vektah
Copy link
Collaborator

vektah commented Apr 26, 2018

Interesting, It wouldn't be too hard to add once that RFC lands.

As for the second issue, you cant reference types from input types.

I will tighten up the error handling for both.

cgxxv pushed a commit to cgxxv/gqlgen that referenced this issue Mar 25, 2022
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

2 participants