GraphQL CRUD Resolvers
Relevant source files
The following files were used as context for generating this wiki page:
GraphQL CRUD Resolvers in nestjs-query provide a code-first implementation of standard data operations for GraphQL APIs. These resolvers automate the creation of queries and mutations for interacting with underlying data services, including support for filtering, sorting, paging, and relationship management.
The package @ptc-org/nestjs-query-graphql serves as the primary integration layer that connects framework-independent services to NestJS GraphQL endpoints. It works alongside persistence adapters like @ptc-org/nestjs-query-typeorm or @ptc-org/nestjs-query-mongoose to provide end-to-end data handling.
Sources: README.md:14-30, packages/query-graphql/README.md:14-16
Core Operations
Resolvers automatically expose a set of standard operations for DTOs (Data Transfer Objects). These operations cover single entity retrieval, collection queries, and data modification.
Query Operations
- Find One: Retrieves a single record by its unique identifier.
- Query Many: Returns a connection or list of records based on filters.
- Aggregate: Performs calculations like count, sum, average, min, and max across records.
Mutation Operations
- Create One/Many: Adds new records to the system.
- Update One/Many: Modifies existing records based on ID or filter criteria.
- Delete One/Many: Removes records from the system.
Sources: examples/basic/e2e/todo-item.resolver.spec.ts:33-280, examples/auth/e2e/tag.resolver.spec.ts:51-380
Operation Flow
The following diagram illustrates the typical request flow through a CRUD resolver:
This flow shows how the resolver acts as an orchestrator between the GraphQL layer and the underlying Query Service.
Sources: examples/auth/e2e/tag.resolver.spec.ts:312-320, examples/hooks/e2e/todo-item.resolver.spec.ts:468-480
Filtering and Sorting
Resolvers support complex querying through a standardized filter and sorting argument.
| Feature | Description | Example Usage |
|---|---|---|
| Filtering | Filters records using operators like in, eq, lt, like, and is. | filter: { id: { in: [1, 2, 3] } } |
| Sorting | Orders results by specific fields in ASC or DESC directions. | sorting: [{ field: id, direction: DESC }] |
| Nested Filtering | Filters parent records based on properties of related entities. | filter: { tags: { name: { eq: "Home" } } } |
Sources: examples/basic/e2e/todo-item.resolver.spec.ts:133-205, examples/auth/e2e/tag.resolver.spec.ts:133-176
Paging Strategies
The library provides multiple strategies for handling large result sets. These are configured at the resolver level via the pagingStrategy option.
- Cursor Paging: Uses base64 encoded cursors for efficient navigation (e.g.,
first,after). - Offset Paging: Uses traditional
limitandoffsetvalues. - None: Returns a simple array without paging metadata.
Sources: examples/all-paging/e2e/sub-task.resolver.spec.ts:122-290, packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:45-55
Relationship Management
Resolvers handle entity relationships through specialized mutations and nested connection queries.
- Add/Remove Relations: Mutations such as
addTagsToTodoItemorremoveTagsFromTodoItemallow managing many-to-many or one-to-many links. - Set/Unset Relations: Mutation like
setTodoItemOnSubTaskmanages many-to-one links. - Nested Connections: Related entities can be queried as sub-fields, often utilizing DataLoaders to prevent N+1 performance issues.
The sequence diagram shows how DataLoaders optimize nested relationship queries.
Sources: examples/basic/e2e/todo-item.resolver.spec.ts:405-460, packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:145-165
Apollo Federation Support
Resolvers support Apollo Federation V2 for building distributed graphs. The referenceBy configuration enables the _entities query, allowing other subgraphs to resolve references to entities owned by the current resolver.
- Resolve Reference: Automated resolution of entity references using the
@ResolveReference()decorator and@Parent()for representation extraction. - Batched Resolution: Uses DataLoaders to resolve multiple entity references in a single database round-trip.
Sources: packages/query-graphql/tests/integration/federation-n1/federation-reference.spec.ts:80-130, examples/federation-v2-e2e/e2e-test/e2e/federation.spec.ts:38-70
Conclusion
GraphQL CRUD Resolvers in nestjs-query provide a highly configurable and automated way to expose data models via GraphQL. By abstracting repetitive boilerplate for filtering, paging, and relationships, they allow developers to focus on domain-specific logic while maintaining high-performance query execution through batching and efficient service integration.