DEV Community

Cover image for πŸš€ GQLSchemaGen v1.0.0: Generate GraphQL Schemas from Go Code
Pablo Ramirez
Pablo Ramirez

Posted on

πŸš€ GQLSchemaGen v1.0.0: Generate GraphQL Schemas from Go Code

I'm excited to release GQLSchemaGen v1.0.0 - a code-first GraphQL schema generator for Go that eliminates the tedious work of manually maintaining .graphqls files.

The Problem

If you've built GraphQL APIs in Go, you know the pain:

  • Write Go structs for your models
  • Manually create matching GraphQL schemas
  • Keep them in sync as your API evolves
  • Repeat for types, inputs, and enums

One missed update and your schema is out of sync. πŸ˜“

The Solution

GQLSchemaGen generates GraphQL schemas directly from your Go code using simple annotations:

// @gqlType
type User struct {
    ID        string    `gql:"id,type:ID"`
    Name      string    `gql:"name"`
    Email     string    `gql:"email"`
    CreatedAt time.Time `gql:"createdAt,ro"` // Read-only
}
Enter fullscreen mode Exit fullscreen mode

Run gqlschemagen generate and get:

type User @goModel(model: "your-module.User") {
  id: ID!
  name: String!
  email: String!
  createdAt: String!
}
Enter fullscreen mode Exit fullscreen mode

Features

🎯 Advanced Field Control - Mark fields as read-only, write-only, or conditionally included
🧬 Generics Support - Works with Go 1.18+ generic types seamlessly
πŸ”„ Auto-Discovery - Automatically generates schemas for referenced types
πŸ”§ Scalar Mappings - Map uuid.UUID β†’ ID, time.Time β†’ DateTime globally
πŸ—‚οΈ Namespace Organization - Keep large schemas organized
βš™οΈ gqlgen Integration - Generates @goModel and @goField directives

Real-World Example

Generate multiple GraphQL types from a single Go struct:

// @gqlType(name:"User")
// @gqlType(name:"PublicUser", ignoreAll:"true")
// @gqlInput(name:"CreateUserInput")
// @gqlInput(name:"UpdateUserInput")
type User struct {
    ID       string `gql:"id,type:ID,ro"`
    Name     string `gql:"name,include:*"`
    Email    string `gql:"email,include:User"` // Only in User type
    Password string `gql:"password,wo"`        // Write-only
}
Enter fullscreen mode Exit fullscreen mode

Generated schema:

type User @goModel(model: "your-module.User") {
  id: ID!
  name: String!
  email: String!
  # Password excluded (write-only)
}

type PublicUser @goModel(model: "your-module.User") {
  id: ID!
  name: String!
  # Email excluded (not in include list)
  # Password excluded (write-only)
}

input CreateUserInput {
  id: ID!
  name: String!
  # Email excluded (not in include list)
  password: String!
}

input UpdateUserInput {
  id: ID!
  name: String!
  # Email excluded (not in include list)
  password: String!
}
Enter fullscreen mode Exit fullscreen mode

One Go struct, 4 different GraphQL definitions with precise field control!

Get Started

# Install
go install github.com/pablor21/gqlschemagen@latest

# Initialize config
gqlschemagen init

# Generate schemas
gqlschemagen generate --gqlgen
Enter fullscreen mode Exit fullscreen mode

Learn More

πŸ“– Full Documentation
πŸ’» GitHub Repository
πŸ”Œ VS Code Extension

Built for the Go and GraphQL communities. Contributions welcome! πŸ™Œ

Top comments (0)