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 doesn't compile due to missing type conversions #12

Closed
jonstaryuk opened this issue Feb 18, 2018 · 2 comments
Closed

Generated code doesn't compile due to missing type conversions #12

jonstaryuk opened this issue Feb 18, 2018 · 2 comments

Comments

@jonstaryuk
Copy link
Contributor

I'm getting a compiler error when gqlgen creates resolvers based on a field that has a wrapped builtin type. For example:

schema.graphql

schema {
    query: Query
}

type Query {
    foo: Foo
}

type Foo {
    id: String!
}

model.go

package tmp

type Identifier string

type Foo struct {
	ID Identifier
}

types.json

{
    "Foo": "tmp.Foo"
}
$ gqlgen -out gen.go
$ go build
# tmp
./gen.go:84:41: cannot use res (type Identifier) as type string in argument to graphql.MarshalString

Because the generated function uses MarshalString(res) instead of MarshalString(string(res)):

// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) _foo(sel []query.Selection, it *Foo) graphql.Marshaler {
	fields := graphql.CollectFields(ec.doc, sel, fooImplementors, ec.variables)
	out := graphql.NewOrderedMap(len(fields))
	for i, field := range fields {
		out.Keys[i] = field.Alias
		out.Values[i] = graphql.Null

		switch field.Name {
		case "__typename":
			out.Values[i] = graphql.MarshalString("Foo")
		case "id":
			badArgs := false
			if badArgs {
				continue
			}
			res := it.ID

			out.Values[i] = graphql.MarshalString(res) // <-- invalid call
		default:
			panic("unknown field " + strconv.Quote(field.Name))
		}
	}

	return out
}
@vektah
Copy link
Collaborator

vektah commented Feb 18, 2018

Interesting, I wasn't expecting it to be used quite like that. It probably should work like that, I'll take a look this afternoon.

In the mean time, you can add the (un)marshalGQL methods to your user type.

eg: https://github.com/vektah/gqlgen/blob/master/example/scalars/model.go#L21-L52

@jonstaryuk
Copy link
Contributor Author

Ah, I hadn't noticed that workaround before, thanks!

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