Skip to content

Commit 144304a

Browse files
authored
Revert #3309 / 1fa42ad for EnforceChangesetEditablePipe (#3408)
I didn't realize it at the time, but because this is a global pipe, it applies as a dependency to all controllers/resolvers. Making this request scoped means all controllers/resolvers become request scoped as well. Revert to using `GqlContextHost`/ALS and add `contextMaybe` to prevent the former try/catch need.
1 parent 964a46f commit 144304a

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

src/components/changeset/enforce-changeset-editable.pipe.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import {
22
ArgumentMetadata,
3-
Inject,
43
Injectable,
54
PipeTransform,
6-
Scope,
75
Type,
86
} from '@nestjs/common';
9-
import { CONTEXT } from '@nestjs/graphql';
107
import { hasCtor, isRegularObject } from '@seedcompany/common';
118
import {
129
DataLoaderContext,
@@ -18,7 +15,7 @@ import {
1815
isIdLike,
1916
loadManyIgnoreMissingThrowAny,
2017
} from '~/common';
21-
import { isGqlContext } from '~/core/graphql';
18+
import { GqlContextHost, ifGqlContext } from '~/core/graphql';
2219
import { ResourceLoaderRegistry } from '~/core/resources/loader.registry';
2320
import { Changeset } from './dto';
2421
import { shouldValidateEditability } from './validate-editability.decorator';
@@ -37,12 +34,10 @@ import { shouldValidateEditability } from './validate-editability.decorator';
3734
* it is called for every argument of every resolver/controller.
3835
* So we want to be careful to do as little work as possible.
3936
*/
40-
@Injectable({ scope: Scope.REQUEST })
37+
@Injectable()
4138
export class EnforceChangesetEditablePipe implements PipeTransform {
4239
constructor(
43-
// This is only the GQL context if this is a GQL execution context.
44-
// If it is http, then it is the request.
45-
@Inject(CONTEXT) private readonly context: unknown,
40+
private readonly gqlContextHost: GqlContextHost,
4641
private readonly loaderRegistry: ResourceLoaderRegistry,
4742
private readonly loaderContext: DataLoaderContext,
4843
) {}
@@ -58,7 +53,7 @@ export class EnforceChangesetEditablePipe implements PipeTransform {
5853
return;
5954
}
6055

61-
const context = isGqlContext(this.context) ? this.context : undefined;
56+
const context = ifGqlContext(this.gqlContextHost.contextMaybe);
6257
if (context?.operation.operation !== 'mutation') {
6358
return;
6459
}

src/core/graphql/gql-context.host.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { Injectable, OnModuleDestroy } from '@nestjs/common';
2+
import { isObjectLike } from '@seedcompany/common';
23
import { AsyncLocalStorage } from 'async_hooks';
34
import { GqlContextType as ContextType } from '~/common';
45
import { AsyncLocalStorageNoContextException } from '../async-local-storage-no-context.exception';
56
import { Plugin } from './plugin.decorator';
67

8+
export const ifGqlContext = (object: unknown): ContextType | undefined =>
9+
isGqlContext(object) ? object : undefined;
710
export const isGqlContext = (object: unknown): object is ContextType =>
8-
object != null && typeof object === 'object' && isGqlContext.KEY in object;
11+
isObjectLike(object) && isGqlContext.KEY in object;
912
isGqlContext.KEY = Symbol('GqlContext');
1013

1114
/**
@@ -16,13 +19,22 @@ export abstract class GqlContextHost {
1619
* The current GraphQL context
1720
*/
1821
readonly context: ContextType;
22+
23+
/**
24+
* The current GraphQL context
25+
*/
26+
readonly contextMaybe: ContextType | undefined;
1927
}
2028

2129
@Injectable()
2230
@Plugin()
2331
export class GqlContextHostImpl implements GqlContextHost, OnModuleDestroy {
2432
als = new AsyncLocalStorage<ContextType>();
2533

34+
get contextMaybe() {
35+
return this.als.getStore();
36+
}
37+
2638
get context() {
2739
const context = this.als.getStore();
2840
if (context) {

src/core/graphql/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './graphql.module';
2-
export { GqlContextHost, isGqlContext } from './gql-context.host';
2+
export { GqlContextHost, isGqlContext, ifGqlContext } from './gql-context.host';
33
export * from './plugin.decorator';

0 commit comments

Comments
 (0)