From b33c12cbd6ea01defaf0ec27f820a4d20ccff04b Mon Sep 17 00:00:00 2001 From: Ghislain Thau Date: Fri, 16 Feb 2024 18:31:00 +0100 Subject: [PATCH 1/2] Fix generated code for Node when graphql-ws subscriptions is enabled Explanation: - according to graphql-ws recipes (https://the-guild.dev/graphql/ws/recipes), using `createClient` on Node requires to explicitly pass the WebSocket implementation, contrary to the browser. - when using Node, we want to import the type definition for Headers from 'node-fetch', if not the type definition from TS lib DOM is used, and it raises an Error when typechecking with tsc. --- packages/graphql-zeus-core/TreeToTS/functions/generated.ts | 1 + packages/graphql-zeus-core/TreeToTS/index.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/graphql-zeus-core/TreeToTS/functions/generated.ts b/packages/graphql-zeus-core/TreeToTS/functions/generated.ts index c46f2582..a025af40 100644 --- a/packages/graphql-zeus-core/TreeToTS/functions/generated.ts +++ b/packages/graphql-zeus-core/TreeToTS/functions/generated.ts @@ -806,6 +806,7 @@ export const apiSubscription = (options: chainOptions) => { const client = createClient({ url: String(options[0]), connectionParams: Object.fromEntries(new Headers(options[1]?.headers).entries()), + webSocketImpl: WebSocket, }); const ws = new Proxy( diff --git a/packages/graphql-zeus-core/TreeToTS/index.ts b/packages/graphql-zeus-core/TreeToTS/index.ts index 663cd0ed..0f3cc230 100644 --- a/packages/graphql-zeus-core/TreeToTS/index.ts +++ b/packages/graphql-zeus-core/TreeToTS/index.ts @@ -110,7 +110,7 @@ export class TreeToTS { }';`.concat( env === 'node' ? ` -import fetch, { Response } from 'node-fetch'; +import fetch, {${subscriptions === 'graphql-ws' ? ' Headers,' : ''} Response } from 'node-fetch'; import WebSocket from 'ws';` : ``, ), From 321b5740c75c893e290a12a6645ca075466e5337 Mon Sep 17 00:00:00 2001 From: Ghislain Thau Date: Fri, 16 Feb 2024 18:39:52 +0100 Subject: [PATCH 2/2] webSocketImpl should be passed to createClient only when env=='node' --- .../graphql-zeus-core/TreeToTS/functions/generated.ts | 11 +++++++---- packages/graphql-zeus-core/TreeToTS/index.ts | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/graphql-zeus-core/TreeToTS/functions/generated.ts b/packages/graphql-zeus-core/TreeToTS/functions/generated.ts index a025af40..cfbc5752 100644 --- a/packages/graphql-zeus-core/TreeToTS/functions/generated.ts +++ b/packages/graphql-zeus-core/TreeToTS/functions/generated.ts @@ -1,3 +1,5 @@ +import type { Environment } from '@/Models'; + export default `const handleFetchResponse = (response: Response): Promise => { if (!response.ok) { return new Promise((_, reject) => { @@ -799,14 +801,15 @@ export const $ = (name: N return (START_VAR_NAME + name + GRAPHQL_TYPE_SEPARATOR + graphqlType) as unknown as Variable; };`; -export const subscriptionFunctions = { +export const subscriptionFunctions = (env: Environment) => ({ 'graphql-ws': `import { createClient, type Sink } from 'graphql-ws'; // keep export const apiSubscription = (options: chainOptions) => { const client = createClient({ url: String(options[0]), - connectionParams: Object.fromEntries(new Headers(options[1]?.headers).entries()), - webSocketImpl: WebSocket, + connectionParams: Object.fromEntries(new Headers(options[1]?.headers).entries()),${ + env === 'node' ? '\n webSocketImpl: WebSocket,' : '' + } }); const ws = new Proxy( @@ -890,4 +893,4 @@ export const apiSubscription = (options: chainOptions) => { throw new Error('No websockets implemented'); } };`, -}; +}); diff --git a/packages/graphql-zeus-core/TreeToTS/index.ts b/packages/graphql-zeus-core/TreeToTS/index.ts index 0f3cc230..25dcea48 100644 --- a/packages/graphql-zeus-core/TreeToTS/index.ts +++ b/packages/graphql-zeus-core/TreeToTS/index.ts @@ -120,7 +120,7 @@ import WebSocket from 'ws';` .concat('\n') .concat(headers ? `export const HEADERS = ${JSON.stringify(headers)}` : '\n\nexport const HEADERS = {}') .concat('\n') - .concat(subscriptionFunctions[subscriptions]) + .concat(subscriptionFunctions(env)[subscriptions]) .concat('\n') .concat(typescriptFunctions) .concat('\n')