Auto-Generation
Instead of annotating every struct with @gqlType, auto-generation intelligently discovers and generates schemas for types that are referenced by your annotated types. This significantly reduces boilerplate while maintaining control over your public API surface.
Quick Start
Enable auto-generation in your configuration:
Example:
All three types are generated in the schema automatically.
Strategies
none - Manual Only
Only generate types with explicit @gqlType, @gqlInput, or @gqlInclude:
Use case: Full control, minimal schema, explicit API design
referenced - Smart Dependencies (Recommended)
Auto-generate types referenced by annotated types:
Example:
Generated types: User, UserProfile, Post
Not generated: UserSettings (exceeds max_depth)
all - Generate Everything
Auto-generate all structs found in scanned packages:
Use case: Quick prototyping, internal APIs, full codebase exposure
patterns - Pattern-Based Selection
Generate based on glob patterns:
Use case: Organized codebases with clear package structure
Max Depth Control
The max_depth setting controls how deep to traverse type dependencies:
Values:
0- Unlimited depth (use with caution)1- Direct references only (recommended)2- Direct + transitive references3+- Further nesting
Example with depth: 1:
Example with depth: 2:
Include Options
Embedded Types
Auto-generate embedded struct types:
Field Types
Auto-generate types used in fields:
Excluding Types
Using @gqlIgnore
Prevent auto-generation of specific types:
Using Exclude Patterns
Exclude entire packages or patterns:
Out-of-Scope Types
Controls behavior when referenced types are not in scanned packages:
Options:
| Option | Behavior |
|---|---|
warn | Log warnings, keep field (default) |
fail | Stop generation with error |
ignore | Silently allow, keep field |
exclude | Remove fields with out-of-scope types |
Example:
- warn: Generates
UserwithCompanyfield, shows warning - fail: Stops generation with error
- ignore: Generates
Usersilently - exclude: Generates
UserwithoutCompanyfield
The @GqlInclude Directive
Mark types for inclusion without explicit type/input directives:
Use cases:
- Shared utility types
- Base types used across multiple schemas
- Types in external packages that should be included
Generic Type Handling
Auto-generation works with generic types and properly expands type parameters:
Generated:
See Generics Documentation for more details.
Best Practices
- Start with
referencedstrategy: Provides good balance of automation and control - Use
max_depth: 1: Prevents excessive schema generation - Leverage
@gqlIgnore: Explicitly exclude internal/sensitive types - Use exclude_patterns: Filter entire package hierarchies
- Review generated schemas: Ensure only intended types are public
- Combine with explicit annotations: Annotate your API boundary, auto-generate internals
Configuration Reference
Once configured, run:
$ gqlschemagen generateto generate schemas with auto-discovery enabled.