Skip to content

Commit

Permalink
Merge pull request #782 from nestjs/feat/expose-context-id
Browse files Browse the repository at this point in the history
feat(): use request context id symbol, attach to ctx
  • Loading branch information
kamilmysliwiec authored Apr 17, 2020
2 parents ad87336 + f89e205 commit 61d6876
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
15 changes: 10 additions & 5 deletions lib/services/resolvers-explorer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { InternalCoreModule } from '@nestjs/core/injector/internal-core-module';
import { Module } from '@nestjs/core/injector/module';
import { ModulesContainer } from '@nestjs/core/injector/modules-container';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { REQUEST_CONTEXT_ID } from '@nestjs/core/router/request/request-constants';
import { head, identity } from 'lodash';
import { GqlModuleOptions, SubscriptionOptions } from '..';
import { GqlParamtype } from '../enums/gql-paramtype.enum';
Expand All @@ -30,8 +31,6 @@ import { extractMetadata } from '../utils/extract-metadata.util';
import { BaseExplorerService } from './base-explorer.service';
import { GqlContextType } from './gql-execution-context';

const gqlContextIdSymbol = Symbol('GQL_CONTEXT_ID');

@Injectable()
export class ResolversExplorerService extends BaseExplorerService {
private readonly gqlParamsFactory = new GqlParamsFactory();
Expand Down Expand Up @@ -151,11 +150,17 @@ export class ResolversExplorerService extends BaseExplorerService {
args,
);
let contextId: ContextId;
if (gqlContext && gqlContext[gqlContextIdSymbol]) {
contextId = gqlContext[gqlContextIdSymbol];
if (gqlContext && gqlContext[REQUEST_CONTEXT_ID]) {
contextId = gqlContext[REQUEST_CONTEXT_ID];
} else if (
gqlContext &&
gqlContext.req &&
gqlContext.req[REQUEST_CONTEXT_ID]
) {
contextId = gqlContext.req[REQUEST_CONTEXT_ID];
} else {
contextId = createContextId();
Object.defineProperty(gqlContext, gqlContextIdSymbol, {
Object.defineProperty(gqlContext, REQUEST_CONTEXT_ID, {
value: contextId,
enumerable: false,
configurable: false,
Expand Down
29 changes: 28 additions & 1 deletion lib/utils/merge-defaults.util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isFunction } from '@nestjs/common/utils/shared.utils';
import { GqlModuleOptions } from '../interfaces/gql-module-options.interface';

const defaultOptions: GqlModuleOptions = {
Expand All @@ -9,8 +10,34 @@ export function mergeDefaults(
options: GqlModuleOptions,
defaults: GqlModuleOptions = defaultOptions,
): GqlModuleOptions {
return {
const moduleOptions = {
...defaults,
...options,
};
if (!moduleOptions.context) {
moduleOptions.context = ({ req }) => ({ req });
} else if (isFunction(moduleOptions.context)) {
moduleOptions.context = (...args: unknown[]) => {
const ctx = (options.context as Function)(...args);
const { req } = args[0] as Record<string, unknown>;
if (typeof ctx === 'object') {
return {
req,
...ctx,
};
}
return ctx;
};
} else {
moduleOptions.context = ({ req }: Record<string, unknown>) => {
if (typeof options.context === 'object') {
return {
req,
...options.context,
};
}
return options.context;
};
}
return moduleOptions;
}

0 comments on commit 61d6876

Please sign in to comment.