GraphQL Clients
Query exactly the data a screen needs with GraphQL, using clients such as Apollo, urql or graphql_flutter with normalised caching.
Intermediate · about 5 hours · difficulty 3/5
Learn first: Networking Clients & Server-state Caching
GraphQL lets the client ask for exactly the fields it needs in one request, which suits mobile screens that combine data from several resources.
Concepts
- A schema defines types and fields. Queries read, mutations write, subscriptions stream updates.
- Fragments let each component declare its data needs.
- Variables pass arguments:
query Order($id: ID!) { order(id: $id) { id total items { name } } }.
Clients
- Apollo Client or urql for React Native, Apollo Kotlin for Android, Apollo iOS, and graphql_flutter or Ferry for Flutter.
- Normalised caching stores objects by
__typenameandid, so updating an order in one screen updates it everywhere. - Code generation (GraphQL Code Generator, Apollo codegen) produces typed hooks and models from the schema.
Trade-offs
GraphQL shines with complex, nested data and many client types. For simple CRUD APIs, REST with a query cache is often simpler. Watch out for over-fetching through huge queries and for pagination patterns (connections, cursors).
Good enough to move on: you can write a query with variables and fragments, run it through a typed client, and see a mutation update the cache.
Key concepts
- Schema, queries and mutations
- Variables and fragments
- Normalised cache
- Code generation for types
- Cursor pagination
- Subscriptions
Mini project
Build a country explorer app using a public GraphQL countries API, with a list screen, a detail screen, and typed generated queries.
Check yourself
What does a normalised cache do when a mutation returns an updated object with an id already in the cache?
Free resources
- Learn GraphQL (GraphQL Foundation)
- Apollo Client for React (Apollo)