Skip to content

Commit 6f3dccd

Browse files
committed
fix(graphql): change info to nullable
1 parent 8d19548 commit 6f3dccd

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

packages/graphql/lib/interfaces/field-middleware.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface MiddlewareContext<
88
source: TSource;
99
args: TArgs;
1010
context: TContext;
11-
info: GraphQLResolveInfo;
11+
info?: GraphQLResolveInfo | null;
1212
}
1313

1414
export type NextFn<T = any> = () => Promise<T>;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { GraphQLResolveInfo } from 'graphql';
2+
import { MiddlewareContext, NextFn } from '../../lib/interfaces';
3+
4+
export const testMiddleware = async (ctx: MiddlewareContext, next: NextFn) => {
5+
let logData: MiddlewareContext = {
6+
source: ctx.source,
7+
args: ctx.args,
8+
context: ctx.context,
9+
};
10+
11+
if (ctx.info) {
12+
logData = {
13+
...logData,
14+
info: ctx.info,
15+
};
16+
}
17+
18+
return next();
19+
};
20+
21+
describe('testMiddleware', () => {
22+
let mockContext: MiddlewareContext;
23+
let mockNext: NextFn;
24+
25+
beforeEach(() => {
26+
mockContext = {
27+
source: {},
28+
args: {},
29+
context: {},
30+
info: {
31+
path: { typename: 'TestType', key: 'testField' },
32+
} as GraphQLResolveInfo,
33+
};
34+
35+
mockNext = jest.fn().mockResolvedValue('next result');
36+
});
37+
38+
afterEach(() => {
39+
jest.clearAllMocks();
40+
});
41+
42+
it('should call next when info is provided', () => {
43+
testMiddleware(mockContext, mockNext);
44+
expect(mockNext).toHaveBeenCalled();
45+
});
46+
47+
it('should handle case when info is undefined', () => {
48+
mockContext.info = undefined;
49+
testMiddleware(mockContext, mockNext);
50+
expect(mockNext).toHaveBeenCalled();
51+
});
52+
53+
it('should handle case when info is null', () => {
54+
mockContext.info = null;
55+
testMiddleware(mockContext, mockNext);
56+
expect(mockNext).toHaveBeenCalled();
57+
});
58+
});

0 commit comments

Comments
 (0)