|
|
@@ -1,7 +1,7 @@
|
|
|
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
|
|
|
import { GqlExecutionContext } from '@nestjs/graphql';
|
|
|
import { VariableValues } from 'apollo-server-core';
|
|
|
-import { GraphQLNamedType, OperationDefinitionNode } from 'graphql';
|
|
|
+import { GraphQLNamedType, GraphQLSchema, OperationDefinitionNode } from 'graphql';
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
|
import { GraphqlValueTransformer } from '../common/graphql-value-transformer';
|
|
|
@@ -25,6 +25,7 @@ type TypeTreeNode = {
|
|
|
*/
|
|
|
@Injectable()
|
|
|
export class IdInterceptor implements NestInterceptor {
|
|
|
+ private graphQlValueTransformers = new WeakMap<GraphQLSchema, GraphqlValueTransformer>();
|
|
|
constructor(private idCodecService: IdCodecService) {}
|
|
|
|
|
|
intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> {
|
|
|
@@ -32,12 +33,22 @@ export class IdInterceptor implements NestInterceptor {
|
|
|
if (isGraphQL) {
|
|
|
const args = GqlExecutionContext.create(context).getArgs();
|
|
|
const info = GqlExecutionContext.create(context).getInfo();
|
|
|
- const graphqlValueTransformer = new GraphqlValueTransformer(info.schema);
|
|
|
- this.decodeIdArguments(graphqlValueTransformer, info.operation, args);
|
|
|
+ const transformer = this.getTransformerForSchema(info.schema);
|
|
|
+ this.decodeIdArguments(transformer, info.operation, args);
|
|
|
}
|
|
|
return next.handle();
|
|
|
}
|
|
|
|
|
|
+ private getTransformerForSchema(schema: GraphQLSchema): GraphqlValueTransformer {
|
|
|
+ const existing = this.graphQlValueTransformers.get(schema);
|
|
|
+ if (existing) {
|
|
|
+ return existing;
|
|
|
+ }
|
|
|
+ const transformer = new GraphqlValueTransformer(schema);
|
|
|
+ this.graphQlValueTransformers.set(schema, transformer);
|
|
|
+ return transformer;
|
|
|
+ }
|
|
|
+
|
|
|
private decodeIdArguments(
|
|
|
graphqlValueTransformer: GraphqlValueTransformer,
|
|
|
definition: OperationDefinitionNode,
|