Skip to content

Commit

Permalink
fix(query-graphql): Fixed GraphQLResultInfo always returning info w…
Browse files Browse the repository at this point in the history
…ithout paging
  • Loading branch information
TriPSs committed Jul 31, 2023
1 parent ca40cc3 commit 89de5c7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/core/src/services/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export interface QueryService<DTO, C = DeepPartial<DTO>, U = DeepPartial<DTO>> {
/**
* QueryService decorator to register with nestjs-query
* @param DTOClass - the DTO class that the QueryService is used for.
* @param options - InjectableOptions
*/
// eslint-disable-next-line @typescript-eslint/no-redeclare,@typescript-eslint/no-unused-vars -- intentional
export function QueryService<DTO, C = DeepPartial<DTO>, U = DeepPartial<DTO>>(DTOClass: Class<DTO>, options?: InjectableOptions) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/services/relation-query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class RelationQueryService<DTO, C = DeepPartial<DTO>, U = DeepPartial<DTO

constructor(queryService: QueryService<DTO, C, U>, relations: Record<string, QueryServiceRelation<DTO, unknown>>)

constructor(relations: Record<string, QueryServiceRelation<DTO, unknown>>)
constructor(relations: Record<string, QueryServiceRelation<DTO, any>>)

constructor(
queryService: QueryService<DTO, C, U> | Record<string, QueryServiceRelation<DTO, unknown>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { GraphQLResolveInfo } from 'graphql'

import type { AggregateQuery, QueryResolveTree } from '@ptc-org/nestjs-query-core'

import { simplifyResolveInfo } from './graphql-resolve-info.utils'
import { removePagingFromSimplifiedInfo, simplifyResolveInfo } from './graphql-resolve-info.utils'

const QUERY_OPERATORS: (keyof AggregateQuery<unknown>)[] = ['groupBy', 'count', 'avg', 'sum', 'min', 'max']

export const AggregateQueryParam = createParamDecorator(<DTO>(data: unknown, ctx: ExecutionContext) => {
const info = GqlExecutionContext.create(ctx).getInfo<GraphQLResolveInfo>()
const simpleResolverInfo = simplifyResolveInfo<DTO>(info)
const simpleResolverInfo = removePagingFromSimplifiedInfo(simplifyResolveInfo<DTO>(info))

return QUERY_OPERATORS.reduce((query, operator) => {
if (simpleResolverInfo.fields[operator]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,31 @@ function isCursorPaging<DTO>(info: unknown): info is QueryResolveTree<CursorConn
}

export function simplifyResolveInfo<DTO>(resolveInfo: ResolveInfo): QueryResolveTree<DTO> {
const simpleInfo = parseFieldNodes(resolveInfo.fieldNodes, resolveInfo, null, resolveInfo.parentType)
return parseFieldNodes<DTO>(resolveInfo.fieldNodes, resolveInfo, null, resolveInfo.parentType) as QueryResolveTree<DTO>
}

export function removePagingFromSimplifiedInfo<DTO>(simpleInfo: QueryResolveTree<DTO>) {
if (isOffsetPaging(simpleInfo)) {
return simpleInfo.fields.nodes as QueryResolveTree<DTO>
} else if (isCursorPaging(simpleInfo)) {
return simpleInfo.fields.edges.fields.node as QueryResolveTree<DTO>
}

return simpleInfo as QueryResolveTree<DTO>
return simpleInfo
}

export function createLookAheadInfo<DTO>(
relations: RelationDescriptor<unknown>[],
simpleResolveInfo: QueryResolveTree<DTO>
): SelectRelation<DTO>[] {
const simplifiedInfoWithoutPaging = removePagingFromSimplifiedInfo(simpleResolveInfo)

return relations
.map((relation): SelectRelation<DTO> | boolean => {
if (relation.name in simpleResolveInfo.fields) {
if (relation.name in simplifiedInfoWithoutPaging.fields) {
return {
name: relation.name,
query: (simpleResolveInfo.fields[relation.name] as QueryResolveTree<DTO>).args || {}
query: (simplifiedInfoWithoutPaging.fields[relation.name] as QueryResolveTree<DTO>).args || {}
}
}

Expand Down

0 comments on commit 89de5c7

Please sign in to comment.