normalize
0.10.0, released 2 years ago by gql-dart.dev
Normalizes and denormalizes GraphQL responses to a cache-friendly map structure.
WASM-readyQuiet but busy
What it does
It traverses GraphQL AST and writes entities with __typename and IDs into a normalized map. It can also reconstruct the original response from that map.
AI-written summary of version 0.10.0, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need client‑side caching for GraphQL queries
- You want to work with normalized data like Apollo cache
- You need to convert GraphQL responses to a reference‑based structure for efficient updates
Skip it when
- You are not using GraphQL
- You require built‑in network transport (use a GraphQL client instead)
Strengths
- Works with gql AST directly
- Supports fragments, variables, interfaces, unions, and type policies
- Lightweight and pure Dart
Weaknesses
- Only handles normalization, not fetching or cache eviction
- Limited support for custom key fields (no functions or nested lists)
- No built‑in reactive or stream integration
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 2
import 'package:gql/language.dart';
import 'package:gql/ast.dart';
import 'package:normalize/normalize.dart';
final DocumentNode query = parseString('''
query TestQuery {
posts { id __typename author { id __typename name } title }
}
''');
final data = {"posts":[{"id":"123","__typename":"Post","author":{"id":"1","__typename":"Author","name":"Paul"},"title":"My post"}]};
final normalized = {};
normalizeOperation(
merge:(dataId,value)=> (normalized[dataId]??={}).addAll(value),
document:query,
data:data,
);
print(normalized);
Works with
Packages commonly used together with normalize: ones it builds on, ones built on it, and its own family.
- gqlnormalize depends on itParse, transform and print GraphQL documents in Dart.
- gql_http_linkSame familyHTTP link for executing GraphQL requests via JSON.
- gql_linkSame familyModular AST-based GraphQL request execution interface.
- gql_code_builderSame familyDart code builder generating typed classes from GraphQL documents and SDL
- gql_dio_linkSame familyA GraphQL terminating link to execute requests via Dio using JSON.
- gql_buildSame familyUseful builders for your GraphQL SDL and documents.
- gql_websocket_linkSame familyGraphQL WebSocket link for executing queries, mutations, and subscriptions.
- ferry_generatorSame familyA Dart code generator to create fully typed OperationRequests for the ferry GraphQL client.