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

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

Closed
jszwedko opened this issue Mar 7, 2019 · 6 comments
Labels
bug Something isn't working v0.8.2 Fixed in 0.8.2

Comments

@jszwedko
Copy link

jszwedko commented Mar 7, 2019

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 #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
@jszwedko
Copy link
Author

jszwedko commented Mar 7, 2019

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

@jszwedko
Copy link
Author

jszwedko commented Mar 7, 2019

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")
}

@jszwedko jszwedko changed the title Nullable union fields with resolvers generate methods expecting to return a pointer to an interface [0.8.1] Nullable union fields with resolvers generate methods expecting to return a pointer to an interface Mar 7, 2019
@vektah vektah added bug Something isn't working v0.8.2 Fixed in 0.8.2 labels Mar 13, 2019
@SeriousSem
Copy link

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

@vektah
Copy link
Collaborator

vektah commented Mar 14, 2019

0.8.2 is getting closer

@jszwedko
Copy link
Author

Awesome, thank you for all of your hard work!

@mxcoppell
Copy link

mxcoppell commented Jan 22, 2022

Folks, which one is the right behavior and why? Thanks!

Between:

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

and

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

In my case of using type/interface as the return type in the schema, I always have B. When the return type is an union, I always get A. Is this expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v0.8.2 Fixed in 0.8.2
Projects
None yet
Development

No branches or pull requests

4 participants