gqlgen icon indicating copy to clipboard operation
gqlgen copied to clipboard

generate panic when @requires with a union is being used

Open grzn opened this issue 9 months ago • 5 comments

We have something like this in our schema

extend type SomeType @key(fields: "id") {
  id: ID! @external
  someField @requires(fields: "anotherField { unionField { ... on typeA { field1 } } }")
}

when we add unionField to requires then go run github.com/99designs/gqlgen generate panics:

federation: template.gotpl: template: template.gotpl:199:54: executing "template.gotpl" at <.Type.UnmarshalFunc>: error calling UnmarshalFunc: runtime error: invalid memory address or nil pointer dereference

grzn avatar Mar 22 '25 18:03 grzn

hmmm, not sure why yet, and if its works as expected in rutime, but with

options:
  explicit_requires: true

generate works.

grzn avatar Mar 22 '25 18:03 grzn

with computed_requires it works as well; closing

grzn avatar Mar 22 '25 20:03 grzn

Reopening, because when adding __typename is panics:

extend type SomeType @key(fields: "id") {
  id: ID! @external
  someField @requires(fields: "anotherField { unionField { __typename ... on typeA { field1 } } }")
}
go run github.com/99designs/gqlgen
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x7ffbbc]

goroutine 1 [running]:
github.com/99designs/gqlgen/plugin/federation/fieldset.fieldByName(0xa43cd0?, {0xc00030ab6c, 0xa})
	/go/pkg/mod/github.com/99designs/[email protected]/plugin/federation/fieldset/fieldset.go:191 +0x1c
github.com/99designs/gqlgen/plugin/federation/fieldset.Field.TypeReference({0xc0003ffc40, 0x3, 0xc0422b8740?}, 0x3c?, {0xc007879688, 0x63, 0xc?})
	/go/pkg/mod/github.com/99designs/[email protected]/plugin/federation/fieldset/fieldset.go:78 +0x8b
github.com/99designs/gqlgen/plugin/federation.(*Federation).GenerateCode(0xc0002d1e90, 0xc000189290)
	/go/pkg/mod/github.com/99designs/[email protected]/plugin/federation/federation.go:286 +0xe36
github.com/99designs/gqlgen/api.Generate(0xc0001e3448, {0x0, 0x0, 0xed4c40?})
	/go/pkg/mod/github.com/99designs/[email protected]/api/generate.go:122 +0xd51
main.init.func2(0xc0000cae80?)
	/go/pkg/mod/github.com/99designs/[email protected]/main.go:190 +0x9f
github.com/urfave/cli/v2.(*Command).Run(0xc0001e46e0, 0xc0000cae80, {0xc0000220b0, 0x1, 0x1})
	/go/pkg/mod/github.com/urfave/cli/[email protected]/command.go:276 +0x7be
github.com/urfave/cli/v2.(*App).RunContext(0xc00019a600, {0xaf0c70, 0xf11a40}, {0xc0000220b0, 0x1, 0x1})
	/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:333 +0x5a5
github.com/urfave/cli/v2.(*App).Run(...)
	/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:307
main.main()
	/go/pkg/mod/github.com/99designs/[email protected]/main.go:227 +0x20b
exit status 2

grzn avatar Mar 23 '25 21:03 grzn

Have you tried it with latest gqlgen and found it still occurs? There were a few changes that might fix your problem.

If the problem still occurs with the latest version, do you have a public repository where I can attempt to re-create this problem? I'm not able to help much when I can't

StevenACoffman avatar Apr 07 '25 15:04 StevenACoffman

@grzn could you please provide the minimal reproducible schema? I might be able to try to resolve it.

git-hulk avatar Apr 25 '25 05:04 git-hulk