How netflix/dgs-codegen works
Project type and purpose
• This is an open-source, multi-module Gradle plugin (“DGS Code Generation Plugin”) that reads GraphQL schema files and produces type-safe Java/Kotlin client APIs, constants, and example data fetchers during a build.
Top-level modules and their roles
• graphql-dgs-codegen-core (Kotlin/Java library)
• Core engine: parses GraphQL schemas → builds IR (intermediate representation) → invokes generators
• Contains subpackages for Java, Kotlin, and Kotlin2 code generators
• Exposes a CLI entry point (CodeGenCli) for ad-hoc codegen outside Gradle
• graphql-dgs-codegen-shared-core (Kotlin library)
• Shared client-side runtime types (projections, serializers)
• Utilities for GraphQL requests, input/value sanitization, reserved-keyword filtering
• graphql-dgs-codegen-gradle (Gradle plugin)
• Defines a PluginExtension and a GenerateJavaTask (and analogous Kotlin tasks)
• Reads user configuration, locates schema files, calls into codegen-core’s API
• Hooks into the standard Gradle lifecycle (compileJava, compileKotlin)
Main components
• Input: GraphQL schema file(s) (user project)
• Gradle Plugin:
• PluginExtension (configuration DSL)
• Generate*Task(s)
• CodeGen Core:
• Schema parser → IR builder
• Language generators (JavaGenerator, KotlinGenerator, Kotlin2Generator)
• Uses JavaPoet/KotlinPoet under the covers
• Shared Core:
• Runtime client code (projections, serializers)
• Code used by generated code at runtime
• Output: Generated source files (DgsClient, DgsConstants, data types, interfaces)
Relationships and data flow
• The Gradle Plugin reads configuration → discovers schema files → passes schema paths + config to CodeGen Core APIs
• CodeGen Core parses schemas → builds IR → invokes each language generator → emits source code
• Generated code references types/utilities from Shared Core
• Optionally, the CLI can be invoked directly (outside Gradle) to drive CodeGen Core and get the same outputs
Important architectural patterns/principles
• Modular layering: plugin orchestration layer → core engine → shared client runtime
• Separation of concerns: parsing/IR, code generation, runtime utilities, build integration
• Extensibility: multiple language backends (Java/Kotlin/Kotlin2) implemented via shared generator interfaces
• Plugin integration: standard Gradle “Plugin → Task → Action” pattern