-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from NansD/feat/add-graphql-adaptaters-for-fin…
…ding-users Feat/add graphql adaptaters for finding users
- Loading branch information
Showing
7 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Field, ObjectType, Int } from '@nestjs/graphql'; | ||
import { Type } from '@nestjs/common'; | ||
|
||
export interface IPaginatedType<T> { | ||
data: T[]; | ||
count: number; | ||
limit: number; | ||
page: number; | ||
} | ||
|
||
export function PaginatedGraphqlResponse<T>( | ||
classRef: Type<T>, | ||
): Type<IPaginatedType<T>> { | ||
@ObjectType({ isAbstract: true }) | ||
abstract class PaginatedType implements IPaginatedType<T> { | ||
constructor(props: IPaginatedType<T>) { | ||
this.count = props.count; | ||
this.limit = props.limit; | ||
this.page = props.page; | ||
this.data = props.data; | ||
} | ||
@Field(() => Int) | ||
page: number; | ||
|
||
@Field(() => Int) | ||
count: number; | ||
|
||
@Field() | ||
limit: number; | ||
|
||
@Field(() => [classRef]) | ||
readonly data: T[]; | ||
} | ||
return PaginatedType as Type<IPaginatedType<T>>; | ||
} |
30 changes: 30 additions & 0 deletions
30
src/modules/user/dtos/graphql/user.graphql-response.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ResponseBase } from '@libs/api/response.base'; | ||
import { Field, ObjectType } from '@nestjs/graphql'; | ||
|
||
@ObjectType() | ||
export class UserGraphqlResponseDto extends ResponseBase { | ||
@Field({ | ||
description: "User's identifier", | ||
}) | ||
id: string; | ||
|
||
@Field({ | ||
description: "User's email address", | ||
}) | ||
email: string; | ||
|
||
@Field({ | ||
description: "User's country of residence", | ||
}) | ||
country: string; | ||
|
||
@Field({ | ||
description: 'Postal code', | ||
}) | ||
postalCode: string; | ||
|
||
@Field({ | ||
description: 'Street where the user is registered', | ||
}) | ||
street: string; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/modules/user/dtos/graphql/user.paginated-gql-response.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Field, ObjectType } from '@nestjs/graphql'; | ||
import { PaginatedGraphqlResponse } from '../../../../libs/api/graphql/paginated.graphql-response.base'; | ||
|
||
import { UserGraphqlResponseDto } from './user.graphql-response.dto'; | ||
|
||
@ObjectType() | ||
export class UserPaginatedGraphqlResponseDto extends PaginatedGraphqlResponse( | ||
UserGraphqlResponseDto, | ||
) { | ||
@Field(() => [UserGraphqlResponseDto]) | ||
data: UserGraphqlResponseDto[]; | ||
} |
38 changes: 38 additions & 0 deletions
38
src/modules/user/queries/find-users/find-users.graphql-resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { QueryBus } from '@nestjs/cqrs'; | ||
import { Args, Query, Resolver } from '@nestjs/graphql'; | ||
import { Result } from 'oxide.ts/dist'; | ||
import { ResponseBase } from '../../../../libs/api/response.base'; | ||
import { Paginated } from '../../../../libs/ddd'; | ||
import { PaginatedParams } from '../../../../libs/ddd/query.base'; | ||
import { UserModel } from '../../database/user.repository'; | ||
import { UserPaginatedGraphqlResponseDto } from '../../dtos/graphql/user.paginated-gql-response.dto'; | ||
import { FindUsersQuery } from './find-users.query-handler'; | ||
|
||
@Resolver() | ||
export class FindUsersGraphqlResolver { | ||
constructor(private readonly queryBus: QueryBus) {} | ||
@Query(() => UserPaginatedGraphqlResponseDto) | ||
async findUsers( | ||
@Args('options', { type: () => String }) | ||
options: PaginatedParams<FindUsersQuery>, | ||
): Promise<UserPaginatedGraphqlResponseDto> { | ||
const query = new FindUsersQuery(options); | ||
const result: Result< | ||
Paginated<UserModel>, | ||
Error | ||
> = await this.queryBus.execute(query); | ||
|
||
const paginated = result.unwrap(); | ||
const response = new UserPaginatedGraphqlResponseDto({ | ||
...paginated, | ||
data: paginated.data.map((user) => ({ | ||
...new ResponseBase(user), | ||
email: user.email, | ||
country: user.country, | ||
street: user.street, | ||
postalCode: user.postalCode, | ||
})), | ||
}); | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters