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 faf2446 commit 1aab807
Show file tree
Hide file tree
Showing 2 changed files with 65 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;
}

export type NextFn<T = any> = () => Promise<T>;
Expand Down
64 changes: 64 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,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();
});
});

0 comments on commit 1aab807

Please sign in to comment.