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

gqlgen fails when there are several @link directives #3398

Open
cyrilc-pro opened this issue Nov 26, 2024 · 0 comments
Open

gqlgen fails when there are several @link directives #3398

cyrilc-pro opened this issue Nov 26, 2024 · 0 comments

Comments

@cyrilc-pro
Copy link

What happened?

gqlgen 0.17.57 fails to run on my schema with the following stack trace:

panic: runtime error: index out of range [1] with length 0

goroutine 1 [running]:
github.com/99designs/gqlgen/api.Generate(0xc000149688, {0x0, 0x0, 0xeaf0385?})
	/<redacted>/go/pkg/mod/github.com/99designs/[email protected]/api/generate.go:41 +0x1533
main.init.func2(0xc0001c0bc0?)
	/<redacted>/go/pkg/mod/github.com/99designs/[email protected]/main.go:190 +0x9c
github.com/urfave/cli/v2.(*Command).Run(0xc00018c580, 0xc0001c0bc0, {0xc0000220f0, 0x3, 0x3})
	/<redacted>/go/pkg/mod/github.com/urfave/cli/[email protected]/command.go:276 +0x7e2
github.com/urfave/cli/v2.(*App).RunContext(0xc000140400, {0xf14baa0, 0xf4f9700}, {0xc0000220f0, 0x3, 0x3})
	/<redacted>/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:333 +0x5a5
github.com/urfave/cli/v2.(*App).Run(...)
	/<redacted>/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:307
main.main()
	/<redacted>/go/pkg/mod/github.com/99designs/[email protected]/main.go:227 +0x20b

This is caused by the use of two @link directives:

extend schema
@link(url: "https://specs.apollo.dev/federation/v2.9", import: ["@key", "@shareable", "@composeDirective"])
@link(url: "https://<readacted>.com", import: ["@owner", "@experimental"])
@composeDirective(name: "@owner")
@composeDirective(name: "@experimental")

Using multiple @link directives is required by the use of @composeDirective (following instructions from Apollo's documentation).

As a workaround, we can add the following to gqlgen's YAML configuration:

federation:
  version: 2

What did you expect?

The issue comes from the regular expression (?s)@link.*\(.*url:.*?"(.*?)"[^)]+\) that is used by gqlgen to "guess" the Apollo Federation version.

This regular expression is too "broad" and matches everything between the beginning of the first @link directive and the end of the last @link directive:

@link(url: "https://specs.apollo.dev/federation/v2.9", import: ["@key", "@shareable", "@composeDirective"])
@link(url: "https://<readacted>.com", import: ["@owner", "@experimental"])

I suggest to use a regular expression that matches the official link URL: https://specs.apollo.dev/federation/v(\d+)\.(\d+).

Minimal graphql.schema and models to reproduce

See example schema above.

versions

  • go run github.com/99designs/gqlgen version? v0.17.57
  • go version? go version go1.23.3 darwin/amd64
@cyrilc-pro cyrilc-pro changed the title gqlgen fails when there are many @link directives gqlgen fails when there are several @link directives Nov 26, 2024
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

1 participant