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(); + }); +});