Skip to content

Commit 2ad4e75

Browse files
authored
migrate @graphiql/toolkit to tsup (#3746)
* aa * removes unneeded `undefined` * try * fix * fix vitest
1 parent 7275c19 commit 2ad4e75

File tree

18 files changed

+497
-126
lines changed

18 files changed

+497
-126
lines changed

.changeset/silent-ghosts-fix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphiql/toolkit': minor
3+
---
4+
5+
compile with `tsup` instead of `tsc`

packages/graphiql-toolkit/docs/create-fetcher.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ const url = 'https://my-schema.com/graphql';
6767

6868
const subscriptionUrl = 'wss://my-schema.com/graphql';
6969

70-
const fetcher = createGraphiQLFetcher({
71-
url,
72-
subscriptionUrl,
73-
});
70+
const fetcher = createGraphiQLFetcher({ url, subscriptionUrl });
7471

7572
export const App = () => <GraphiQL fetcher={fetcher} />;
7673

@@ -209,10 +206,7 @@ import { createGraphiQLFetcher } from '@graphiql/toolkit';
209206

210207
const url = 'https://my-schema.com/graphql';
211208

212-
const fetcher = createGraphiQLFetcher({
213-
url,
214-
fetch,
215-
});
209+
const fetcher = createGraphiQLFetcher({ url, fetch });
216210

217211
export const App = () => <GraphiQL fetcher={fetcher} />;
218212

packages/graphiql-toolkit/package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
"url": "https://github.com/graphql/graphiql/issues?q=issue+label:@graphiql/toolkit"
1616
},
1717
"license": "MIT",
18-
"main": "dist/index.js",
19-
"module": "esm/index.js",
20-
"typings": "dist/index.d.ts",
21-
"scripts": {},
18+
"main": "dist/cjs/index.js",
19+
"module": "dist/esm/index.js",
20+
"typings": "dist/esm/index.d.mts",
21+
"scripts": {
22+
"build": "tsup",
23+
"dev": "tsup --watch",
24+
"prebuild": "yarn types:check",
25+
"types:check": "tsc --noEmit"
26+
},
2227
"dependencies": {
2328
"@n1ru4l/push-pull-async-iterable-iterator": "^3.1.0",
2429
"meros": "^1.1.4"
@@ -27,7 +32,8 @@
2732
"graphql": "^17.0.0-alpha.7",
2833
"graphql-ws": "^5.5.5",
2934
"isomorphic-fetch": "^3.0.0",
30-
"subscriptions-transport-ws": "0.11.0"
35+
"subscriptions-transport-ws": "0.11.0",
36+
"tsup": "^8.2.4"
3137
},
3238
"peerDependencies": {
3339
"graphql": "^15.5.0 || ^16.0.0 || ^17.0.0-alpha.2",

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,21 @@ describe('getWsFetcher', () => {
6868
});
6969
it('provides an observable wsClient when custom wsClient option is provided', () => {
7070
createClient.mockReturnValue(true);
71-
getWsFetcher({
72-
url: '',
73-
wsClient: true,
74-
});
71+
getWsFetcher({ url: '', wsClient: true });
7572
// @ts-ignore
7673
expect(createClient.mock.calls).toHaveLength(0);
7774
});
7875
it('creates a subscriptions-transports-ws observable when custom legacyClient option is provided', () => {
7976
createClient.mockReturnValue(true);
80-
getWsFetcher({
81-
url: '',
82-
legacyClient: true,
83-
});
77+
getWsFetcher({ url: '', legacyClient: true });
8478
// @ts-ignore
8579
expect(createClient.mock.calls).toHaveLength(0);
8680
expect(SubscriptionClient.mock.calls).toHaveLength(0);
8781
});
8882

8983
it('if subscriptionsUrl is provided, create a client on the fly', () => {
9084
createClient.mockReturnValue(true);
91-
getWsFetcher({
92-
url: '',
93-
subscriptionUrl: 'wss://example',
94-
});
85+
getWsFetcher({ url: '', subscriptionUrl: 'wss://example' });
9586
expect(createClient.mock.calls[0]).toEqual([
9687
{ connectionParams: {}, url: 'wss://example' },
9788
]);

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,15 @@ import {
1111
* build a GraphiQL fetcher that is:
1212
* - backwards compatible
1313
* - optionally supports graphql-ws or `
14-
*
15-
* @param options {CreateFetcherOptions}
16-
* @returns {Fetcher}
1714
*/
1815
export function createGraphiQLFetcher(options: CreateFetcherOptions): Fetcher {
19-
let httpFetch;
20-
if (typeof window !== 'undefined' && window.fetch) {
21-
httpFetch = window.fetch;
22-
}
23-
if (
24-
options?.enableIncrementalDelivery === null ||
25-
options.enableIncrementalDelivery !== false
26-
) {
27-
options.enableIncrementalDelivery = true;
28-
}
29-
if (options.fetch) {
30-
httpFetch = options.fetch;
31-
}
16+
const httpFetch =
17+
options.fetch || (typeof window !== 'undefined' && window.fetch);
3218
if (!httpFetch) {
3319
throw new Error('No valid fetcher implementation available');
3420
}
21+
options.enableIncrementalDelivery =
22+
options.enableIncrementalDelivery !== false;
3523
// simpler fetcher for schema requests
3624
const simpleFetcher = createSimpleFetcher(options, httpFetch);
3725

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const errorHasCode = (err: unknown): err is { code: string } => {
3232
*/
3333
export const isSubscriptionWithName = (
3434
document: DocumentNode,
35-
name: string | undefined,
35+
name?: string,
3636
): boolean => {
3737
let isSubscription = false;
3838
visit(document, {
@@ -79,10 +79,7 @@ export const createWebsocketsFetcherFromUrl = (
7979
};
8080

8181
// TODO: defaults?
82-
wsClient = createClient({
83-
url,
84-
connectionParams,
85-
});
82+
wsClient = createClient({ url, connectionParams });
8683
return createWebsocketsFetcherFromClient(wsClient);
8784
} catch (err) {
8885
if (errorHasCode(err) && err.code === 'MODULE_NOT_FOUND') {
@@ -97,12 +94,10 @@ export const createWebsocketsFetcherFromUrl = (
9794

9895
/**
9996
* Create ws/s fetcher using provided wsClient implementation
100-
*
101-
* @param wsClient {Client}
102-
* @returns {Fetcher}
10397
*/
10498
export const createWebsocketsFetcherFromClient =
105-
(wsClient: Client) => (graphQLParams: FetcherParams) =>
99+
(wsClient: Client): Fetcher =>
100+
(graphQLParams: FetcherParams) =>
106101
makeAsyncIterableIteratorFromSink<ExecutionResult>(sink =>
107102
wsClient.subscribe(graphQLParams, {
108103
...sink,
@@ -125,12 +120,9 @@ export const createWebsocketsFetcherFromClient =
125120
/**
126121
* Allow legacy websockets protocol client, but no definitions for it,
127122
* as the library is deprecated and has security issues
128-
*
129-
* @param legacyWsClient
130-
* @returns
131123
*/
132124
export const createLegacyWebsocketsFetcher =
133-
(legacyWsClient: { request: (params: FetcherParams) => unknown }) =>
125+
(legacyWsClient: { request: (params: FetcherParams) => unknown }): Fetcher =>
134126
(graphQLParams: FetcherParams) => {
135127
const observable = legacyWsClient.request(graphQLParams);
136128
return makeAsyncIterableIteratorFromSink<ExecutionResult>(
@@ -139,11 +131,8 @@ export const createLegacyWebsocketsFetcher =
139131
);
140132
};
141133
/**
142-
* create a fetcher with the `IncrementalDelivery` HTTP/S spec for
134+
* Create a fetcher with the `IncrementalDelivery` HTTP/S spec for
143135
* `@stream` and `@defer` support using `fetch-multipart-graphql`
144-
*
145-
* @param options {CreateFetcherOptions}
146-
* @returns {Fetcher}
147136
*/
148137
export const createMultipartFetcher = (
149138
options: CreateFetcherOptions,
@@ -187,13 +176,11 @@ export const createMultipartFetcher = (
187176

188177
/**
189178
* If `wsClient` or `legacyClient` are provided, then `subscriptionUrl` is overridden.
190-
* @param options {CreateFetcherOptions}
191-
* @returns
192179
*/
193180
export const getWsFetcher = (
194181
options: CreateFetcherOptions,
195-
fetcherOpts: FetcherOpts | undefined,
196-
) => {
182+
fetcherOpts?: FetcherOpts,
183+
): Fetcher | void => {
197184
if (options.wsClient) {
198185
return createWebsocketsFetcherFromClient(options.wsClient);
199186
}

packages/graphiql-toolkit/src/graphql-helpers/auto-complete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function getIndentation(str: string, index: number) {
200200
}
201201

202202
function isFieldType(
203-
fieldType: GraphQLOutputType | null | undefined,
203+
fieldType?: GraphQLOutputType | null,
204204
): GraphQLOutputType | void {
205205
if (fieldType) {
206206
return fieldType;

packages/graphiql-toolkit/src/graphql-helpers/operation-name.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { OperationDefinitionNode } from 'graphql';
55
* operations, determine what the next selected operation should be.
66
*/
77
export function getSelectedOperationName(
8-
prevOperations?: OperationDefinitionNode[] | undefined,
8+
prevOperations?: OperationDefinitionNode[],
99
prevSelectedOperationName?: string,
1010
operations?: OperationDefinitionNode[],
1111
) {

packages/graphiql-toolkit/tsconfig.esm.json

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
{
2-
"extends": "../../resources/tsconfig.base.cjs.json",
32
"compilerOptions": {
4-
"rootDir": "./src",
5-
"outDir": "./dist",
6-
"composite": true,
7-
"jsx": "react",
8-
"target": "es5",
9-
"baseUrl": ".",
10-
"strictPropertyInitialization": false
3+
"target": "es2016",
4+
"module": "ESNext",
5+
"declaration": true,
6+
"noEmit": true,
7+
"esModuleInterop": true,
8+
"strict": true,
9+
"skipLibCheck": true,
10+
"allowJs": true,
11+
"lib": ["es2022", "dom"],
12+
"moduleResolution": "node"
1113
},
12-
"include": ["src"],
13-
"exclude": [
14-
"**/__tests__/**",
15-
"**/dist/**.*",
16-
"**/*.spec.ts",
17-
"**/*.spec.js",
18-
"**/*-test.ts",
19-
"**/*-test.js"
20-
]
14+
"include": ["src", "tsup.config.ts"],
15+
"exclude": ["**/*.spec.ts"]
2116
}

0 commit comments

Comments
 (0)