Skip to content

Commit

Permalink
feat(graphql,relations): Revert back to unPagedRelation
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Feb 26, 2021
1 parent 171673c commit cb3dc62
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 77 deletions.
2 changes: 1 addition & 1 deletion documentation/docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Yes!

The `@FilterableField` decorator accepts the same arguments as the `@Field` decorator from `@nestjs/graphql`

The `@Relation` `@FilterableRelation`, `@AllRelations`, `@FilterableAllRelations`, `@OffsetConnection`, `@FilterableOffsetConnection`, `@CursorConnection`, and `@FilterableCursorConnection` decorators also accept a complexity option.
The `@Relation` `@FilterableRelation`, `@UnPagedRelation`, `@FilterableUnPagedRelation`, `@OffsetConnection`, `@FilterableOffsetConnection`, `@CursorConnection`, and `@FilterableCursorConnection` decorators also accept a complexity option.

To read more about complexity [see the nestjs docs](https://docs.nestjs.com/graphql/complexity)

60 changes: 30 additions & 30 deletions documentation/docs/graphql/relations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ When using the `nestjs-query` you can specify relations that should be exposed f

* `@Relation` - A relation that is a single value (one-to-one, many-to-one)
* `@FilterableRelation` - A `@Relation` that enables filtering the parent by fields of the relation `DTO`.
* `@AllRelations` - An array of relations (e.g, many-to-many, one-to-many) that returns all of the related records.
* `@FilterableAllRelations` - An `@AllRelations` that enables filtering the parent by fields of the relation
* `@UnPagedRelation` - An array of relations (e.g, many-to-many, one-to-many) that returns all of the related records.
* `@FilterableUnPagedRelation` - An `@UnPagedRelation` that enables filtering the parent by fields of the relation
`DTO`.
* `@OffsetConnection` - A connection that represents a collection (e.g, many-to-many, one-to-many) that uses `offset`
based pagination.
Expand All @@ -22,7 +22,7 @@ When using the `nestjs-query` you can specify relations that should be exposed f
connection `DTO`.

:::warning
`@FilterableAllRelations`, `@FilterableOffsetConnection`, and `@FilterableCursorConnection` are not supported by
`@FilterableUnPagedRelation`, `@FilterableOffsetConnection`, and `@FilterableCursorConnection` are not supported by
mongoose!
:::

Expand Down Expand Up @@ -295,22 +295,22 @@ In this example we'll find all subTasks that are related to a `todoItem` with a

```

## @AllRelations
## @UnPagedRelation

You can also use the `@AllRelations` decorator to define a relation that does not use paging and returns an array
You can also use the `@UnPagedRelation` decorator to define a relation that does not use paging and returns an array
of all the related records.

### Example

Based on the entity definition above we can define a `TodoItemDTO` with a `subTasks` relation.

```ts title="todo-item/todo-item.dto.ts" {6}
import { FilterableField, AllRelations } from '@nestjs-query/query-graphql';
import { FilterableField, UnPagedRelation } from '@nestjs-query/query-graphql';
import { ObjectType, ID, GraphQLISODateTime } from '@nestjs/graphql';
import { SubTaskDTO } from '../sub-task/sub-task.dto'

@ObjectType('TodoItem')
@AllRelations('subTasks', () => SubTaskDTO, { disableRemove: true })
@UnPagedRelation('subTasks', () => SubTaskDTO, { disableRemove: true })
export class TodoItemDTO {
@FilterableField(() => ID)
id!: string;
Expand Down Expand Up @@ -394,14 +394,14 @@ input RelationsInput {
If `disableRemove` was set to `false` or not specified a `removeSubTasksFromTodoItem` mutation would also be exposed with the same arguments as `addSubTasksToTodoItem`.
:::

## @FilterableAllRelations
## @FilterableUnPagedRelation

The `@FilterableAllRelations` extends the `@AllRelations` decorator exposing the ability to filter the `DTO` that
The `@FilterableUnPagedRelation` extends the `@UnPagedRelation` decorator exposing the ability to filter the `DTO` that
defines the relation by relation properties.

:::warning
The `@FilterableAllRelations` decorator will **only** work with relations defined by the orm used (e.g. `typeorm`,
`sequelize`). If your relations are federated or you are using `mongoose` you cannot use the `@FilterableAllRelations`
The `@FilterableUnPagedRelation` decorator will **only** work with relations defined by the orm used (e.g. `typeorm`,
`sequelize`). If your relations are federated or you are using `mongoose` you cannot use the `@FilterableUnPagedRelation`
decorator.
:::

Expand All @@ -411,12 +411,12 @@ In this example we'll use the same Entities defined above to create a graphql en
by `SubTasks`.

```ts title="sub-task/sub-task.dto.ts" {6}
import { FilterableField, FilterableAllRelations } from '@nestjs-query/query-graphql';
import { FilterableField, FilterableUnPagedRelation } from '@nestjs-query/query-graphql';
import { ObjectType, ID, GraphQLISODateTime } from '@nestjs/graphql';
import { SubTaskDTO } from '../sub-task/sub-task.dto'

@ObjectType('TodoItem')
@FilterableAllRelations('subTasks', () => SubTaskDTO, { disableRemove: true })
@FilterableUnPagedRelation('subTasks', () => SubTaskDTO, { disableRemove: true })
export class TodoItemDTO {
@FilterableField(() => ID)
id!: string;
Expand All @@ -434,8 +434,8 @@ export class TodoItemDTO {
updated!: Date;
}
```
Notice the use of `@FilterableAllRelations` instead of `@AllRelations`, by using the
`@FilterableAllRelations` version `nestjs-query` will allow filtering on the `subTasks` relation.
Notice the use of `@FilterableUnPagedRelation` instead of `@UnPagedRelation`, by using the
`@FilterableUnPagedRelation` version `nestjs-query` will allow filtering on the `subTasks` relation.

The module definition remains the same.

Expand Down Expand Up @@ -1041,7 +1041,7 @@ Sometimes you may want to expose a relation that has a different name when persi
{ label: 'Relation', value: 'relation', },
{ label: 'CursorConnection', value: 'cursor-connection', },
{ label: 'OffsetConnection', value: 'offset-connection', },
{ label: 'AllRelations', value: 'all-relations', },
{ label: 'UnPagedRelation', value: 'unpaged-relation', },
]
}>
<TabItem value="relation">
Expand All @@ -1068,11 +1068,11 @@ Sometimes you may want to expose a relation that has a different name when persi
```

</TabItem>
<TabItem value="all-relations">
<TabItem value="unpaged-relation">

```ts
// expose subTasks as subTaskConnection in graphql
@AllRelations('subTasks', () => SubTaskDTO, { relationName: 'subTasks' })
@UnPagedRelation('subTasks', () => SubTaskDTO, { relationName: 'subTasks' })
```

</TabItem>
Expand All @@ -1089,7 +1089,7 @@ To disable the `read` `queries` you can set the `disableRead` option to `true`.
{ label: 'Relation', value: 'relation', },
{ label: 'CursorConnection', value: 'cursor-connection', },
{ label: 'OffsetConnection', value: 'offset-connection', },
{ label: 'AllRelations', value: 'all-relations', },
{ label: 'UnPagedRelation', value: 'unpaged-relation', },
]
}>
<TabItem value="relation">
Expand All @@ -1116,11 +1116,11 @@ To disable the `read` `queries` you can set the `disableRead` option to `true`.
```

</TabItem>
<TabItem value="all-relations">
<TabItem value="unpaged-relation">

```ts
// disable reading the relation
@AllRelations('subTaskConnection', () => SubTaskDTO, { disableRead: true })
@UnPagedRelation('subTaskConnection', () => SubTaskDTO, { disableRead: true })
```

</TabItem>
Expand All @@ -1137,7 +1137,7 @@ To disable the `update` `mutations` you can set the `disableUpdate` option to `t
{ label: 'Relation', value: 'relation', },
{ label: 'CursorConnection', value: 'cursor-connection', },
{ label: 'OffsetConnection', value: 'offset-connection', },
{ label: 'AllRelations', value: 'all-relations', },
{ label: 'UnPagedRelation', value: 'unpaged-relation', },
]
}>
<TabItem value="relation">
Expand All @@ -1164,11 +1164,11 @@ To disable the `update` `mutations` you can set the `disableUpdate` option to `t
```

</TabItem>
<TabItem value="all-relations">
<TabItem value="unpaged-relation">

```ts
// disable updating subTasks
@AllRelations('subTasks', () => SubTaskDTO, { disableUpdate: true })
@UnPagedRelation('subTasks', () => SubTaskDTO, { disableUpdate: true })
```

</TabItem>
Expand All @@ -1185,7 +1185,7 @@ To disable the `remove` `mutations` you can set the `disableRemove` option to `t
{ label: 'Relation', value: 'relation', },
{ label: 'CursorConnection', value: 'cursor-connection', },
{ label: 'OffsetConnection', value: 'offset-connection', },
{ label: 'AllRelations', value: 'all-relations', },
{ label: 'UnPagedRelation', value: 'unpaged-relation', },
]
}>
<TabItem value="relation">
Expand All @@ -1212,11 +1212,11 @@ To disable the `remove` `mutations` you can set the `disableRemove` option to `t
```

</TabItem>
<TabItem value="all-relations">
<TabItem value="unpaged-relation">

```ts
// disable removing subTasks from the relations
@AllRelations('subTasks', () => SubTaskDTO, { disableRemove: true })
@UnPagedRelation('subTasks', () => SubTaskDTO, { disableRemove: true })
```

</TabItem>
Expand Down Expand Up @@ -1270,7 +1270,7 @@ We can then add it to our relations
{ label: 'Relation', value: 'relation', },
{ label: 'CursorConnection', value: 'cursor-connection', },
{ label: 'OffsetConnection', value: 'offset-connection', },
{ label: 'AllRelations', value: 'all-relations', },
{ label: 'UnPagedRelation', value: 'unpaged-relation', },
]
}>
<TabItem value="relation">
Expand All @@ -1297,11 +1297,11 @@ We can then add it to our relations
```

</TabItem>
<TabItem value="all-relations">
<TabItem value="unpaged-relation">

```ts
// Add the AuthGuard using the guards option
@AllRelations('subTasks', () => SubTaskDTO, { guards: [AuthGuard] })
@UnPagedRelation('subTasks', () => SubTaskDTO, { guards: [AuthGuard] })
```

</TabItem>
Expand Down
8 changes: 4 additions & 4 deletions documentation/docs/migration-guides/v0.22.x-to-v0.23.x.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ In `v0.23.0` the decorators have been renamed to be more explicit.

* `@Relation` - A relation that is a single value (one-to-one, many-to-one)
* `@FilterableRelation` - A `@Relation` that enables filtering the parent by fields of the relation `DTO`.
* `@AllRelations` - An array of relations (e.g, many-to-many, one-to-many) that returns all the related records.
* `@FilterableAllRelations` - An `@AllRelations` that enables filtering the parent by fields of the relation
* `@UnPagedRelation` - An array of relations (e.g, many-to-many, one-to-many) that returns all the related records.
* `@FilterableUnPagedRelation` - An `@UnPagedRelation` that enables filtering the parent by fields of the relation
`DTO`.
* `@OffsetConnection` - A connection that represents a collection (e.g, many-to-many, one-to-many) that uses `offset`
based pagination.
Expand Down Expand Up @@ -111,14 +111,14 @@ In previous versions the `OFFSET` paging strategy returned an array of relations
//old
@Relation('subTasks', () => [TodoItem], {pagingStrategy: PagingStrategies.NONE})
//new
@AllRelations('subTasks', () => TodoItem)
@UnPagedRelation('subTasks', () => TodoItem)
```

```ts
//old
@FilterableRelation('subTasks', () => [TodoItem], {pagingStrategy: PagingStrategies.NONE})
//new
@FilterableAllRelations('subTasks', () => TodoItem)
@FilterableUnPagedRelation('subTasks', () => TodoItem)
```

```ts
Expand Down
2 changes: 1 addition & 1 deletion documentation/docs/persistence/services.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ This section only applies when you combine your DTO and entity and are using Typ

When your DTO and entity are the same class and you have relations defined, you should not decorate your the relations in the DTO with `@Field` or `@FilterableField`.

Instead decorate the class with `@CursorConnection`, `@OffsetConnection`, '@AllRelations' or `@Relation`.
Instead decorate the class with `@CursorConnection`, `@OffsetConnection`, '@UnPagedRelation' or `@Relation`.

### Example

Expand Down
4 changes: 2 additions & 2 deletions examples/hooks/src/tag/dto/tag.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign */
import {
FilterableField,
FilterableConnection,
FilterableCursorConnection,
BeforeCreateOne,
BeforeCreateMany,
BeforeUpdateOne,
Expand All @@ -14,7 +14,7 @@ import { TodoItemDTO } from '../../todo-item/dto/todo-item.dto';

@ObjectType('Tag')
@KeySet(['id'])
@FilterableConnection('todoItems', () => TodoItemDTO)
@FilterableCursorConnection('todoItems', () => TodoItemDTO)
@BeforeCreateOne(CreatedByHook)
@BeforeCreateMany(CreatedByHook)
@BeforeUpdateOne(UpdatedByHook)
Expand Down
6 changes: 3 additions & 3 deletions examples/hooks/src/todo-item/dto/todo-item.dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { FilterableField, FilterableConnection, KeySet } from '@nestjs-query/query-graphql';
import { FilterableField, FilterableCursorConnection, KeySet } from '@nestjs-query/query-graphql';
import { ObjectType, ID, GraphQLISODateTime } from '@nestjs/graphql';
import { AuthGuard } from '../../auth/auth.guard';
import { SubTaskDTO } from '../../sub-task/dto/sub-task.dto';
import { TagDTO } from '../../tag/dto/tag.dto';

@ObjectType('TodoItem')
@KeySet(['id'])
@FilterableConnection('subTasks', () => SubTaskDTO, { disableRemove: true, guards: [AuthGuard] })
@FilterableConnection('tags', () => TagDTO, { guards: [AuthGuard] })
@FilterableCursorConnection('subTasks', () => SubTaskDTO, { disableRemove: true, guards: [AuthGuard] })
@FilterableCursorConnection('tags', () => TagDTO, { guards: [AuthGuard] })
export class TodoItemDTO {
@FilterableField(() => ID)
id!: number;
Expand Down
4 changes: 2 additions & 2 deletions examples/no-paging/src/tag/dto/tag.dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FilterableField, AllRelations } from '@nestjs-query/query-graphql';
import { FilterableField, UnPagedRelation } from '@nestjs-query/query-graphql';
import { ObjectType, ID, GraphQLISODateTime } from '@nestjs/graphql';
import { TodoItemDTO } from '../../todo-item/dto/todo-item.dto';

@ObjectType('Tag')
@AllRelations('todoItems', () => TodoItemDTO)
@UnPagedRelation('todoItems', () => TodoItemDTO)
export class TagDTO {
@FilterableField(() => ID)
id!: number;
Expand Down
6 changes: 3 additions & 3 deletions examples/no-paging/src/todo-item/dto/todo-item.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { FilterableField, AllRelations } from '@nestjs-query/query-graphql';
import { FilterableField, UnPagedRelation } from '@nestjs-query/query-graphql';
import { ObjectType, ID, GraphQLISODateTime } from '@nestjs/graphql';
import { SubTaskDTO } from '../../sub-task/dto/sub-task.dto';
import { TagDTO } from '../../tag/dto/tag.dto';

@ObjectType('TodoItem')
@AllRelations('subTasks', () => SubTaskDTO, { disableRemove: true })
@AllRelations('tags', () => TagDTO)
@UnPagedRelation('subTasks', () => SubTaskDTO, { disableRemove: true })
@UnPagedRelation('tags', () => TagDTO)
export class TodoItemDTO {
@FilterableField(() => ID)
id!: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ input TestDtoFilter {
filterableRelation: TestFilterDtoFilterTestRelationDtoFilter
filterableCursorConnection: TestFilterDtoFilterTestRelationDtoFilter
filterableOffsetConnection: TestFilterDtoFilterTestRelationDtoFilter
filterableAllRelations: TestFilterDtoFilterTestRelationDtoFilter
filterableUnPagedRelations: TestFilterDtoFilterTestRelationDtoFilter
}

input TestFilterDtoFilter {
Expand All @@ -37,7 +37,7 @@ input TestFilterDtoFilter {
filterableRelation: TestFilterDtoFilterTestRelationDtoFilter
filterableCursorConnection: TestFilterDtoFilterTestRelationDtoFilter
filterableOffsetConnection: TestFilterDtoFilterTestRelationDtoFilter
filterableAllRelations: TestFilterDtoFilterTestRelationDtoFilter
filterableUnPagedRelations: TestFilterDtoFilterTestRelationDtoFilter
}

input NumberFieldComparison {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { Filter } from '@nestjs-query/core';
import { Injectable } from '@nestjs/common';
import { Authorizer, Relation, Authorize, AllRelations } from '../../src';
import { Authorizer, Relation, Authorize, UnPagedRelation } from '../../src';
import { getAuthorizerToken } from '../../src/auth';
import { createAuthorizerProviders } from '../../src/providers';

Expand Down Expand Up @@ -39,7 +39,7 @@ describe('createDefaultAuthorizer', () => {
@Relation('relations', () => TestRelation, {
auth: { authorize: (ctx: UserContext) => ({ relationOwnerId: { eq: ctx.user.id } }) },
})
@AllRelations('allDecoratorRelations', () => TestDecoratorRelation)
@UnPagedRelation('unPagedDecoratorRelations', () => TestDecoratorRelation)
@Relation('authorizerRelation', () => RelationWithAuthorizer)
class TestDTO {
ownerId!: number;
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('createDefaultAuthorizer', () => {

it('should create an auth filter for relations using the default auth decorator', async () => {
const authorizer = testingModule.get<Authorizer<TestDTO>>(getAuthorizerToken(TestDTO));
const filter = await authorizer.authorizeRelation('allDecoratorRelations', { user: { id: 2 } });
const filter = await authorizer.authorizeRelation('unPagedDecoratorRelations', { user: { id: 2 } });
expect(filter).toEqual({ decoratorOwnerId: { eq: 2 } });
});

Expand Down
Loading

0 comments on commit cb3dc62

Please sign in to comment.