-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(graphql,aggregations): Exclude __typename in aggregations
- Loading branch information
1 parent
99e06f4
commit 3897673
Showing
2 changed files
with
18 additions
and
24 deletions.
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
38 changes: 15 additions & 23 deletions
38
packages/query-graphql/src/decorators/aggregate-query-param.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,30 +1,22 @@ | ||
import { AggregateQuery } from '@nestjs-query/core'; | ||
import { GraphQLResolveInfo, SelectionNode, FieldNode, Kind } from 'graphql'; | ||
import { GraphQLResolveInfo } from 'graphql'; | ||
import { GqlExecutionContext } from '@nestjs/graphql'; | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import graphqlFields from 'graphql-fields'; | ||
|
||
const isFieldNode = (node: SelectionNode): node is FieldNode => { | ||
return node.kind === Kind.FIELD; | ||
}; | ||
|
||
const EXCLUDED_FIELDS = ['__typename']; | ||
const QUERY_OPERATORS: (keyof AggregateQuery<unknown>)[] = ['count', 'avg', 'sum', 'min', 'max']; | ||
export const AggregateQueryParam = createParamDecorator(<DTO>(data: unknown, ctx: ExecutionContext) => { | ||
const info = GqlExecutionContext.create(ctx).getInfo<GraphQLResolveInfo>(); | ||
const query = info.fieldNodes.map(({ selectionSet }) => { | ||
return selectionSet?.selections.reduce((aggQuery, selection) => { | ||
if (isFieldNode(selection)) { | ||
const aggType = selection.name.value; | ||
const fields = selection.selectionSet?.selections | ||
.map((s) => { | ||
if (isFieldNode(s)) { | ||
return s.name.value; | ||
} | ||
return undefined; | ||
}) | ||
.filter((f) => !!f); | ||
return { ...aggQuery, [aggType]: fields }; | ||
} | ||
return aggQuery; | ||
}, {} as AggregateQuery<DTO>); | ||
})[0]; | ||
return query || {}; | ||
const fields = graphqlFields(info, {}, { excludedFields: EXCLUDED_FIELDS }) as Record< | ||
keyof AggregateQuery<DTO>, | ||
Record<keyof DTO, unknown> | ||
>; | ||
return QUERY_OPERATORS.filter((operator) => !!fields[operator]).reduce((query, operator) => { | ||
const queryFields = Object.keys(fields[operator]) as (keyof DTO)[]; | ||
if (queryFields && queryFields.length) { | ||
return { ...query, [operator]: queryFields }; | ||
} | ||
return query; | ||
}, {} as AggregateQuery<DTO>); | ||
}); |