Skip to content

fix(graphql): change info to nullable #3314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(graphql): change info to nullable
  • Loading branch information
niamu01 committed Sep 22, 2024
commit 6f3dccd5e77d7caeb26ebf576c772c771aa1da2d
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();
});
});