DEV Community

Cover image for GraphQL in Flutter: Queries, Mutations, and Why It Changes How You Think About APIs
Yadnesh Teli
Yadnesh Teli

Posted on AI-assisted

GraphQL in Flutter: Queries, Mutations, and Why It Changes How You Think About APIs

Series 7 — Part 1 of 1

GraphQL in Flutter: Queries, Mutations, and Why It Changes How You Think About APIs

A standalone post on integrating GraphQL using the graphql_flutter package

Every API I had worked with in Flutter so far used REST — a fixed set of endpoints that return fixed data shapes. GraphQL is different. Instead of the server deciding what data you get, the client specifies exactly what it needs. I spent a day reading about GraphQL concepts and another day implementing a Flutter app that used it.

The GraphQL mental model

In REST you call GET /products and get everything the server thinks you need. In GraphQL you write a query that specifies exactly which fields you want:

query GetProducts {
  products {
    id
    name
    price
  }
}
Enter fullscreen mode Exit fullscreen mode

The server returns exactly that — no more, no less. This matters in mobile development where you want to minimize payload size over mobile networks.

Setting up graphql_flutter

I used the graphql_flutter package, which wraps the graphql Dart package with Flutter-specific widgets. Setup involves creating a GraphQLClient with an HttpLink pointing to your API endpoint and wrapping your widget tree in a GraphQLProvider.

final client = GraphQLClient(
  link: HttpLink('https://your-api.com/graphql'),
  cache: GraphQLCache(store: InMemoryStore()),
);
Enter fullscreen mode Exit fullscreen mode

Queries and mutations

graphql_flutter provides Query and Mutation widgets that handle loading, error, and data states. I also used the client directly inside repositories for cleaner MVVM separation:

final result = await _client.query(
  QueryOptions(document: gql(getProductsQuery)),
);
if (result.hasException) throw result.exception!;
return (result.data!['products'] as List)
    .map((e) => Product.fromJson(e))
    .toList();
Enter fullscreen mode Exit fullscreen mode

Understanding the implementation

The biggest adjustment from REST was thinking in terms of the graph — products have categories, categories have products, and a single query can traverse both in one round trip. This is powerful for nested data but requires understanding the API's schema before you can write effective queries.

What I learned

  • No over-fetching: GraphQL eliminates over-fetching and under-fetching — you specify exactly what you need.
  • Widgets vs Repositories: For MVVM apps, use the GraphQLClient directly in repositories rather than Query widgets — keeps the view layer clean.
  • Schema-first thinking: GraphQL requires understanding the schema first. Unlike REST where you can guess endpoint names, GraphQL is introspection-first.
  • Cache invalidation: Caching in GraphQL is more nuanced than REST — the InMemoryStore handles it but needs invalidation strategies for mutations.

These explorations — Riverpod, MVVM, GetX, BLoC, Supabase, Animations, and GraphQL — have significantly widened my Flutter toolkit. Next up, I will share what I am building with these skills combined in real-world side projects.

Top comments (0)