GraphQL Generates GraphQL Generates

Overview

When you run go 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:

1. generated.go - The GraphQL Server Engine

Size: ~4,093 lines
Purpose: Complete GraphQL server implementation
You modify: ❌ Never (auto-generated)

What It Contains

A. Resolver Interfaces

gqlgen generates type-safe interfaces from your schema:
Key interfaces:

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 lines
Purpose: 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:
Notice: It reuses your domain.User instead of generating a new type!

3. *.resolvers.go - Resolver Stubs

Size: ~100 lines
Purpose: 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:
  1. generated.go (4,093 lines)
    • Complete GraphQL server
    • Type-safe interfaces
    • Argument parsing
    • Marshaling/unmarshaling
    • Introspection
  2. models_gen.go (36 lines)
    • Input types (CreateUserInput, UpdateUserInput)
    • Output types (UserList)
    • Reuses domain types when possible
  3. *.resolvers.go (137 lines)
    • Stub implementations
    • You fill in the business logic
    • Type-safe method signatures
You write:
  • GraphQL schema (.graphql files)
  • Resolver implementations (fill stubs)
  • Wire up in main.go
Result: Production-ready, type-safe GraphQL API with minimal code!

Why This Matters

Without gqlgen:
  • 5000+ lines of boilerplate
  • Manual type checking
  • Error-prone JSON parsing
  • Hard to maintain
With gqlgen:
  • 200 lines of business logic
  • Compiler-enforced type safety
  • Auto-generated parsing
  • Easy to maintain
This is why gqlgen is the most popular GraphQL library for Go! 🚀