graphql_codegen
3.0.2, released 5 months ago by heft.app
Generate Dart serializers and client helpers from GraphQL queries and schemas.
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.
74Compare - nhost_graphql_adapter
Adapter that connects the graphql package to an Nhost.io GraphQL backend
77Compare - dbus
Native Dart D-Bus client for Linux and Windows.
91Compare - ferry_generator
A Dart code generator to create fully typed OperationRequests for the ferry GraphQL client.
75Compare
Works with
Packages commonly used together with graphql_codegen: ones it builds on, ones built on it, and its own family.
- globgraphql_codegen depends on itA file and directory globbing library that supports Bash-style path matching and listing.
- dart_stylegraphql_codegen depends on itProvides an API and CLI to format Dart source code automatically.
- json_annotationgraphql_codegen depends on itProvides annotations for json_serializable to generate JSON (de)serialization code.
- code_buildergraphql_codegen depends on itA fluent, builder-based library for generating valid Dart code programmatically.
- built_collectiongraphql_codegen depends on itImmutable collections based on SDK collections with builder pattern.
- buildgraphql_codegen depends on itPackage for authoring build_runner compatible code generators.
- recasegraphql_codegen depends on itChanges the case of input text to various conventions.
- gqlgraphql_codegen depends on itParse, transform and print GraphQL documents in Dart.