From 53ac3db4eed81b57e4d09bf311f3c272f99b03c9 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 | 64 +++++++++++++++++++ 2 files changed, 65 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 957c62c48f..077dd8d436 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; } 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 0000000000..5dd7e58bff --- /dev/null +++ b/packages/graphql/tests/middleware/middleware-type-check.spec.ts @@ -0,0 +1,64 @@ +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(async () => { + mockContext = { + source: {}, + args: {}, + context: {}, + info: { + path: { typename: 'TestType', key: 'testField' }, + } as GraphQLResolveInfo, + }; + + mockNext = jest.fn().mockResolvedValue('next result'); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should log the context and call next function', async () => { + const result = await testMiddleware(mockContext, mockNext); + + expect(mockNext).toHaveBeenCalled(); + expect(result).toBe('next result'); + }); + + it('should handle undefined info object', async () => { + mockContext.info = undefined; + + await testMiddleware(mockContext, mockNext); + + expect(mockNext).toHaveBeenCalled(); + }); + + it('should handle null info object', async () => { + mockContext.info = null; + + await testMiddleware(mockContext, mockNext); + + expect(mockNext).toHaveBeenCalled(); + }); +});