Loading…
Using GraphQL for High-Performing Mobile Applications
2023-10-18
- Source
- Shopify
- Published
- Added to Yomu
Summary
GraphQL is presented as a way to address mobile developers’ REST API pain points, especially repeated JSON-to-model mappings and failures when server fields or types change. It provides a strongly typed, self-documented schema contract, while introspection enables tooling to generate client code and surface deprecations at compile time. The post describes mobile practices including using reusable UI-oriented fragments, aliases for readable data models, and directives that conditionally include or skip fields; Shopify uses the open-source Syrup tool to generate strongly typed Swift and Kotlin code from application queries. This flexibility shifts responsibility toward query design: Shopify calculates field costs, limits calls through a replenishing per-user cost bucket, and requires clients to consider query complexity for responsiveness and server resources. GraphQL is positioned as language-independent and suited to bandwidth, latency, and UX-sensitive clients.
Context
Mobile applications using REST APIs must map unstructured JSON into statically typed models and maintain hard-coded assumptions as server fields and objects change. The post presents these mappings and synchronization requirements as bug-prone, recurring maintenance concerns, particularly for released applications.
Approach / What changed
Use GraphQL schemas as a strongly typed contract between mobile clients and servers, with introspection and code generation for client models. The practices described include reusable UI-oriented fragments, aliases, conditional directives, and query-cost management; Shopify uses its open-source Syrup tool to generate strongly typed Swift and Kotlin code.
Takeaways
- GraphQL schemas define server-side types as a contract, allowing client tooling to provide warnings about field deprecations and changes at compile time.
- Fragments can reuse the fields required by shared UI components across screens and queries without adding query cost.
- Shopify’s GraphQL rate limiting uses calculated field cost and a replenishing per-user bucket, so complex queries consume proportionally more capacity and can affect responsiveness.