gqlgen icon indicating copy to clipboard operation
gqlgen copied to clipboard

Use embed for all graphql files sources

Open frederikhors opened this issue 2 years ago • 4 comments

@vikstrous Now that I have tried to solve https://github.com/99designs/gqlgen/issues/2196 by myself I have realized that you are only using embed for the first file in the list of possible graphql files of the config schema field.

Can you try adding another in your project (maybe even using the glob way) and check that it's not just me?

Like my one:

schema:
  - schema/*.graphql
  - ../../pkg/**/gqlgen/schema/*.graphql

Just as an example, this is an exceprt of the generated.go file after your improvement:

var sources = []*ast.Source{
	{Name: "schema/schema.graphql", Input: sourceData("schema/schema.graphql"), BuiltIn: false},
	{Name: "../../pkg/player/gqlgen/schema/player.graphql", Input: `type Player {
  id: ID!
  createdAt: Time!
  name: String!
  age: Int!
}
`, BuiltIn: false},
// and so on many other sources here...

frederikhors avatar May 24 '22 20:05 frederikhors

Unfortunately, not all files can be embedded because "embed" doesn't allow embedding files that are not in the package's directory or subdirectories. There's nothing we can do about that. I recommend trying to move the graphql schema files to the same directory as the generated code.

vikstrous avatar May 25 '22 11:05 vikstrous

We should be able to set up embed to be used if you call api.Generate programmatically which is easy to do.

It would be similar to what I did in #2262.

clayne11 avatar Jun 30 '22 16:06 clayne11

Can you PR please?

frederikhors avatar Jun 30 '22 19:06 frederikhors

After looking more closely I didn't exactly understand what we were trying to do. I thought this was about the generation step, not the serving step. As @vikstrous said there's no way to do this unless the schema files are in the same directory, at which point they're already being embedded.

In terms of the Generate API, it's already taking in parsed schema files, so there's no place to use go:embed here - the API callers can already do that in their own code.

clayne11 avatar Jul 07 '22 15:07 clayne11