Skip to content

Commit

Permalink
Merge pull request #3309 from SeedCompany/graphql/refactor-changeset-…
Browse files Browse the repository at this point in the history
…editable-context

Change EnforceChangesetEditablePipe to use injected, request-scoped GQL context
  • Loading branch information
CarsonF authored Oct 9, 2024
2 parents b7eba35 + 1fa42ad commit 27ebbb1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
28 changes: 9 additions & 19 deletions src/components/changeset/enforce-changeset-editable.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable, PipeTransform, Type } from '@nestjs/common';
import { Inject, Injectable, PipeTransform, Scope, Type } from '@nestjs/common';
import { CONTEXT } from '@nestjs/graphql';
import {
DataLoaderContext,
DataLoaderStrategy,
Expand All @@ -10,7 +11,7 @@ import {
isIdLike,
loadManyIgnoreMissingThrowAny,
} from '~/common';
import { GqlContextHost, NotGraphQLContext } from '~/core/graphql';
import { isGqlContext } from '~/core/graphql';
import { ResourceLoaderRegistry } from '~/core/resources/loader.registry';
import { Changeset } from './dto';
import { shouldValidateEditability } from './validate-editability.decorator';
Expand All @@ -25,12 +26,12 @@ import { shouldValidateEditability } from './validate-editability.decorator';
* information easily accessible at that point.
* Though it could be possible with some work.
*/
@Injectable()
@Injectable({ scope: Scope.REQUEST })
export class EnforceChangesetEditablePipe implements PipeTransform {
constructor(
private readonly contextHost: GqlContextHost,
// Cannot use request scoped injection here in global pipes,
// So we have to re-create the loader fetching here.
// This is only the GQL context if this is a GQL execution context.
// If it is http, then it is the request.
@Inject(CONTEXT) private readonly context: unknown,
private readonly loaderRegistry: ResourceLoaderRegistry,
private readonly loaderContext: DataLoaderContext,
) {}
Expand All @@ -41,19 +42,8 @@ export class EnforceChangesetEditablePipe implements PipeTransform {
}

async validateRequest(value: any) {
let context;

try {
({ context } = this.contextHost);
} catch (e) {
if (e instanceof NotGraphQLContext) {
// Nothing to do if not GQL request
return;
}
throw e;
}

if (context.operation.operation !== 'mutation') {
const context = isGqlContext(this.context) ? this.context : undefined;
if (context?.operation.operation !== 'mutation') {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/graphql/gql-context.host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { GqlContextType as ContextType } from '~/common';
import { HttpMiddleware as NestMiddleware } from '~/core/http';
import { AsyncLocalStorageNoContextException } from '../async-local-storage-no-context.exception';

export const isGqlContext = (object: unknown): object is ContextType =>
object != null && typeof object === 'object' && isGqlContext.KEY in object;
isGqlContext.KEY = Symbol('GqlContext');

/**
* A service holding the current GraphQL context
*/
Expand Down
2 changes: 2 additions & 0 deletions src/core/graphql/graphql.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { GqlContextType, ServerException, Session } from '~/common';
import { getRegisteredScalars } from '~/common/scalars';
import { ConfigService } from '../config/config.service';
import { VersionService } from '../config/version.service';
import { isGqlContext } from './gql-context.host';
import { GraphqlErrorFormatter } from './graphql-error-formatter';
import { GraphqlTracingPlugin } from './graphql-tracing.plugin';

Expand Down Expand Up @@ -81,6 +82,7 @@ export class GraphqlOptions implements GqlOptionsFactory {

context: ContextFunction<[ExpressContextFunctionArgument], GqlContextType> =
async ({ req, res }) => ({
[isGqlContext.KEY]: true,
request: req,
response: res,
operation: createFakeStubOperation(),
Expand Down
6 changes: 5 additions & 1 deletion src/core/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export * from './graphql.module';
export { GqlContextHost, NotGraphQLContext } from './gql-context.host';
export {
GqlContextHost,
isGqlContext,
NotGraphQLContext,
} from './gql-context.host';

0 comments on commit 27ebbb1

Please sign in to comment.