Skip to content

Commit

Permalink
feat(complexity): Add complexity support for relations
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Jul 17, 2020
1 parent c8d6751 commit aa85325
Show file tree
Hide file tree
Showing 33 changed files with 3,008 additions and 8 deletions.
13 changes: 13 additions & 0 deletions .run/start - complexity.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="start - complexity" type="js.build_tools.npm">
<package-json value="$PROJECT_DIR$/examples/package.json" />
<command value="run" />
<scripts>
<script value="start" />
</scripts>
<arguments value="-- complexity" />
<node-interpreter value="project" />
<envs />
<method v="2" />
</configuration>
</component>
12 changes: 12 additions & 0 deletions documentation/docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ To read more and see examples read the following docs.

* [`@FilterableRelation`](./graphql/relations.mdx#filterablerelation-decorator)
* [`@FilterableConnection`](./graphql/relations.mdx#filterableconnection-decorator)


## Does nestjs-query support specifying complexity.

Yes!

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

The `@Relation` `@FilterableRelation`, `@Connection` and `@FilterableConnection` decorators also accept a complexity option.

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

1 change: 1 addition & 0 deletions documentation/docs/graphql/relations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ The following options can be passed to the `@Relation` or `@Connection` decorato

* `relationName` - The name of the relation to use when looking up the relation from the `QueryService`
* `nullable` - Set to `true` if the relation is nullable.
* `complexity` - Set to specify relation complexity. For more info see [complexity docs](https://docs.nestjs.com/graphql/complexity)
* `disableRead` - Set to `true` to disable read operations.
* `disableUpdate` - Set to `true` to disable update operations.
* `disableRemove` - Set to `true` to disable remove operations.
Expand Down
49 changes: 49 additions & 0 deletions examples/complexity/e2e/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Connection } from 'typeorm';
import { SubTaskEntity } from '../src/sub-task/sub-task.entity';
import { TagEntity } from '../src/tag/tag.entity';
import { TodoItemEntity } from '../src/todo-item/todo-item.entity';

const tables = ['todo_item', 'sub_task', 'tag'];
export const truncate = async (connection: Connection): Promise<void> => {
await tables.reduce(async (prev, table) => {
await prev;
await connection.query(`TRUNCATE ${table} RESTART IDENTITY CASCADE`);
}, Promise.resolve());
};

export const refresh = async (connection: Connection): Promise<void> => {
await truncate(connection);

const todoRepo = connection.getRepository(TodoItemEntity);
const subTaskRepo = connection.getRepository(SubTaskEntity);
const tagsRepo = connection.getRepository(TagEntity);

const urgentTag = await tagsRepo.save({ name: 'Urgent' });
const homeTag = await tagsRepo.save({ name: 'Home' });
const workTag = await tagsRepo.save({ name: 'Work' });
const questionTag = await tagsRepo.save({ name: 'Question' });
const blockedTag = await tagsRepo.save({ name: 'Blocked' });

const todoItems = await todoRepo.save([
{ title: 'Create Nest App', completed: true, tags: [urgentTag, homeTag] },
{ title: 'Create Entity', completed: false, tags: [urgentTag, workTag] },
{ title: 'Create Entity Service', completed: false, tags: [blockedTag, workTag] },
{ title: 'Add Todo Item Resolver', completed: false, tags: [blockedTag, homeTag] },
{
title: 'How to create item With Sub Tasks',
completed: false,
tags: [questionTag, blockedTag],
},
]);

await subTaskRepo.save(
todoItems.reduce((subTasks, todo) => {
return [
...subTasks,
{ completed: true, title: `${todo.title} - Sub Task 1`, todoItem: todo },
{ completed: false, title: `${todo.title} - Sub Task 2`, todoItem: todo },
{ completed: false, title: `${todo.title} - Sub Task 3`, todoItem: todo },
];
}, [] as Partial<SubTaskEntity>[]),
);
};
39 changes: 39 additions & 0 deletions examples/complexity/e2e/graphql-fragments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export const todoItemFields = `
id
title
completed
description
`;

export const subTaskFields = `
id
title
description
completed
todoItemId
`;

export const tagFields = `
id
name
`;

export const pageInfoField = `
pageInfo{
hasNextPage
hasPreviousPage
startCursor
endCursor
}
`;

export const edgeNodes = (fields: string): string => {
return `
edges {
node{
${fields}
}
cursor
}
`;
};
Loading

0 comments on commit aa85325

Please sign in to comment.