Skip to content

Graphql-ws implementation error in nextjs? #3216

Answered by kitten
siamahnaf asked this question in Q&A
Discussion options

You must be logged in to vote

tl;dr, don't do this:

subscriptionExchange({
    forwardSubscription(request) {
        const input = { ...request, query: request.query || '' };
        return {
            subscribe(sink) {
                const unsubscribe = wsClient?.subscribe(input, sink);
                return { unsubscribe };
            },
        };
    },
}),

That'll make unsubscribe undefined when wsClient is undefined, and TypeScript will (rightfully) yell at you, as you've seen.

Instead, exclude the entire exchange if you don't need it, e.g.:

client = new Client({
  // ...
  exchanges: [
    // ...
    wsClient ? subscriptionExchange({
        forwardSubscription(request) {
            const input = { ...re…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@siamahnaf
Comment options

@kitten
Comment options

Answer selected by siamahnaf
@siamahnaf
Comment options

@velidan
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants