Overview
When you rungo run github.com/99designs/gqlgen generate, gqlgen reads your GraphQL schema and generates type-safe Go code for your GraphQL server. This document explains exactly what gets generated and why.
Generated Files
gqlgen generates 3 main files:| File | Lines | Purpose |
|---|---|---|
generated.go | ~4000 | Complete GraphQL server engine |
models_gen.go | ~50 | Input/output types from schema |
*.resolvers.go | ~100 | Resolver stubs (you implement) |
1. generated.go - The GraphQL Server Engine
Size: ~4,093 linesPurpose: Complete GraphQL server implementation
You modify: ❌ Never (auto-generated)
What It Contains
A. Resolver Interfaces
gqlgen generates type-safe interfaces from your schema:B. Argument Parsing
Converts GraphQL arguments to Go types:C. Field Resolution
Handles each GraphQL field:D. Marshaling/Unmarshaling
Converts between Go types and GraphQL JSON:E. Complexity Calculation
For query cost analysis:F. Introspection Support
Powers GraphQL Playground:2. models_gen.go - Input/Output Types
Size: ~36 linesPurpose: Types that don’t exist in your domain
You modify: ❌ Never (auto-generated)
What It Contains
A. Input Types
From your GraphQL schema inputs:B. Optional Fields (Pointers)
For update operations:C. Custom Output Types
Types that don’t exist in domain:domain.User instead of generating a new type!
3. *.resolvers.go - Resolver Stubs
Size: ~100 linesPurpose: Implementation stubs for you to fill in
You modify: ✅ Yes! This is where you write code
What It Contains
A. Generated Stubs
gqlgen creates stub implementations:B. Your Implementation
You replace the panic with actual logic:C. Resolver Types
gqlgen generates resolver type definitions:How It All Works Together
1. You Write GraphQL Schema
2. Run Code Generation
3. gqlgen Generates Code
4. You Implement Resolvers
5. Wire Up in main.go
6. GraphQL API Ready!
Key Benefits
1. Type Safety
2. No Manual JSON Parsing
3. Automatic Updates
When you change your schema:Comparison: Manual vs gqlgen
Without gqlgen
With gqlgen
File Size Breakdown
Summary
gqlgen generates:-
generated.go(4,093 lines)- Complete GraphQL server
- Type-safe interfaces
- Argument parsing
- Marshaling/unmarshaling
- Introspection
-
models_gen.go(36 lines)- Input types (
CreateUserInput,UpdateUserInput) - Output types (
UserList) - Reuses domain types when possible
- Input types (
-
*.resolvers.go(137 lines)- Stub implementations
- You fill in the business logic
- Type-safe method signatures
- GraphQL schema (
.graphqlfiles) - Resolver implementations (fill stubs)
- Wire up in
main.go
Why This Matters
Without gqlgen:- 5000+ lines of boilerplate
- Manual type checking
- Error-prone JSON parsing
- Hard to maintain
- 200 lines of business logic
- Compiler-enforced type safety
- Auto-generated parsing
- Easy to maintain