Skip to content

Fix zeus generated file when using graphql-ws subscriptions and Node #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions packages/graphql-zeus-core/TreeToTS/functions/generated.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Environment } from '@/Models';

export default `const handleFetchResponse = (response: Response): Promise<GraphQLResponse> => {
if (!response.ok) {
return new Promise((_, reject) => {
Expand Down Expand Up @@ -799,13 +801,15 @@ export const $ = <Type extends GraphQLVariableType, Name extends string>(name: N
return (START_VAR_NAME + name + GRAPHQL_TYPE_SEPARATOR + graphqlType) as unknown as Variable<Type, Name>;
};`;

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()),
connectionParams: Object.fromEntries(new Headers(options[1]?.headers).entries()),${
env === 'node' ? '\n webSocketImpl: WebSocket,' : ''
}
});

const ws = new Proxy(
Expand Down Expand Up @@ -889,4 +893,4 @@ export const apiSubscription = (options: chainOptions) => {
throw new Error('No websockets implemented');
}
};`,
};
});
4 changes: 2 additions & 2 deletions packages/graphql-zeus-core/TreeToTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';`
: ``,
),
Expand All @@ -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')
Expand Down