Skip to content

Commit

Permalink
migrate @graphiql/toolkit to tsup (#3746)
Browse files Browse the repository at this point in the history
* aa

* removes unneeded `undefined`

* try

* fix

* fix vitest
  • Loading branch information
dimaMachina authored Aug 23, 2024
1 parent 7275c19 commit 2ad4e75
Show file tree
Hide file tree
Showing 18 changed files with 497 additions and 126 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-ghosts-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphiql/toolkit': minor
---

compile with `tsup` instead of `tsc`
10 changes: 2 additions & 8 deletions packages/graphiql-toolkit/docs/create-fetcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ const url = 'https://my-schema.com/graphql';

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

const fetcher = createGraphiQLFetcher({
url,
subscriptionUrl,
});
const fetcher = createGraphiQLFetcher({ url, subscriptionUrl });

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

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

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

const fetcher = createGraphiQLFetcher({
url,
fetch,
});
const fetcher = createGraphiQLFetcher({ url, fetch });

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

Expand Down
16 changes: 11 additions & 5 deletions packages/graphiql-toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
"url": "https://github.com/graphql/graphiql/issues?q=issue+label:@graphiql/toolkit"
},
"license": "MIT",
"main": "dist/index.js",
"module": "esm/index.js",
"typings": "dist/index.d.ts",
"scripts": {},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"typings": "dist/esm/index.d.mts",
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"prebuild": "yarn types:check",
"types:check": "tsc --noEmit"
},
"dependencies": {
"@n1ru4l/push-pull-async-iterable-iterator": "^3.1.0",
"meros": "^1.1.4"
Expand All @@ -27,7 +32,8 @@
"graphql": "^17.0.0-alpha.7",
"graphql-ws": "^5.5.5",
"isomorphic-fetch": "^3.0.0",
"subscriptions-transport-ws": "0.11.0"
"subscriptions-transport-ws": "0.11.0",
"tsup": "^8.2.4"
},
"peerDependencies": {
"graphql": "^15.5.0 || ^16.0.0 || ^17.0.0-alpha.2",
Expand Down
15 changes: 3 additions & 12 deletions packages/graphiql-toolkit/src/create-fetcher/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,21 @@ describe('getWsFetcher', () => {
});
it('provides an observable wsClient when custom wsClient option is provided', () => {
createClient.mockReturnValue(true);
getWsFetcher({
url: '',
wsClient: true,
});
getWsFetcher({ url: '', wsClient: true });
// @ts-ignore
expect(createClient.mock.calls).toHaveLength(0);
});
it('creates a subscriptions-transports-ws observable when custom legacyClient option is provided', () => {
createClient.mockReturnValue(true);
getWsFetcher({
url: '',
legacyClient: true,
});
getWsFetcher({ url: '', legacyClient: true });
// @ts-ignore
expect(createClient.mock.calls).toHaveLength(0);
expect(SubscriptionClient.mock.calls).toHaveLength(0);
});

it('if subscriptionsUrl is provided, create a client on the fly', () => {
createClient.mockReturnValue(true);
getWsFetcher({
url: '',
subscriptionUrl: 'wss://example',
});
getWsFetcher({ url: '', subscriptionUrl: 'wss://example' });
expect(createClient.mock.calls[0]).toEqual([
{ connectionParams: {}, url: 'wss://example' },
]);
Expand Down
20 changes: 4 additions & 16 deletions packages/graphiql-toolkit/src/create-fetcher/createFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,15 @@ import {
* build a GraphiQL fetcher that is:
* - backwards compatible
* - optionally supports graphql-ws or `
*
* @param options {CreateFetcherOptions}
* @returns {Fetcher}
*/
export function createGraphiQLFetcher(options: CreateFetcherOptions): Fetcher {
let httpFetch;
if (typeof window !== 'undefined' && window.fetch) {
httpFetch = window.fetch;
}
if (
options?.enableIncrementalDelivery === null ||
options.enableIncrementalDelivery !== false
) {
options.enableIncrementalDelivery = true;
}
if (options.fetch) {
httpFetch = options.fetch;
}
const httpFetch =
options.fetch || (typeof window !== 'undefined' && window.fetch);
if (!httpFetch) {
throw new Error('No valid fetcher implementation available');
}
options.enableIncrementalDelivery =
options.enableIncrementalDelivery !== false;
// simpler fetcher for schema requests
const simpleFetcher = createSimpleFetcher(options, httpFetch);

Expand Down
29 changes: 8 additions & 21 deletions packages/graphiql-toolkit/src/create-fetcher/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const errorHasCode = (err: unknown): err is { code: string } => {
*/
export const isSubscriptionWithName = (
document: DocumentNode,
name: string | undefined,
name?: string,
): boolean => {
let isSubscription = false;
visit(document, {
Expand Down Expand Up @@ -79,10 +79,7 @@ export const createWebsocketsFetcherFromUrl = (
};

// TODO: defaults?
wsClient = createClient({
url,
connectionParams,
});
wsClient = createClient({ url, connectionParams });
return createWebsocketsFetcherFromClient(wsClient);
} catch (err) {
if (errorHasCode(err) && err.code === 'MODULE_NOT_FOUND') {
Expand All @@ -97,12 +94,10 @@ export const createWebsocketsFetcherFromUrl = (

/**
* Create ws/s fetcher using provided wsClient implementation
*
* @param wsClient {Client}
* @returns {Fetcher}
*/
export const createWebsocketsFetcherFromClient =
(wsClient: Client) => (graphQLParams: FetcherParams) =>
(wsClient: Client): Fetcher =>
(graphQLParams: FetcherParams) =>
makeAsyncIterableIteratorFromSink<ExecutionResult>(sink =>
wsClient.subscribe(graphQLParams, {
...sink,
Expand All @@ -125,12 +120,9 @@ export const createWebsocketsFetcherFromClient =
/**
* Allow legacy websockets protocol client, but no definitions for it,
* as the library is deprecated and has security issues
*
* @param legacyWsClient
* @returns
*/
export const createLegacyWebsocketsFetcher =
(legacyWsClient: { request: (params: FetcherParams) => unknown }) =>
(legacyWsClient: { request: (params: FetcherParams) => unknown }): Fetcher =>
(graphQLParams: FetcherParams) => {
const observable = legacyWsClient.request(graphQLParams);
return makeAsyncIterableIteratorFromSink<ExecutionResult>(
Expand All @@ -139,11 +131,8 @@ export const createLegacyWebsocketsFetcher =
);
};
/**
* create a fetcher with the `IncrementalDelivery` HTTP/S spec for
* Create a fetcher with the `IncrementalDelivery` HTTP/S spec for
* `@stream` and `@defer` support using `fetch-multipart-graphql`
*
* @param options {CreateFetcherOptions}
* @returns {Fetcher}
*/
export const createMultipartFetcher = (
options: CreateFetcherOptions,
Expand Down Expand Up @@ -187,13 +176,11 @@ export const createMultipartFetcher = (

/**
* If `wsClient` or `legacyClient` are provided, then `subscriptionUrl` is overridden.
* @param options {CreateFetcherOptions}
* @returns
*/
export const getWsFetcher = (
options: CreateFetcherOptions,
fetcherOpts: FetcherOpts | undefined,
) => {
fetcherOpts?: FetcherOpts,
): Fetcher | void => {
if (options.wsClient) {
return createWebsocketsFetcherFromClient(options.wsClient);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function getIndentation(str: string, index: number) {
}

function isFieldType(
fieldType: GraphQLOutputType | null | undefined,
fieldType?: GraphQLOutputType | null,
): GraphQLOutputType | void {
if (fieldType) {
return fieldType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { OperationDefinitionNode } from 'graphql';
* operations, determine what the next selected operation should be.
*/
export function getSelectedOperationName(
prevOperations?: OperationDefinitionNode[] | undefined,
prevOperations?: OperationDefinitionNode[],
prevSelectedOperationName?: string,
operations?: OperationDefinitionNode[],
) {
Expand Down
20 changes: 0 additions & 20 deletions packages/graphiql-toolkit/tsconfig.esm.json

This file was deleted.

29 changes: 12 additions & 17 deletions packages/graphiql-toolkit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
{
"extends": "../../resources/tsconfig.base.cjs.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"composite": true,
"jsx": "react",
"target": "es5",
"baseUrl": ".",
"strictPropertyInitialization": false
"target": "es2016",
"module": "ESNext",
"declaration": true,
"noEmit": true,
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"allowJs": true,
"lib": ["es2022", "dom"],
"moduleResolution": "node"
},
"include": ["src"],
"exclude": [
"**/__tests__/**",
"**/dist/**.*",
"**/*.spec.ts",
"**/*.spec.js",
"**/*-test.ts",
"**/*-test.js"
]
"include": ["src", "tsup.config.ts"],
"exclude": ["**/*.spec.ts"]
}
22 changes: 22 additions & 0 deletions packages/graphiql-toolkit/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineConfig, Options } from 'tsup';

const opts: Options = {
entry: ['src/**/*.ts', '!**/__tests__'],
bundle: false,
clean: true,
dts: true,
};

export default defineConfig([
{
...opts,
format: 'esm',
outDir: 'dist/esm',
outExtension: () => ({ js: '.js' }),
},
{
...opts,
format: 'cjs',
outDir: 'dist/cjs',
},
]);
31 changes: 31 additions & 0 deletions packages/graphiql/test/beforeDevServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2021 GraphQL Contributors.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const express = require('express');
const path = require('node:path');
// eslint-disable-next-line import-x/no-extraneous-dependencies
const { createHandler } = require('graphql-http/lib/use/express');
const schema = require('./schema');
const badSchema = require('../cypress/fixtures/bad-schema.json');

module.exports = function beforeDevServer(app, _server, _compiler) {
// GraphQL Server
app.post('/graphql', createHandler({ schema }));
app.get('/graphql', createHandler({ schema }));

app.post('/bad/graphql', (_req, res, next) => {
res.json({ data: badSchema });
next();
});

app.use('/images', express.static(path.join(__dirname, 'images')));

app.use(
'/resources/renderExample.js',
express.static(path.join(__dirname, '../resources/renderExample.js')),
);
};
3 changes: 0 additions & 3 deletions packages/graphiql/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
{
"path": "../codemirror-graphql"
},
{
"path": "../graphiql-toolkit"
},
{
"path": "../graphql-language-service"
}
Expand Down
3 changes: 0 additions & 3 deletions packages/graphiql/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"include": ["src"],
"exclude": ["**/__tests__/**", "**/dist/**.*", "cypress/**"],
"references": [
{
"path": "../graphiql-toolkit"
},
{
"path": "../codemirror-graphql"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/monaco-graphql/test/monaco-editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('monaco-editor', () => {
// expect(lines[1]).toMatch(' building for production...');
// expect(lines[2]).toBe('transforming...');
expect(lines[3]).toMatch(
`✓ ${parseInt(version, 10) > 16 ? 856 : 843} modules transformed.`,
`✓ ${parseInt(version, 10) > 16 ? 857 : 843} modules transformed.`,
);
// expect(lines[4]).toBe('rendering chunks...');
// expect(lines[5]).toBe('computing gzip size...');
Expand Down
3 changes: 0 additions & 3 deletions resources/tsconfig.build.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
"files": [],
"include": [],
"references": [
{
"path": "../packages/graphiql-toolkit"
},
{
"path": "../packages/monaco-graphql"
},
Expand Down
3 changes: 0 additions & 3 deletions resources/tsconfig.build.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
"files": [],
"include": [],
"references": [
{
"path": "../packages/graphiql-toolkit/tsconfig.esm.json"
},
{
"path": "../packages/cm6-graphql/tsconfig.esm.json"
},
Expand Down
Loading

0 comments on commit 2ad4e75

Please sign in to comment.