Skip to content

Commit

Permalink
fix(graphql): change info to nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
niamu01 committed Sep 22, 2024
1 parent 8d19548 commit 6f3dccd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface MiddlewareContext<
source: TSource;
args: TArgs;
context: TContext;
info: GraphQLResolveInfo;
info?: GraphQLResolveInfo | null;
}

export type NextFn<T = any> = () => Promise<T>;
Expand Down
58 changes: 58 additions & 0 deletions packages/graphql/tests/middleware/middleware-type-check.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 6f3dccd

Please sign in to comment.