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

Subscriptions with elysia graphql yoga don't work #12

Open
snigdha920 opened this issue Jul 21, 2024 · 9 comments
Open

Subscriptions with elysia graphql yoga don't work #12

snigdha920 opened this issue Jul 21, 2024 · 9 comments

Comments

@snigdha920
Copy link

snigdha920 commented Jul 21, 2024

I needed subscriptions in my project and in the end had to make a different server for graphql yoga because graphql-ws subscriptions wouldn't work with Elysia.

The main issues are:

  1. Elysia websocket handlers have a separate type than bun websocket handlers, so this example from the graphql ws library doesn't work out of the box: https://the-guild.dev/graphql/ws/get-started#with-bun
  2. The handler from the example for uWebSocket.js satisfies the type, but functionally also doesn't work, I think that's designed for uWebSocket servers, and not bun servers.
  3. GraphQL SSE also doesn't quite work, in all the examples for GraphQL SSE with the graphql-sse client, the url that the client connects to is /graphql/stream, a URL that elysia graphql yoga don't expose: https://the-guild.dev/graphql/sse/recipes#client-usage

Because of all these limitations, I had to literally create a separate server and then handle metrics collection, logs collection, etc. for that server again.

I think this is something important, and shouldn't be too hard to add? I'm going to attempt a PR for this @SaltyAom I will try to make the websocket handlers work first, then expose the /graphql/stream url and make it work

Related to: #11

@kamalkech
Copy link

????

@bogeychan
Copy link
Contributor

bogeychan commented Jul 26, 2024

graphql-mobius needs an update to support subscriptions types. SaltyAom/mobius#6

you can do this instead as a workaround (don't forget to bun add graphql-yoga):

import { Elysia } from "elysia";
import { yoga } from "@elysiajs/graphql-yoga";
import { createSchema } from "graphql-yoga";

const schema = createSchema({
  typeDefs: `
    type Query {
      hello: String
    }

    type Subscription {
      countdown(from: Int!): Int!
    }
  `,
  resolvers: {
    Query: {
      hello: () => "yay",
    },
    Subscription: {
      countdown: {
        subscribe: async function* (_, { from }) {
          for (let i = from; i >= 0; i--) {
            await Bun.sleep(1000);
            yield { countdown: i };
          }
        },
      },
    },
  },
});

new Elysia()
  .use(
    yoga({
      // @ts-ignore
      schema,
    })
  )
  .listen(5000);

http://localhost:5000/graphql?query=subscription+%7B%0A++countdown%28from%3A+5%29%0A%7D

@kamalkech
Copy link

kamalkech commented Jul 26, 2024

@bogeychan that not working

WebSocket connection to 'ws://localhost:3333/graphql' failed:

@bogeychan
Copy link
Contributor

@kamalkech works for me, use latest versions:

├── @elysiajs/[email protected]
├── [email protected]
├── [email protected]
curl -N -H "accept:text/event-stream" http://localhost:5000/graphql?query=subscription%20%7B%0A%20%20countdown%28from%3A%205%29%0A%7D

image

@bogeychan
Copy link
Contributor

bogeychan commented Jul 26, 2024

@kamalkech you have to pass WS option if you wanna use WebSockets:

yoga({
  // @ts-ignore
  schema,
  graphiql: {
    // Use WebSockets in GraphiQL
    subscriptionsProtocol: "WS",
  },
})

https://the-guild.dev/graphql/yoga-server/docs/features/subscriptions#graphql-over-websocket-protocol-via-graphql-ws

@bogeychan
Copy link
Contributor

bogeychan commented Jul 26, 2024

nvm, @elysiajs/graphql-yoga doesn't support WS at the moment, you'll have to use SSE for now

#14

@kamalkech
Copy link

@bogeychan how use sse with yoga on elysiajs server instead of yoga server

@snigdha920
Copy link
Author

snigdha920 commented Aug 4, 2024

nvm, @elysiajs/graphql-yoga doesn't support WS at the moment, you'll have to use SSE for now

#14

Even SSE doesn't work, I tried to use it with EventSource, as mentioned in the example here:
https://the-guild.dev/graphql/sse/recipes#client-usage

But no events are sent. All the examples use a /graphql/stream endpoint, which could be why. It is a different endpoint that is not configured in the plugin:

return app

@kamalkech
Copy link

@snigdha920 any piste ?

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