Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class FetchAdapter implements GraphQLAdapter {
fetchFn?: typeof globalThis.fetch,
) {
this.headers = headers ?? {};
this.fetchFn = fetchFn ?? createFetch();
this.fetchFn = (fetchFn ?? createFetch()).bind(globalThis);
}

async execute<T>(
Expand Down
28 changes: 28 additions & 0 deletions graphql/codegen/src/__tests__/codegen/client-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ describe('client-generator', () => {
);
expect(result.content).toContain('createFetch()');
});

it('binds fetchFn to globalThis to avoid Illegal invocation in browsers', () => {
const result = generateOrmClientFile();

// The generated FetchAdapter must .bind(globalThis) to prevent
// "Illegal invocation" when native window.fetch is stored as
// an instance property (which detaches it from its original this).
expect(result.content).toContain('.bind(globalThis)');
});

it('bind(globalThis) prevents Illegal invocation for this-sensitive fetch', () => {
// Simulate a native browser fetch that requires this === globalThis
// (calling window.fetch with any other this throws TypeError)
function thisSensitiveFetch(this: unknown): Promise<Response> {
if (this !== globalThis) {
throw new TypeError('Illegal invocation');
}
return Promise.resolve(new Response(JSON.stringify({ data: null })));
}

// Without bind: storing on an object and calling detaches this
const broken = { fetchFn: thisSensitiveFetch };
expect(() => broken.fetchFn()).toThrow('Illegal invocation');

// With bind: the pattern used in FetchAdapter keeps correct this
const fixed = { fetchFn: thisSensitiveFetch.bind(globalThis) };
expect(() => fixed.fetchFn()).not.toThrow();
});
});

describe('generateQueryBuilderFile', () => {
Expand Down
2 changes: 1 addition & 1 deletion graphql/codegen/src/core/codegen/templates/orm-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class FetchAdapter implements GraphQLAdapter {
fetchFn?: typeof globalThis.fetch,
) {
this.headers = headers ?? {};
this.fetchFn = fetchFn ?? createFetch();
this.fetchFn = (fetchFn ?? createFetch()).bind(globalThis);
}

async execute<T>(
Expand Down
Loading