Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsMay 15, 2026·2 min de lecture

gqlgen — Code-First GraphQL Server for Go

A Go library for building GraphQL servers using a schema-first approach with code generation. Define your schema in SDL, run the generator, and implement resolver methods with full type safety.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
gqlgen Overview
Commande CLI universelle
npx tokrepo install cb4a0816-5036-11f1-9bc6-00163e2b0d79

Introduction

gqlgen generates Go code from GraphQL schemas, producing type-safe resolvers, models, and a runtime that handles query parsing, validation, and execution. You write the schema in standard GraphQL SDL, run the generator, and fill in resolver stubs with your business logic.

What gqlgen Does

  • Generates type-safe Go structs and resolver interfaces from GraphQL SDL
  • Handles query parsing, validation, and execution at runtime
  • Supports subscriptions over WebSocket out of the box
  • Provides dataloading patterns to solve the N+1 query problem
  • Integrates with any HTTP router via a standard http.Handler

Architecture Overview

gqlgen reads .graphql schema files and a gqlgen.yml config, then generates Go types for every object, input, and enum plus an interface for each resolver. At runtime, the generated execution engine walks the parsed query AST, calls your resolver methods, and assembles the JSON response. Complexity limiting and tracing middleware are built into the execution pipeline.

Setup & Configuration

  • Initialize a project with go run github.com/99designs/gqlgen init
  • Define your schema in graph/schema.graphqls
  • Customize model binding and paths in gqlgen.yml
  • Regenerate after schema changes with go run github.com/99designs/gqlgen generate
  • Mount the handler: srv := handler.NewDefaultServer(graph.NewExecutableSchema(...))

Key Features

  • Schema-first development with automatic code generation
  • Supports federation for distributed GraphQL architectures
  • Built-in query complexity analysis to prevent abusive queries
  • Plugin system for custom directives, validation, and middleware
  • File upload support via the GraphQL multipart request spec

Comparison with Similar Tools

  • graphql-go — runtime-only library without code generation; resolvers use interface{} instead of typed arguments
  • Apollo Server (Node.js) — ecosystem leader in JavaScript; gqlgen offers comparable features in Go with compiled performance
  • Hasura — auto-generates GraphQL APIs from a database; gqlgen gives full control over resolver logic
  • Juniper (Rust) — similar code-gen approach for Rust; gqlgen targets Go with its own plugin model

FAQ

Q: Can I use gqlgen with an existing Go project? A: Yes. Run gqlgen init in a subdirectory, point gqlgen.yml at your existing models, and mount the handler on your router.

Q: How does gqlgen handle N+1 queries? A: gqlgen documents a dataloader pattern using middleware to batch and cache database calls within a single request.

Q: Does gqlgen support Apollo Federation? A: Yes. Enable federation in gqlgen.yml and add the @key directive to your schema entities.

Q: What Go version is required? A: gqlgen requires Go 1.20 or later.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires