Skip to content

Commit 21c4409

Browse files
authored
[@graphiql/toolkit] do not include require statements in ESM build, include import in esm and require in cjs builds (#3747)
* aa * fix lint * fix vitest
1 parent 2ad4e75 commit 21c4409

File tree

7 files changed

+42
-25
lines changed

7 files changed

+42
-25
lines changed

.changeset/gold-ears-knock.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@graphiql/toolkit': minor
3+
---
4+
5+
do not include `require` statements in ESM build, include `import` in esm and `require` in cjs builds
6+
7+
make `getWsFetcher`, `createWebsocketsFetcherFromUrl` async

packages/graphiql-toolkit/src/create-fetcher/__tests__/lib.spec.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { createClient } from 'graphql-ws';
1515

1616
import { SubscriptionClient } from 'subscriptions-transport-ws';
1717

18-
const exampleWithSubscription = /* GraphQL */ parse(`
18+
const exampleWithSubscription = parse(/* GraphQL */ `
1919
subscription Example {
2020
example
2121
}
@@ -47,9 +47,9 @@ describe('createWebsocketsFetcherFromUrl', () => {
4747
jest.resetAllMocks();
4848
});
4949

50-
it('creates a websockets client using provided url', () => {
50+
it('creates a websockets client using provided url', async () => {
5151
createClient.mockReturnValue(true);
52-
createWebsocketsFetcherFromUrl('wss://example.com');
52+
await createWebsocketsFetcherFromUrl('wss://example.com');
5353
// @ts-ignore
5454
expect(createClient.mock.calls[0][0]).toEqual({ url: 'wss://example.com' });
5555
});
@@ -66,39 +66,41 @@ describe('getWsFetcher', () => {
6666
afterEach(() => {
6767
jest.resetAllMocks();
6868
});
69-
it('provides an observable wsClient when custom wsClient option is provided', () => {
69+
it('provides an observable wsClient when custom wsClient option is provided', async () => {
7070
createClient.mockReturnValue(true);
71-
getWsFetcher({ url: '', wsClient: true });
71+
await getWsFetcher({ url: '', wsClient: true });
7272
// @ts-ignore
7373
expect(createClient.mock.calls).toHaveLength(0);
7474
});
75-
it('creates a subscriptions-transports-ws observable when custom legacyClient option is provided', () => {
75+
it('creates a subscriptions-transports-ws observable when custom legacyClient option is provided', async () => {
7676
createClient.mockReturnValue(true);
77-
getWsFetcher({ url: '', legacyClient: true });
77+
await getWsFetcher({ url: '', legacyClient: true });
7878
// @ts-ignore
7979
expect(createClient.mock.calls).toHaveLength(0);
8080
expect(SubscriptionClient.mock.calls).toHaveLength(0);
8181
});
8282

83-
it('if subscriptionsUrl is provided, create a client on the fly', () => {
83+
it('if subscriptionsUrl is provided, create a client on the fly', async () => {
8484
createClient.mockReturnValue(true);
85-
getWsFetcher({ url: '', subscriptionUrl: 'wss://example' });
85+
await getWsFetcher({ url: '', subscriptionUrl: 'wss://example' });
8686
expect(createClient.mock.calls[0]).toEqual([
8787
{ connectionParams: {}, url: 'wss://example' },
8888
]);
8989
expect(SubscriptionClient.mock.calls).toHaveLength(0);
9090
});
9191
});
9292

93-
describe('missing graphql-ws dependency', () => {
94-
it('should throw a nice error', () => {
93+
describe('missing `graphql-ws` dependency', () => {
94+
it('should throw a nice error', async () => {
9595
jest.resetModules();
9696
jest.doMock('graphql-ws', () => {
9797
// eslint-disable-next-line no-throw-literal
9898
throw { code: 'MODULE_NOT_FOUND' };
9999
});
100100

101-
expect(() => createWebsocketsFetcherFromUrl('wss://example.com')).toThrow(
101+
await expect(
102+
createWebsocketsFetcherFromUrl('wss://example.com'),
103+
).rejects.toThrow(
102104
/You need to install the 'graphql-ws' package to use websockets when passing a 'subscriptionUrl'/,
103105
);
104106
});

packages/graphiql-toolkit/src/create-fetcher/createFetcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function createGraphiQLFetcher(options: CreateFetcherOptions): Fetcher {
2727
? createMultipartFetcher(options, httpFetch)
2828
: simpleFetcher;
2929

30-
return (graphQLParams, fetcherOpts) => {
30+
return async (graphQLParams, fetcherOpts) => {
3131
if (graphQLParams.operationName === 'IntrospectionQuery') {
3232
return (options.schemaFetcher || simpleFetcher)(
3333
graphQLParams,
@@ -41,7 +41,7 @@ export function createGraphiQLFetcher(options: CreateFetcherOptions): Fetcher {
4141
)
4242
: false;
4343
if (isSubscription) {
44-
const wsFetcher = getWsFetcher(options, fetcherOpts);
44+
const wsFetcher = await getWsFetcher(options, fetcherOpts);
4545

4646
if (!wsFetcher) {
4747
throw new Error(

packages/graphiql-toolkit/src/create-fetcher/lib.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ export const createSimpleFetcher =
6868
return data.json();
6969
};
7070

71-
export const createWebsocketsFetcherFromUrl = (
71+
export async function createWebsocketsFetcherFromUrl(
7272
url: string,
7373
connectionParams?: ClientOptions['connectionParams'],
74-
) => {
74+
): Promise<Fetcher | void> {
7575
let wsClient;
7676
try {
77-
const { createClient } = require('graphql-ws') as {
78-
createClient: typeof createClientType;
79-
};
77+
const { createClient } =
78+
process.env.USE_IMPORT === 'false'
79+
? (require('graphql-ws') as { createClient: typeof createClientType })
80+
: await import('graphql-ws');
8081

8182
// TODO: defaults?
8283
wsClient = createClient({ url, connectionParams });
@@ -90,7 +91,7 @@ export const createWebsocketsFetcherFromUrl = (
9091
// eslint-disable-next-line no-console
9192
console.error(`Error creating websocket client for ${url}`, err);
9293
}
93-
};
94+
}
9495

9596
/**
9697
* Create ws/s fetcher using provided wsClient implementation
@@ -177,10 +178,10 @@ export const createMultipartFetcher = (
177178
/**
178179
* If `wsClient` or `legacyClient` are provided, then `subscriptionUrl` is overridden.
179180
*/
180-
export const getWsFetcher = (
181+
export async function getWsFetcher(
181182
options: CreateFetcherOptions,
182183
fetcherOpts?: FetcherOpts,
183-
): Fetcher | void => {
184+
): Promise<Fetcher | void> {
184185
if (options.wsClient) {
185186
return createWebsocketsFetcherFromClient(options.wsClient);
186187
}
@@ -194,4 +195,4 @@ export const getWsFetcher = (
194195
if (legacyWebsocketsClient) {
195196
return createLegacyWebsocketsFetcher(legacyWebsocketsClient);
196197
}
197-
};
198+
}

packages/graphiql-toolkit/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es2016",
3+
"target": "es2017",
44
"module": "ESNext",
55
"declaration": true,
66
"noEmit": true,

packages/graphiql-toolkit/tsup.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const opts: Options = {
55
bundle: false,
66
clean: true,
77
dts: true,
8+
minifySyntax: true,
89
};
910

1011
export default defineConfig([
@@ -13,10 +14,16 @@ export default defineConfig([
1314
format: 'esm',
1415
outDir: 'dist/esm',
1516
outExtension: () => ({ js: '.js' }),
17+
env: {
18+
USE_IMPORT: 'true',
19+
},
1620
},
1721
{
1822
...opts,
1923
format: 'cjs',
2024
outDir: 'dist/cjs',
25+
env: {
26+
USE_IMPORT: 'false',
27+
},
2128
},
2229
]);

packages/monaco-graphql/test/monaco-editor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('monaco-editor', () => {
1515
// expect(lines[1]).toMatch(' building for production...');
1616
// expect(lines[2]).toBe('transforming...');
1717
expect(lines[3]).toMatch(
18-
`✓ ${parseInt(version, 10) > 16 ? 857 : 843} modules transformed.`,
18+
`✓ ${parseInt(version, 10) > 16 ? 862 : 843} modules transformed.`,
1919
);
2020
// expect(lines[4]).toBe('rendering chunks...');
2121
// expect(lines[5]).toBe('computing gzip size...');

0 commit comments

Comments
 (0)