Gqlgen: [0.8.1] Nullable union fields with resolvers generate methods expecting to return a pointer to an interface

Created on 7 Mar 2019  路  5Comments  路  Source: 99designs/gqlgen

Expected Behaviour

A schema of:

type Foo {
  bar: String
}

union Bar = Foo

type Query {
  bar: Bar
}

With:

models:
  Baz:
    fields:
      foo:
        resolver: true

Will generate a resolver with signature:

func (r *queryResolver) Bar(ctx context.Context) (Bar, error) {
        panic("not implemented")
}

Actual Behavior

A resolver with signature:

func (r *queryResolver) Bar(ctx context.Context) (*Bar, error) {
        panic("not implemented")
}

is generated where *Bar is a pointer to the Go interface Bar.

I've worked around this for now by making the type not nullable in the GraphQL schema:

type Query {
  bar: Bar!
}

But this means the schema is not accurately expressing the fact that null can also be returned.

This seems similar to https://github.com/99designs/gqlgen/issues/484

Minimal graphql.schema and models to reproduce

schema.graphql

type Foo {
  bar: String
}

union Bar = Foo

type Query {
  bar: Bar
}

gengql.yml

# .gqlgen.yml example
#
# Refer to https://gqlgen.com/config/
# for detailed .gqlgen.yml documentation.

schema:
- schema.graphql
exec:
  filename: generated.go
model:
  filename: models_gen.go
resolver:
  filename: resolver.go
  type: Resolver

models:
  Baz:
    fields:
      foo:
        resolver: true
bug v0.8.2

All 5 comments

Notably, interface types seem to do the right thing here; just not union types.

This appears to be a regression as v0.7.1 generates the right code:

package test_gqlgen

import (
        "context"
)

type Resolver struct{}

func (r *Resolver) Query() QueryResolver {
        return &queryResolver{r}
}

type queryResolver struct{ *Resolver }

func (r *queryResolver) Bar(ctx context.Context) (Bar, error) {
        panic("not implemented")
}

Any plans when the new version with this bug fix will be released?

0.8.2 is getting closer

Awesome, thank you for all of your hard work!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

steebchen picture steebchen  路  3Comments

jszwedko picture jszwedko  路  3Comments

lynntobing picture lynntobing  路  3Comments

cemremengu picture cemremengu  路  3Comments

sumanthakannantha picture sumanthakannantha  路  3Comments