Skip to content

Commit

Permalink
feat(): log error if subscription is request-scoped
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Oct 20, 2021
1 parent d44ff1c commit 4c546f2
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/services/resolvers-explorer.service.ts
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { isUndefined } from '@nestjs/common/utils/shared.utils';
import { REQUEST } from '@nestjs/core';
import { createContextId } from '@nestjs/core/helpers/context-id-factory';
Expand Down Expand Up @@ -37,6 +37,7 @@ import { GqlContextType } from './gql-execution-context';

@Injectable()
export class ResolversExplorerService extends BaseExplorerService {
private readonly logger = new Logger(ResolversExplorerService.name);
private readonly gqlParamsFactory = new GqlParamsFactory();
private readonly injector = new Injector();

Expand Down Expand Up @@ -103,6 +104,13 @@ export class ResolversExplorerService extends BaseExplorerService {
transform,
);
if (resolver.type === SUBSCRIPTION_TYPE) {
if (!wrapper.isDependencyTreeStatic()) {
// Note: We don't throw an exception here for backward
// compatibility reasons.
this.logger.error(
`"${wrapper.metatype.name}" resolver is request or transient-scoped. Resolvers that register subscriptions with the "@Subscription()" decorator must be static (singleton).`,
);
}
const subscriptionOptions = Reflect.getMetadata(
SUBSCRIPTION_OPTIONS_METADATA,
instance[resolver.methodName],
Expand Down Expand Up @@ -299,7 +307,7 @@ export class ResolversExplorerService extends BaseExplorerService {
TSource extends object = any,
TContext = {},
TArgs = { [argName: string]: any },
TOutput = any
TOutput = any,
>(resolverFn: Function, instance: object, methodKey: string) {
const fieldMiddleware = Reflect.getMetadata(
FIELD_RESOLVER_MIDDLEWARE_METADATA,
Expand All @@ -314,9 +322,10 @@ export class ResolversExplorerService extends BaseExplorerService {
return resolverFn;
}

const originalResolveFnFactory = (
...args: [TSource, TArgs, TContext, GraphQLResolveInfo]
) => () => resolverFn(...args);
const originalResolveFnFactory =
(...args: [TSource, TArgs, TContext, GraphQLResolveInfo]) =>
() =>
resolverFn(...args);

return decorateFieldResolverWithMiddleware<
TSource,
Expand Down

0 comments on commit 4c546f2

Please sign in to comment.