Skip to content

Commit 4e58a0e

Browse files
authored
fix(ws): fix missing websockets config on CLI (#1417)
1 parent af39c3d commit 4e58a0e

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

src/interfaces.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ export interface PostGraphileOptions<
6969
ownerConnectionString?: string;
7070
// Enable GraphQL websocket transport support for subscriptions (you still need a subscriptions plugin currently)
7171
subscriptions?: boolean;
72-
// Choose which websocket transport libraries to use. Use commas to define multiple. Defaults to '[v0, v1]' if `--subscriptions` was passed, '[]' otherwise
72+
// [EXPERIMENTAL] Enables live-query support via GraphQL subscriptions (sends updated payload any time nested collections/records change)
73+
live?: boolean;
74+
// Choose which websocket transport libraries to use. Use commas to define multiple. Defaults to `['v0', 'v1']` if `subscriptions` or `live` are true, `[]` otherwise
7375
websockets?: ('v0' | 'v1')[];
7476
// Toggle which GraphQL websocket transport operations are supported: 'subscriptions' or 'all'. Defaults to `subscriptions`
7577
websocketOperations?: 'all' | 'subscriptions';
76-
// [EXPERIMENTAL] Enables live-query support via GraphQL subscriptions (sends updated payload any time nested collections/records change)
77-
live?: boolean;
7878
// [EXPERIMENTAL] If you're using websockets (subscriptions || live) then you
7979
// may want to authenticate your users using sessions or similar. You can
8080
// pass some simple middlewares here that will be executed against the

src/postgraphile/cli.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ program
122122
)
123123
.option(
124124
'--websockets <string>',
125-
"Choose which websocket transport libraries to use. Use commas to define multiple. Defaults to '[v0, v1]' if `--subscriptions` was passed, '[]' otherwise",
125+
"Choose which websocket transport libraries to use. Use commas to define multiple. Defaults to 'v0,v1' if `--subscriptions` or `--live` were passed, '[]' otherwise",
126126
(option: string) => option.split(','),
127127
)
128128
.option(
@@ -435,8 +435,8 @@ const {
435435
connection: pgConnectionString,
436436
ownerConnection,
437437
subscriptions,
438-
websockets = subscriptions ? ['v0', 'v1'] : [],
439438
live,
439+
websockets = subscriptions || live ? ['v0', 'v1'] : [],
440440
watch: watchPg,
441441
schema: dbSchema,
442442
host: hostname = 'localhost',
@@ -695,6 +695,7 @@ const postgraphileOptions = pluginHook(
695695
retryOnInitFail,
696696
pgDefaultRole,
697697
subscriptions: subscriptions || live,
698+
websockets,
698699
live,
699700
watchPg,
700701
showErrorStack,

src/postgraphile/http/createPostGraphileHttpRequestHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export default function createPostGraphileHttpRequestHandler(
184184
watchPg,
185185
disableQueryLog,
186186
enableQueryBatching,
187-
websockets = subscriptions ? ['v0', 'v1'] : [],
187+
websockets = options.subscriptions || options.live ? ['v0', 'v1'] : [],
188188
} = options;
189189
const live = !!options.live;
190190
const enhanceGraphiql =

src/postgraphile/http/subscriptions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ export async function enhanceHttpServerWithWebSockets<
7373
const graphqlRoute =
7474
(subscriptionServerOptions && subscriptionServerOptions.graphqlRoute) ||
7575
(options.externalUrlBase || '') + (options.graphqlRoute || '/graphql');
76+
const { subscriptions, live, websockets = subscriptions || live ? ['v0', 'v1'] : [] } = options;
7677

7778
// enhance with WebSockets shouldnt be called if there are no websocket versions
78-
if (!options.websockets?.length) {
79-
throw new Error(`Invalid value for \`websockets\` option: '${options.websockets}'`);
79+
if (!websockets?.length) {
80+
throw new Error(`Invalid value for \`websockets\` option: '${JSON.stringify(websockets)}'`);
8081
}
8182

8283
const schema = await getGraphQLSchema();
@@ -185,7 +186,7 @@ export async function enhanceHttpServerWithWebSockets<
185186
let socketId = 0;
186187

187188
let v0Wss: WebSocket.Server | null = null;
188-
if (options.websockets.includes('v0')) {
189+
if (websockets.includes('v0')) {
189190
v0Wss = new WebSocket.Server({ noServer: true });
190191
SubscriptionServer.create(
191192
{
@@ -324,7 +325,7 @@ export async function enhanceHttpServerWithWebSockets<
324325
}
325326

326327
let v1Wss: WebSocket.Server | null = null;
327-
if (options.websockets.includes('v1')) {
328+
if (websockets.includes('v1')) {
328329
v1Wss = new WebSocket.Server({ noServer: true });
329330
useServer(
330331
{

0 commit comments

Comments
 (0)