GQL Inputs
GQLSchemaGen allows you to generate GraphQL input types from your Go structs. Input types are useful for mutations or any operation that requires structured input data. You can use @GqlInput directly on your model struct, or create a dedicated DTO struct. Applying multiple @GqlInput annotations to a single struct allows generating multiple input types (for example, CreateUserInput and UpdateUserInput) from the same base struct.
Additionally, you can add fields that do not exist in the struct using @GqlInputExtraField. This is especially useful for fields like passwords, confirmation codes, or computed values that should exist in the GraphQL input but are not stored directly in the model.
For example:
This configuration produces:
Directive Parameters Reference
| Directive | Parameter | Description |
|---|---|---|
@GqlInput | name | The name of the GraphQL input type to generate. This is required. |
@GqlInput | description | Optional documentation for the input type, included in the schema as a doc string. |
@GqlInput | namespace | Optional namespace for organizing schema files. Used with multiple files strategy. |
@GqlInput | ignoreAll | When true, ignores all struct fields by default (opt-in mode). Use with field-level rw tag or include. |
@GqlInputExtraField | name | Name of the extra field in the input type. Required. |
@GqlInputExtraField | type | GraphQL type of the field (e.g., String!, ID, [String!]). Required. |
@GqlInputExtraField | description | Optional documentation string for the field, which appears in the GraphQL schema. |
@GqlInputExtraField | on | Optional list of input type names to apply the field to. Defaults to * (applies to all inputs annotated on the struct). Multiple names can be comma-separated. |
Usage Notes
- Fields added via
@GqlInputExtraFieldare automatically treated as requiring resolvers or input processing. For example, when adding a password field forCreateUserInput, your mutation resolver or input handling logic must process this field accordingly. - The
onparameter provides granular control over which inputs receive a particular field. You can target specific input types or use*to apply it universally. - You can combine multiple
@GqlInputannotations with@GqlInputExtraFieldto generate fully customized input types from a single struct.
Once your input types are annotated, run:
$ gqlschemagen generateThis will produce both your GraphQL object types and input types in your configured schema output location.