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:

user.go

This configuration produces:

schema.graphqls

Directive Parameters Reference

DirectiveParameterDescription
@GqlInputnameThe name of the GraphQL input type to generate. This is required.
@GqlInputdescriptionOptional documentation for the input type, included in the schema as a doc string.
@GqlInputnamespaceOptional namespace for organizing schema files. Used with multiple files strategy.
@GqlInputignoreAllWhen true, ignores all struct fields by default (opt-in mode). Use with field-level rw tag or include.
@GqlInputExtraFieldnameName of the extra field in the input type. Required.
@GqlInputExtraFieldtypeGraphQL type of the field (e.g., String!, ID, [String!]). Required.
@GqlInputExtraFielddescriptionOptional documentation string for the field, which appears in the GraphQL schema.
@GqlInputExtraFieldonOptional 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 @GqlInputExtraField are automatically treated as requiring resolvers or input processing. For example, when adding a password field for CreateUserInput, your mutation resolver or input handling logic must process this field accordingly.
  • The on parameter 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 @GqlInput annotations with @GqlInputExtraField to generate fully customized input types from a single struct.

Once your input types are annotated, run:

$ gqlschemagen generate

This will produce both your GraphQL object types and input types in your configured schema output location.