-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(query-graphql): Look ahead
totalCount
Fixes #137
- Loading branch information
Showing
7 changed files
with
89 additions
and
77 deletions.
There are no files selected for viewing
45 changes: 0 additions & 45 deletions
45
packages/query-graphql/src/decorators/graphql-look-ahead-relations.decorator.ts
This file was deleted.
Oops, something went wrong.
28 changes: 20 additions & 8 deletions
28
packages/query-graphql/src/decorators/graphql-resolve-info.decorator.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 |
---|---|---|
@@ -1,21 +1,33 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common' | ||
import { GqlExecutionContext } from '@nestjs/graphql' | ||
|
||
import type { Class, SelectRelation } from '@ptc-org/nestjs-query-core' | ||
import type { GraphQLResolveInfo as ResolveInfo } from 'graphql' | ||
|
||
import { QueryResolveTree, simplifyResolveInfo } from './graphql-resolve-info.utils' | ||
import { createLookAheadInfo, QueryResolveTree, simplifyResolveInfo } from './graphql-resolve-info.utils' | ||
import { getRelationsDescriptors } from './relation.decorator' | ||
|
||
export interface GraphQLResolveInfoResult<InfoDTO, RelationsDTO = InfoDTO> { | ||
info?: QueryResolveTree<InfoDTO> | ||
relations?: SelectRelation<RelationsDTO>[] | ||
} | ||
|
||
/** | ||
* @internal this implementation is not final and subjected to change! Use at own risk! | ||
*/ | ||
export function GraphQLResolveInfo<DTO>(simplify = true): ParameterDecorator { | ||
return createParamDecorator((data: unknown, ctx: ExecutionContext): QueryResolveTree<DTO> | ResolveInfo => { | ||
const resolveInfo = GqlExecutionContext.create(ctx).getInfo<ResolveInfo>() | ||
export const GraphQLResultInfo = <DTO>(DTOClass: Class<DTO>): ParameterDecorator => { | ||
// Get all relations that have look ahead enabled | ||
const relations = getRelationsDescriptors(DTOClass).filter( | ||
(relation) => relation.relationOpts.enableLookAhead && !relation.isMany | ||
) | ||
|
||
if (simplify) { | ||
return simplifyResolveInfo<DTO>(resolveInfo) | ||
} | ||
return createParamDecorator((data: unknown, ctx: ExecutionContext): GraphQLResolveInfoResult<DTO> => { | ||
const info = GqlExecutionContext.create(ctx).getInfo<ResolveInfo>() | ||
const simplifiedInfo = simplifyResolveInfo<DTO>(info) | ||
|
||
return resolveInfo | ||
return { | ||
info: simplifiedInfo, | ||
relations: relations.length === 0 ? [] : createLookAheadInfo<DTO>(relations, simplifiedInfo) | ||
} | ||
})() | ||
} |
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
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