99designs / gqlgen

go generate based graphql server library
https://gqlgen.com
MIT License
9.95k stars 1.17k forks source link

Plugin code gen fails in vendor mode #2710

Open adrianlungu opened 1 year ago

adrianlungu commented 1 year ago

What happened?

When using -mod=vendor, the project should properly account for imports; however, one of the imports is not added to the mod file due to it being specified only in the go template file resulting in:

merging type systems failed: unable to build object definition: unable to find type: github.com/99designs/gqlgen/graphql/introspection.Schema

The offending file is github.com/99designs/gqlgen/codegen/root_.gotpl where {{ reserveImport "github.com/99designs/gqlgen/graphql/introspection" }} is reserved but not imported explicitly anywhere.

Using _ "github.com/99designs/gqlgen/graphql/introspection" fixes the issue but this should probably be included in the project and not in projects importing the package.

Keep in mind this is in a plugin project that imports gqlgen.

What did you expect?

Code generation to work successfully after running go mod vendor and go mod tidy in vendor mode via -mod=vendor set in GOFLAGS.

Minimal graphql.schema and models to reproduce

N/A

versions

github.com/99designs/gqlgen v0.17.34

go version go1.20.5 darwin/arm64

bradhe commented 10 months ago

I just ran into this too, thanks for documenting it. Was there an actual work around found? You said you imported the subpackage but I'm not sure exactly where you imported it to.

adrianlungu commented 10 months ago

@bradhe yes, I have a cmd/generate/main.go file in which I have the code that loads the gqlgen codegen config, and calls api.Generate; in there, I added the following import

_ "github.com/99designs/gqlgen/graphql/introspection"

to make sure that the code will run.