Skip to content

Commit

Permalink
test(graphql,federation,doug-martin#1051): added test for federation …
Browse files Browse the repository at this point in the history
…null reference
  • Loading branch information
mwoelk committed Apr 8, 2021
1 parent a4f3475 commit 0498b40
Show file tree
Hide file tree
Showing 20 changed files with 911 additions and 7 deletions.
6 changes: 3 additions & 3 deletions examples/federation/todo-item-graphql/e2e/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export const refresh = async (connection: Connection): Promise<void> => {

const todoRepo = connection.getRepository(TodoItemEntity);
await todoRepo.save([
{ title: 'Create Nest App', completed: true },
{ title: 'Create Entity', completed: false },
{ title: 'Create Entity Service', completed: false },
{ title: 'Create Nest App', completed: true, assigneeId: 1 },
{ title: 'Create Entity', completed: false, assigneeId: 2 },
{ title: 'Create Entity Service', completed: false, assigneeId: 3 },
{ title: 'Add Todo Item Resolver', completed: false },
{ title: 'How to create item With Sub Tasks', completed: false },
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,75 @@ describe('Federated - TodoItemResolver (e2e)', () => {
});
}));

it(`should find a todo and assignee`, () =>
request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query: `{
todoItem(id: 1) {
${todoItemFields}
assigneeId
assignee {
id
__typename
}
}
}`,
})
.expect(200)
.then(({ body }) => {
expect(body).toEqual({
data: {
todoItem: {
id: '1',
title: 'Create Nest App',
completed: true,
description: null,
assigneeId: '1',
assignee: {
id: '1',
__typename: 'User',
},
},
},
});
}));

it(`should return null if there is no assignee`, () =>
request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query: `{
todoItem(id: 5) {
${todoItemFields}
assigneeId
assignee {
id
__typename
}
}
}`,
})
.expect(200)
.then(({ body }) => {
expect(body).toEqual({
data: {
todoItem: {
id: '5',
title: 'How to create item With Sub Tasks',
completed: false,
description: null,
assigneeId: null,
assignee: null,
},
},
});
}));

it(`should return null if the todo item is not found`, () =>
request(app.getHttpServer())
.post('/graphql')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IsString, MaxLength, IsBoolean } from 'class-validator';
import { Field, InputType } from '@nestjs/graphql';
import { IsString, MaxLength, IsBoolean, IsOptional } from 'class-validator';
import { Field, ID, InputType } from '@nestjs/graphql';

@InputType('TodoItemInput')
export class TodoItemInputDTO {
Expand All @@ -11,4 +11,8 @@ export class TodoItemInputDTO {
@IsBoolean()
@Field()
completed!: boolean;

@IsOptional()
@Field(() => ID, { nullable: true })
assigneeId?: number;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IsString, MaxLength, IsBoolean, IsOptional } from 'class-validator';
import { Field, InputType } from '@nestjs/graphql';
import { Field, ID, InputType } from '@nestjs/graphql';

@InputType('TodoItemUpdate')
export class TodoItemUpdateDTO {
Expand All @@ -13,4 +13,8 @@ export class TodoItemUpdateDTO {
@IsBoolean()
@Field({ nullable: true })
completed?: boolean;

@IsOptional()
@Field(() => ID, { nullable: true })
assigneeId?: number;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { FilterableField } from '@nestjs-query/query-graphql';
import { FilterableField, Reference } from '@nestjs-query/query-graphql';
import { ObjectType, ID, GraphQLISODateTime, Directive } from '@nestjs/graphql';
import { UserReferenceDTO } from './user-reference.dto';

@ObjectType('TodoItem')
@Directive('@key(fields: "id")')
@Reference('assignee', () => UserReferenceDTO, { id: 'assigneeId' }, { nullable: true })
export class TodoItemDTO {
@FilterableField(() => ID)
id!: number;
Expand All @@ -16,6 +18,9 @@ export class TodoItemDTO {
@FilterableField()
completed!: boolean;

@FilterableField({ nullable: true })
assigneeId?: string;

@FilterableField(() => GraphQLISODateTime)
created!: Date;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ObjectType, Directive, Field, ID } from '@nestjs/graphql';

@ObjectType('User')
@Directive('@extends')
@Directive('@key(fields: "id")')
export class UserReferenceDTO {
@Field(() => ID)
@Directive('@external')
id!: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class TodoItemEntity {
@Column()
completed!: boolean;

@Column({ nullable: true })
assigneeId?: number;

@CreateDateColumn()
created!: Date;

Expand Down
17 changes: 17 additions & 0 deletions examples/federation/user-graphql/e2e/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Connection } from 'typeorm';
import { UserEntity } from '../src/user/user.entity';
import { executeTruncate } from '../../../helpers';

const tables = ['user'];
export const truncate = async (connection: Connection): Promise<void> => executeTruncate(connection, tables);

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

const userRepo = connection.getRepository(UserEntity);
await userRepo.save([
{ name: 'User 1', email: 'user1@example.com' },
{ name: 'User 2', email: 'user2@example.com' },
{ name: 'User 3', email: 'user3@example.com' },
]);
};
23 changes: 23 additions & 0 deletions examples/federation/user-graphql/e2e/graphql-fragments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const userFields = `
id
name
email
`;

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

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

0 comments on commit 0498b40

Please sign in to comment.