elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
9.8k stars 208 forks source link

GraphQL Subscription with apollo isn't working #282

Open normanaranez opened 10 months ago

normanaranez commented 10 months ago

GraphQL Subscription isn't working

i tried to use graphql-subscriptions but i don't know what missing here that my subscription isn't running

notes: query and mutation are working as expected just the subscription


import { apollo, gql } from "@elysiajs/apollo";
import { PubSub } from 'graphql-subscriptions';

type Book = {
  title: string;
  author: string;
};

const books: Book[] = [
  {
    title: "Elysia",
    author: "saltyAom",
  },
];

export const pubsub = new PubSub();

const typeDefs = gql`
  type Book {
      title: String
      author: String
    }

    type Query {
      books: [Book]
    }

    type Mutation {
      addBook(title: String, author: String): Book
    }

    type Subscription {
      newBook: Book
    }
`;

const resolvers = {
  Query: {
    books: () => books,
  },
  Mutation: {
    addBook: (_ : any, { title, author }: Book) => {
      const newBook = { title, author };
      books.push(newBook);
      pubsub.publish("NEW_BOOK_ADDED", { newBook });
      return newBook;
    },
  },
  Subscription: {
    newBook: {
      subscribe: () => pubsub.asyncIterator("NEW_BOOK_ADDED"),
    },
  },
};

const app = new Elysia()
  .use(
    apollo({
      typeDefs: typeDefs,
      resolvers: resolvers
    })
  )
  .listen(8080);

console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);
MYKEU commented 9 months ago

Did you ever get this working @normanaranez ?

kamalkech commented 1 month ago

the contributor completely slow, i am not sure if still use elysiajs for next projects

kravetsone commented 1 month ago

the contributor completely slow, i am not sure if still use elysiajs for next projects

its very sadly... but i agree