Skip to content
pubscout

graphql_codegen

3.0.2, released 5 months ago by heft.app

Generate Dart serializers and client helpers from GraphQL queries and schemas.

WASM-ready
$ flutter pub add graphql_codegen
Source repository

What it does

It reads GraphQL schema and document files to generate strongly typed Dart classes, serializers, and query/mutation helper methods. It supports optional integration with graphql and graphql_flutter clients.

AI-written summary of version 3.0.2, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You are using GraphQL in a Dart or Flutter project and want strongly typed models
  • You want helper extensions and methods for graphql or graphql_flutter clients
  • You use fragments and inline fragments extensively in your GraphQL queries

Skip it when

  • You prefer manual parsing and plain JSON maps over code generation
  • You are working with REST, gRPC, or WebSockets instead of GraphQL

Strengths

  • Generates strong-typed classes for queries, mutations, and fragments
  • Supports custom scalars and custom enum transformations
  • Provides optional generated client extensions and cache manipulation helpers

Weaknesses

  • Requires build_runner and code generation setup
  • Does not fetch your schema automatically

Setup

Kind
Pure Dart
Effort
Medium
Code generation
Yes (build_runner)
Direct dependencies
10

Add graphql_codegen and build_runner to dev dependencies, configure build.yaml options if needed, and run `dart run build_runner build`.

import 'person.graphql.dart';

main () {
    final data = fetchDataFromSomewhereMaybeOuterSpace();
    final parsedData = Query$FetchPerson.fromJson(data);
    final name = parsedData.fetchPerson?.name;
    print(name);
}

Alternatives

Packages that do the same job and could replace graphql_codegen.

  • ferry

    A GraphQL client for Dart and Flutter with code generation and normalized caching.

  • nhost_graphql_adapter

    Adapter that connects the graphql package to an Nhost.io GraphQL backend

  • dbus

    Native Dart D-Bus client for Linux and Windows.

  • ferry_generator

    A Dart code generator to create fully typed OperationRequests for the ferry GraphQL client.

Works with

Packages commonly used together with graphql_codegen: ones it builds on, ones built on it, and its own family.