GQL Enums
GQLSchemaGen can generate GraphQL enum types from Go types and their constants. Enums can be string-based or integer-based (using iota). You annotate your Go type with @GqlEnum, and optionally provide a custom name and description.
String-based enums
Example:
Generates:
Integer-based enums with iota
You can also define enums using iota:
Generates:
Auto-generated names
If you do not specify @GqlEnumValue(name:"..."), GQLSchemaGen generates enum value names automatically:
- Strips the enum type prefix from the constant (e.g.,
UserRoleAdmin → Admin) - Converts the result to SCREAMING_SNAKE_CASE (
Admin → ADMIN)
Example:
Generates:
Deprecated values
You can mark enum values as deprecated with the deprecated parameter:
Generates:
Cross-Package Enums
GQLSchemaGen fully supports enums where the type and its constants are defined in separate packages. This allows you to organize your project cleanly, keeping type definitions and constant values in different modules or files.
For example, you could define the enum type in one package:
And define the constants in another package:
When you run gqlschemagen generate, the generated GraphQL enum will correctly include all constants regardless of which package they are declared in. This makes it easy to maintain large projects without coupling enum type definitions to their values.
Using enums in object types
Once an enum is defined, it can be used in @GqlType annotated structs just like any other type:
Directive Parameters
@GqlEnum
| Parameter | Description |
|---|---|
name | Optional custom GraphQL enum name. Defaults to the Go type name. |
description | Optional description for the enum, used as a doc string. |
namespace | Optional namespace for organizing schema files. Used with multiple files strategy. |
@GqlEnumValue
| Parameter | Description |
|---|---|
name | Optional custom GraphQL enum value name. If omitted, name is auto-generated. |
description | Optional description for the value. |
deprecated | Optional deprecation reason, which will generate @deprecated(reason: "..."). |
Additional Notes
- Enums must have at least one constant to generate.
- Constant values must have explicit type annotations.
- Enum types and constants can be defined in different files or packages. Cross-package enums are fully supported.
- For integer-based enums, only the names are used in the schema; the numeric value is ignored.
- GQLSchemaGen ensures Go ↔ GraphQL conversion automatically when using gqlgen directives.
Once your enums are annotated, run:
$ gqlschemagen generateto include them in your GraphQL schema.