Przeglądaj źródła

perf(core): Trim payload of Job type to omit verbose ctx data

Closes #1376
Michael Bromley 3 lat temu
rodzic
commit
c25a1e327f

+ 42 - 0
packages/core/src/api/resolvers/entity/job-entity.resolver.ts

@@ -1,6 +1,9 @@
 import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
+import { omit } from '@vendure/common/lib/omit';
+import { pick } from '@vendure/common/lib/pick';
 
 import { Job } from '../../../job-queue/job';
+import { RequestContext, SerializedRequestContext } from '../../common/request-context';
 
 @Resolver('Job')
 export class JobEntityResolver {
@@ -10,4 +13,43 @@ export class JobEntityResolver {
     async duration(@Parent() job: Job) {
         return Math.min(job.duration, this.graphQlMaxInt);
     }
+
+    @ResolveField()
+    async data(@Parent() job: Job) {
+        const ctx = job.data.ctx;
+        if (this.isSerializedRequestContext(ctx)) {
+            // The job data includes a serialized RequestContext object
+            // This can be very large, so we will manually prune it before
+            // returning
+            const prunedCtx = {
+                ...pick(ctx, [
+                    '_apiType',
+                    '_languageCode',
+                    '_authorizedAsOwnerOnly',
+                    '_isAuthorized',
+                    '_channel',
+                ]),
+                _session: ctx._session
+                    ? {
+                          ...ctx._session,
+                          user: ctx._session.user ? omit(ctx._session.user, ['channelPermissions']) : {},
+                      }
+                    : {},
+            };
+            job.data.ctx = prunedCtx;
+        }
+        return job.data;
+    }
+
+    private isSerializedRequestContext(input: unknown): input is SerializedRequestContext {
+        if (typeof input !== 'object' || input == null) {
+            return false;
+        }
+        return (
+            typeof input === 'object' &&
+            input.hasOwnProperty('_apiType') &&
+            input.hasOwnProperty('_channel') &&
+            input.hasOwnProperty('_languageCode')
+        );
+    }
 }