Apollo GraphQL integration

Integrate GraphQL into your Aurelia 2 application with modern tooling, type safety, and optimized performance. This guide covers multiple GraphQL clients and 2025 best practices.

Prerequisites

  • Aurelia 2 project

  • GraphQL API endpoint

  • TypeScript configured

Client Options

Choose the right GraphQL client for your needs:

npm install @apollo/client graphql
  • Bundle size: ~30.7KB minified+gzipped

  • Best for: Complex apps with caching, offline support, and state management

URQL (Lightweight)

npm install @urql/core graphql
  • Bundle size: ~12KB minified+gzipped

  • Best for: Performance-critical apps, mobile-first development

graphql-request (Minimal)

  • Bundle size: ~13.2KB minified+gzipped

  • Best for: Simple scripts, minimal apps without caching needs

Type Generation Setup

Install GraphQL Code Generator for type safety:

Create codegen.ts:

Add to package.json:

Apollo Client Setup

Environment Configuration

Apollo Client Service

Register Service

Using with Type Generation

Create your GraphQL queries in .graphql files:

Run codegen to generate types:

Typed Component Example

Alternative Client Examples

URQL (Lightweight Alternative)

graphql-request (Minimal)

Best Practices

Performance

  • Bundle size matters - Choose URQL (~12KB) or graphql-request (~13KB) for performance-critical apps

  • Caching strategy - Use Apollo Client's normalized cache for complex data relationships

  • Query optimization - Use fragments to avoid data over-fetching

Type Safety

  • Always use codegen - Generate types from your actual operations, not schema

  • Strict typing - Avoid any types, use generated interfaces

  • Operation-based types - Types should match exactly what you query

Error Handling

  • Network errors - Handle offline scenarios gracefully

  • GraphQL errors - Display user-friendly error messages

  • Loading states - Provide clear feedback during data fetching

Development Workflow

This setup provides a complete, type-safe GraphQL integration with Aurelia 2, following 2025 best practices for performance, developer experience, and maintainability.

Last updated

Was this helpful?