Examples Overview
Relevant source files
The following files were used as context for generating this wiki page:
The nestjs-query repository contains a dedicated examples/ directory that serves as an integration and end-to-end (E2E) testing suite. These examples demonstrate the practical implementation of CRUD functionalities using GraphQL and various persistence layers. Each example project is a runnable NestJS application designed to validate specific framework features like authentication, hooks, query complexity, and database integrations.
Sources: AGENTS.md:6-8, CONTRIBUTING.md:79-81
Project Architecture and Structure
The examples are managed as a monorepo using Nx and Yarn workspaces. The examples/ directory contains multiple independent projects, each configured within the global Nest CLI and Nx workspace settings. These projects depend on the core packages located in the packages/ directory.
Sources: AGENTS.md:6-8, examples/project.json:7-8
Dependency Graph
The following diagram illustrates the relationship between the example applications and the core library packages.
The example applications consume the core primitives and integration packages to provide functional API endpoints.
Sources: examples/project.json:7-8, AGENTS.md:27-31
Core Example Categories
The project categorizes examples based on the specific feature or persistence adapter they implement.
| Example Name | Purpose | Key Files |
|---|---|---|
basic | Demonstrates standard CRUD operations with TypeORM. | examples/basic/e2e/todo-item.resolver.spec.ts |
auth | Implements JWT-based security and resource authorization. | examples/auth/e2e/todo-item.resolver.spec.ts |
hooks | Shows how to intercept operations using beforeCreate, beforeUpdate, etc. | examples/hooks/e2e/todo-item.resolver.spec.ts |
complexity | Validates query complexity limits to prevent resource exhaustion. | examples/complexity/e2e/todo-item.resolver.spec.ts |
federation | Demonstrates Apollo Federation v2 subgraphs and gateway. | examples/federation-v2-e2e/ |
Sources: examples/nest-cli.json:7-160, CONTRIBUTING.md:120-123
E2E Testing Workflow
Every example includes an e2e directory containing .spec.ts files. These tests use supertest to send GraphQL queries and mutations to the running application and verify the response against expected data fixtures.
Sequence of an E2E Test
- The test runner initializes the NestJS application.
- The database is refreshed using SQL init scripts.
- A
supertestrequest sends a GraphQL operation to/graphql. - The application processes the request through resolvers and services.
- The test verifies the status code and response body.
E2E tests ensure that changes to the core packages do not break existing functionality in real-world scenarios.
Sources: examples/basic/e2e/todo-item.resolver.spec.ts:24-42, CONTRIBUTING.md:79-84
Feature Implementations
Authentication and Authorization
The auth example validates that endpoints are protected and that users can only access their own data. Tests ensure that requests without valid JWT tokens return "Unauthorized" and that filters are applied based on the user's identity.
Sources: examples/auth/e2e/todo-item.resolver.spec.ts:51-60, examples/auth/e2e/todo-item.resolver.spec.ts:470-478
Hooks and Interceptors
The hooks example demonstrates the execution of lifecycle hooks. For instance, the beforeCreateOne hook is used to automatically assign a createdBy field to new entities based on the request context.
Sources: examples/hooks/e2e/todo-item.resolver.spec.ts:316-340
Query Complexity
The complexity example uses a plugin to calculate the "cost" of a GraphQL query. If a query exceeds the defined threshold (e.g., 30), the application returns an internal server error.
Sources: examples/complexity/e2e/todo-item.resolver.spec.ts:133-157
Development and Execution
To run these examples locally for development or testing, you must start the backing services using Docker.
- Install dependencies:
yarn - Start databases:
cd ./examples && docker compose up -d - Run E2E tests:
npm run jest:e2e - Start a specific example:
npm run start -- {example-name}
Sources: CONTRIBUTING.md:65-71, CONTRIBUTING.md:125-132, README.md:46-56
The examples module is a critical component of the nestjs-query project, acting as both a comprehensive documentation of features and a robust regression testing suite. By providing diverse scenarios—from basic CRUD to complex federated architectures—it ensures the stability of the core packages across different environments and configurations.