Skip to content
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

Unable to create subscription using GraphQLWebSocketClient #432

Closed
kpostekk opened this issue Jan 25, 2023 · 6 comments
Closed

Unable to create subscription using GraphQLWebSocketClient #432

kpostekk opened this issue Jan 25, 2023 · 6 comments

Comments

@kpostekk
Copy link

tl;dr
The current implementation of graphql-transport-ws in this library has a bug where initial flag is not sent.

Accroding to PR #330 I should be able to establish a WS connection with any GraphQL server that supports graphql-transport-ws.
Unfortunately, there is no mention in docs how to do it using the GraphQLWebSocketClient class.
Anyway, I have tried using one of my subscriptions from Mercurius server with following code.

const socket = new WebSocket('ws://localhost:3000/graphql')
socket.addEventListener('open', () => {
  const why = new GraphQLWebSocketClient(socket, {})
  why.rawSubscribe<SpeedSubscription>(
    `subscription {
      state {
        speed
      }
    }`,
    {
      error: (err) => console.error('gql-req', { err }),
      next: (data) => console.log('gql-req', { data }),
      complete: () => console.log('complete'),
    }
  )
})

Instead of continous communication, socket was immediately closed.
image
image

Implementing identical logic in graphql-ws was working as it should

const c = createClient({
  url: 'ws://localhost:3000/graphql',
})
c.subscribe(
  {
    query: `subscription {
      state {
        speed
      }
    }`,
  },
  {
    error: (err) => console.error('gql-ws', { err }),
    next: (data) => console.log('gql-ws', { data }),
    complete: () => console.log('complete'),
  }
)

image
image

I think implementation of graphql-transport-ws in previously mentioned PR has lack of initial flag.

@lyubo
Copy link
Contributor

lyubo commented Mar 6, 2023

@kpostekk Please see recently added test file for a working subscription example

@kpostekk
Copy link
Author

kpostekk commented Mar 6, 2023

Gonna test it asap

@lyubo
Copy link
Contributor

lyubo commented Mar 6, 2023

Gonna test it asap
It is browsable in PR #474, you can try to run it from my fork, but I think just looking at source would be enough.

@kpostekk
Copy link
Author

kpostekk commented Mar 6, 2023

After receiving "connection_ack" code does not send a query to server.

My implementation

export function useStateSubscription() {
  const config = useConfig()
  const socket = useMemo(
    () => new WebSocket(config.stateStreamer, 'graphql-transport-ws'),
    [config.stateStreamer]
  )
  const client = useMemo(() => new GraphQLWebSocketClient(socket, {}), [socket])

  useEffect(() => {
    if (socket.readyState !== WebSocket.OPEN) return
    const unsubscribe = client.rawSubscribe<
      RealtimeContentSubscription,
      RealtimeContentSubscriptionVariables
    >(RealtimeContentDocument, {
      next: (data) => console.log({ data }),
      complete: () => console.log('completed (gqlr)')
    })
    return unsubscribe
  }, [config.stateStreamer])
}

Received:
image
I think it's worth to mention the fact that connection is not closed, because server is wating for query.

Expected:
image

@kpostekk
Copy link
Author

kpostekk commented Mar 6, 2023

I also don't exclude bugs from my side. I think not including docs for WS isn't helpful tho.

@jasonkuhrt
Copy link
Member

jasonkuhrt commented Mar 11, 2023

@kpostekk PR adding docs for web socket stuff is welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants