From 1217f5a9c39cd4f43d9630b8b71d2cef735ecb14 Mon Sep 17 00:00:00 2001 From: doug-martin Date: Mon, 2 Mar 2020 17:27:23 -0600 Subject: [PATCH] Fix for issue 42 * [DOCS] Added clarification around individual resolvers and relations with examples #42 * [ADDED] Exposed `Relatable` mixin from `@nestjs-query/graphql` #42 --- HISTORY.md | 5 +++ documentation/docs/graphql/relations.mdx | 32 +++++++++++++++++++ documentation/docs/graphql/resolvers.mdx | 4 +++ packages/query-graphql/src/index.ts | 3 +- packages/query-graphql/src/resolvers/index.ts | 2 ++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 5da7dd70d..16b550905 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +# v0.5.1 + +* [DOCS] Added clarification around individual resolvers and relations with examples [#42](https://github.com/doug-martin/nestjs-query/issues/42) +* [ADDED] Exposed `Relatable` mixin from `@nestjs-query/graphql` [#42](https://github.com/doug-martin/nestjs-query/issues/42) + # v0.5.0 * Added `decorators` option to resolver options to allow providing custom decorators to endpoints [#36](https://github.com/doug-martin/nestjs-query/issues/36) diff --git a/documentation/docs/graphql/relations.mdx b/documentation/docs/graphql/relations.mdx index 3c3c2149d..6986dc266 100644 --- a/documentation/docs/graphql/relations.mdx +++ b/documentation/docs/graphql/relations.mdx @@ -507,3 +507,35 @@ export class TodoItemResolver extends CRUDResolver(TodoItemDTO, { ``` Now any requests that go to the `update` or `remove` endpoints will require the guard. + +## Relation Mixin + +If you are using the [resolvers individually](./resolvers#individual-resolvers) you can use the following mixins to add relations functionality. + +### Relatable + +When using The `Relatable` mixin adds all relations functionality to a resolver. + +In this example we expose on read endpoints for todo items with a readable subtasks realtion. + +```ts +import { ReadResolver, Relatable } from '@nestjs-query/query-graphql'; +import { Resolver } from '@nestjs/graphql'; +import { AuthGuard } from '../auth.guard'; +import { SubTaskDTO } from '../sub-task/dto/sub-task.dto'; +import { TodoItemDTO } from './dto/todo-item.dto'; +import { TodoItemService } from './todo-item.service'; + +const guards = [AuthGuard]; + +@Resolver(() => TodoItemDTO) +export class TodoItemResolver extends Relatable(TodoItemDTO, { + many: { + subTasks: { DTO: SubTaskDTO, disableRemove: true, disableUpdate: true }, + }, +})(ReadResolver(TodoItemDTO)) { + constructor(readonly service: TodoItemService) { + super(service); + } +} +``` diff --git a/documentation/docs/graphql/resolvers.mdx b/documentation/docs/graphql/resolvers.mdx index 4861f8c56..c055fa4b7 100644 --- a/documentation/docs/graphql/resolvers.mdx +++ b/documentation/docs/graphql/resolvers.mdx @@ -371,8 +371,12 @@ export class TodoItemResolver extends CRUDResolver(TodoItemDTO, { The `@nestjs-query/query-graphql` package exposes each part of `CRUD` into individual mixins and resolvers allowing you to pick and choose what functionality you want to expose. +**NOTE** This is advanced usage of the resolvers API and is subject to change! + **NOTE** All examples below can be achieved with the options exposed through the `CRUDResolver`. +**NOTE** The following resolvers do not expose relations options, to add relations options see [Relateable](./relations#relatable) + ### `CreateResolver` The `CreateResolver` will only expose the `createOne` and `createMany` endpoints. The [options](#options) described for diff --git a/packages/query-graphql/src/index.ts b/packages/query-graphql/src/index.ts index 1d1cfb582..bf684fbe8 100644 --- a/packages/query-graphql/src/index.ts +++ b/packages/query-graphql/src/index.ts @@ -1,3 +1,4 @@ export * from './types'; -export { FilterableField } from './decorators'; +export { FilterableField, ResolverMethodOpts } from './decorators'; export * from './resolvers'; +export { DTONamesOpts } from './common'; diff --git a/packages/query-graphql/src/resolvers/index.ts b/packages/query-graphql/src/resolvers/index.ts index b882a6c26..56c985428 100644 --- a/packages/query-graphql/src/resolvers/index.ts +++ b/packages/query-graphql/src/resolvers/index.ts @@ -3,3 +3,5 @@ export { CreateResolver, CreateResolverOpts } from './create.resolver'; export { ReadResolver, ReadResolverOpts } from './read.resolver'; export { UpdateResolver, UpdateResolverOpts } from './update.resolver'; export { DeleteResolver, DeleteResolverOpts } from './delete.resolver'; +export { Relatable, RemoveRelationsResolver, ReadRelationsResolver, UpdateRelationsResolver } from './relations'; +export { RelationsOpts, ResolverOpts, ResolverRelation } from './resolver.interface';