From 6f3dccd5e77d7caeb26ebf576c772c771aa1da2d Mon Sep 17 00:00:00 2001 From: niamu01 Date: Sun, 22 Sep 2024 21:41:40 +0900 Subject: [PATCH] fix(graphql): change info to nullable --- .../interfaces/field-middleware.interface.ts | 2 +- .../middleware/middleware-type-check.spec.ts | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 packages/graphql/tests/middleware/middleware-type-check.spec.ts diff --git a/packages/graphql/lib/interfaces/field-middleware.interface.ts b/packages/graphql/lib/interfaces/field-middleware.interface.ts index 957c62c48..d209ed813 100644 --- a/packages/graphql/lib/interfaces/field-middleware.interface.ts +++ b/packages/graphql/lib/interfaces/field-middleware.interface.ts @@ -8,7 +8,7 @@ export interface MiddlewareContext< source: TSource; args: TArgs; context: TContext; - info: GraphQLResolveInfo; + info?: GraphQLResolveInfo | null; } export type NextFn = () => Promise; diff --git a/packages/graphql/tests/middleware/middleware-type-check.spec.ts b/packages/graphql/tests/middleware/middleware-type-check.spec.ts new file mode 100644 index 000000000..21bafadf6 --- /dev/null +++ b/packages/graphql/tests/middleware/middleware-type-check.spec.ts @@ -0,0 +1,58 @@ +import { GraphQLResolveInfo } from 'graphql'; +import { MiddlewareContext, NextFn } from '../../lib/interfaces'; + +export const testMiddleware = async (ctx: MiddlewareContext, next: NextFn) => { + let logData: MiddlewareContext = { + source: ctx.source, + args: ctx.args, + context: ctx.context, + }; + + if (ctx.info) { + logData = { + ...logData, + info: ctx.info, + }; + } + + return next(); +}; + +describe('testMiddleware', () => { + let mockContext: MiddlewareContext; + let mockNext: NextFn; + + beforeEach(() => { + mockContext = { + source: {}, + args: {}, + context: {}, + info: { + path: { typename: 'TestType', key: 'testField' }, + } as GraphQLResolveInfo, + }; + + mockNext = jest.fn().mockResolvedValue('next result'); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should call next when info is provided', () => { + testMiddleware(mockContext, mockNext); + expect(mockNext).toHaveBeenCalled(); + }); + + it('should handle case when info is undefined', () => { + mockContext.info = undefined; + testMiddleware(mockContext, mockNext); + expect(mockNext).toHaveBeenCalled(); + }); + + it('should handle case when info is null', () => { + mockContext.info = null; + testMiddleware(mockContext, mockNext); + expect(mockNext).toHaveBeenCalled(); + }); +});