From 7729d3ce6315f2b675c95b2bf52bd78f5a4ad58c Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Sat, 30 Nov 2024 17:00:34 +0700 Subject: [PATCH] refactor: improve code readability and logging in GraphQL builder - Reformat plugin array for better readability in the Builder class constructor. - Enhance unused query handling by adding logging for unused queries in development. - Update TypeScript error comment for clarity in GraphqlModule, specifying that the request is not typed. --- src/Graphql/graphql.builder.ts | 19 ++++++++++++++++--- src/Graphql/graphql.module.ts | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Graphql/graphql.builder.ts b/src/Graphql/graphql.builder.ts index 2fc1699..de41f77 100644 --- a/src/Graphql/graphql.builder.ts +++ b/src/Graphql/graphql.builder.ts @@ -85,11 +85,22 @@ export class Builder extends SchemaBuilder { constructor(private readonly prisma: PrismaClient) { super({ - plugins: [PrismaPlugin, PrismaUtils, SimpleObjectPlugin, SmartSubscriptionPlugin, RelayPlugin, ErrorsPlugin, AuthzPlugin, ZodPlugin], + plugins: [ + PrismaPlugin, + PrismaUtils, + SimpleObjectPlugin, + SmartSubscriptionPlugin, + RelayPlugin, + ErrorsPlugin, + AuthzPlugin, + ZodPlugin, + ], smartSubscriptions: { debounceDelay: 1000, ...subscribeOptionsFromIterator((name, context) => { - return context.isSubscription ? context.websocket.pubSub.asyncIterator(name) : context.http.pubSub.asyncIterator(name) + return context.isSubscription + ? context.websocket.pubSub.asyncIterator(name) + : context.http.pubSub.asyncIterator(name) }), }, zod: { @@ -105,7 +116,9 @@ export class Builder extends SchemaBuilder { client: prisma, exposeDescriptions: true, filterConnectionTotalCount: true, - onUnusedQuery: process.env.NODE_ENV === 'production' ? null : 'warn', + onUnusedQuery: (info) => { + Logger.log(`Unused query: ${info.fieldName}`, 'GraphQL') + }, dmmf: getDatamodel(), }, errors: { diff --git a/src/Graphql/graphql.module.ts b/src/Graphql/graphql.module.ts index 1fdffdc..8fa5f38 100644 --- a/src/Graphql/graphql.module.ts +++ b/src/Graphql/graphql.module.ts @@ -114,7 +114,7 @@ import { PubSubService } from 'src/PubSub/pubsub.service' if (!ctx.extra) { throw new Error('No extra provided') } - // @ts-expect-error: TODO + // @ts-expect-error: Request is not typed ctx.extra.request.headers['x-session-id'] = ctx.connectionParams['x-session-id'] }, },