Skip to content

Add operation name to subscribe message #440

Answered by enisdenjo
tomas-light asked this question in Q&A
Discussion options

You must be logged in to vote

Similar to #398.

I wouldn't add a new field to the subscribe message, I'd recommend instead using the generateID client option to ease up lookups during development. The argument receives the operation payload so you can construct whatever you want.

You can add something random to disambiguate the IDs during development. Roughly:

import { createClient } from 'graphql-ws';

const client = createClient({
  url: 'ws://dev-gen-id:4000/graphql',
  generateID: ({ operationName }) =>
    // operationName plus a random 4 character hash
    `${operationName}_${Math.random().toString(36).slice(-4)}`,
});

Do note that the GraphQL over WebSocket Protocol disallows multiple active operations with the …

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by enisdenjo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #438 on January 30, 2023 09:56.