Integration with GQLGen

To use gqlschemagen with gqlgen, you should run the schema generator before running gqlgen. This ensures your GraphQL schemas are up-to-date before gqlgen generates resolvers and models.

Option 1: Using go:generate Directives

Add this to a file in your project (e.g., graph/generate.go):

graph/generate.go

Then run:

$ go generate ./...

This will:

  • First run gqlschemagen to generate GraphQL schemas from your Go structs
  • Then run gqlgen to generate resolvers and type-safe code

Option 2: Custom generate.go Script

Create a generate.go file with a custom main function:

generate.go

Run it with:

$ go run generate.go

Option 3: Makefile

Create a Makefile with targets:

Makefile

Then run:

$ make generate

Hybrid Approach: Auto-generated + Hand-written Schemas

You can combine auto-generated schemas with hand-written ones:

gqlgen.yml
gqlschemagen.yml

This approach lets you:

  • Auto-generate types from domain models
  • Manually write queries, mutations, and subscriptions
  • Keep concerns separated and organized

Benefits

  • Schemas stay in sync with your Go models
  • No manual schema writing for domain types
  • Full type safety from structs to GraphQL
  • Single source of truth for your data models