Dartpollo0.1.0-alpha.6
Documentation
On this page

Pokémon tutorial

Generate and execute typed Pokémon queries from repository files without fetching a schema or creating an API token.

Prerequisites

This tutorial runs packages/dartpollo/example/pokemon using the local Dartpollo packages.

Start in packages/dartpollo/example/pokemon.

Inspect the local schema

Open pokemon.schema.graphql and find the Query fields used by the example: pokemon(name:) and pokemons(first:). The builder reads this committed SDL locally; no introspection occurs during generation.

Inspect the first operation

graphql/simple_query.query.graphql

query simple_query {
  pokemon(name: "Charmander") {
    number
    types
  }
}

Read each mapping

build.yaml

options:
  optimize_document_nodes: false
  fragments_glob: graphql/**.fragment.graphql
  schema_mapping:
    - schema: pokemon.schema.graphql
      queries_glob: graphql/simple_query.query.graphql
    - schema: pokemon.schema.graphql
      queries_glob: graphql/big_query.query.graphql
    - schema: pokemon.schema.graphql
      queries_glob: graphql/fragment_query.query.graphql
    - schema: pokemon.schema.graphql
      queries_glob: graphql/fragments_glob.query.graphql

All four mappings use the same schema and exact operation paths. The global fragment glob makes committed fragment files available to each mapping.

Keep optimization false because of the document limitations.

Install and generate

Terminal

cd packages/dartpollo/example/pokemon
dart pub get
dart run build_runner build --delete-conflicting-outputs

Inspect these outputs:

Expected generated files

lib/__generated__/simple_query.graphql.dart
lib/__generated__/simple_query.graphql.g.dart
lib/__generated__/big_query.graphql.dart
lib/__generated__/big_query.graphql.g.dart

The first library contains SimpleQuery$Query, SIMPLE_QUERY_QUERY_DOCUMENT, and SimpleQueryQuery.

Execute the basic query

lib/main.dart

final client = DartpolloClient('https://graphql-pokemon2.vercel.app');
final response = await client.execute(SimpleQueryQuery());
print(response.data?.pokemon?.number);
client.dispose();

Run the committed example:

Terminal

dart run lib/main.dart

The program prints the query response and a list of Pokémon.

Add typed variables and aliases

graphql/big_query.query.graphql

query big_query($quantity: Int!) {
  charmander: pokemon(name: "Charmander") {
    number
    types
  }
  pokemons(first: $quantity) {
    number
    name
  }
}

Construct the required variable through the generated argument type:

lib/main.dart

final query = BigQueryQuery(
  variables: BigQueryArguments(quantity: 5),
);
final response = await client.execute(query);
print(response.data?.charmander?.number);

The alias charmander determines the generated response property. After this works, inspect fragment_query.query.graphql and fragments_glob.query.graphql, then continue with the fragments guide.

Troubleshooting: If generation reports missing files, confirm the current directory is the Pokémon package and that graphql/** plus pokemon.schema.graphql remain in the target sources. Use the troubleshooting reference for exact messages.