On this page
Mental model
Dartpollo generates types from the fields your operations select. Your client sends the request; the generated code parses its response.
The build pipeline
- Schema: a local SDL file describes the API’s types and fields.
- Operations:
.graphqlfiles select the fields your application needs. - Dartpollo Generator: reads both and writes response models, input types and optional operation documents.
- json_serializable: writes the
.g.dartfunctions used byfromJsonandtoJson.
Connect the schema and operation in build.yaml:
build.yaml
schema_mapping:
- schema: github.schema.graphql
queries_glob: lib/graphql/viewer.graphql
Prepare the schema
The generator reads SDL from disk. If you need to download it from your API, do that before building; the GitHub tutorial shows how.
Use the generated code
A query named Viewer generates Viewer$Query. With document generation enabled, it also provides:
VIEWER_QUERY_DOCUMENT: the GraphQL document to send through your client.VIEWER_QUERY_DOCUMENT_OPERATION_NAME: the operation name,Viewer.
Parse the response’s data object with the generated factory:
final data = Viewer$Query.fromJson({
'viewer': {'id': 'user-1', 'login': 'octocat'},
});
print(data.viewer.login);
Your client handles authentication, transport and GraphQL errors. The generated models give you typed access to the returned fields.
Where output lands
For lib/graphql/viewer.graphql, the builder writes:
lib/__generated__/viewer.graphql.dart
lib/__generated__/viewer.graphql.g.dart
Rebuild after editing the schema or operations. Choose an integration mode to use these files with your client.