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

Error "ReadableStream has already been used" in ElysiaJS with Apollo plugin #1057

Open
baranozdemir1 opened this issue Feb 9, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@baranozdemir1
Copy link

baranozdemir1 commented Feb 9, 2025

What version of Elysia is running?

latest

What platform is your computer?

Darwin 24.3.0 arm64 arm

What steps can reproduce the bug?

I'm encountering an error while trying to run a simple project with the ElysiaJS framework and the Apollo plugin. After setting up the project as described in the official documentation and starting the server using bun run dev, I get the following error:

$ bun run --watch src/index.ts
🦊 Elysia is running at localhost:3003
233 |           (response as Response).status !== set.status &&
234 |           set.status !== 200 &&
235 |           ((response.status as number) <= 300 ||
236 |                   (response.status as number) > 400)
237 |   )
238 |           response = new Response(response.body, {
                    ^
error: ReadableStream has already been used
    at mergeResponseWithSetHeaders (/Users/baran/projects/test/app/node_modules/elysia/src/adapter/web-standard/handler.ts:238:14)
    at mapResponse (/Users/baran/projects/test/app/node_modules/elysia/src/adapter/web-standard/handler.ts:324:16)
    at createNativeStaticHandler (/Users/baran/projects/test/app/node_modules/elysia/src/adapter/bun/handler.ts:25:19)
    at add (/Users/baran/projects/test/app/node_modules/elysia/src/index.ts:730:15)
    at get (/Users/baran/projects/test/app/node_modules/elysia/src/index.ts:4061:8)
    at <anonymous> (/Users/baran/projects/test/app/node_modules/@elysiajs/apollo/dist/index.mjs:33:11)

Steps to Reproduce:

  1. Set up a new project using ElysiaJS and Apollo plugin following the official documentation.
  2. Use the following code in index.ts to set up ElysiaJS with Apollo:
import { Elysia } from "elysia";
import { apollo, gql } from '@elysiajs/apollo'
import { cors } from '@elysiajs/cors'
import { typeDefsGql, resolversGql } from "./graphql";

const app = new Elysia()
    .use(
        cors({
            origin: 'http://localhost'
        })
    )
    .use(
        apollo({
            typeDefs: typeDefsGql,
            resolvers: resolversGql,
            path: "/graphql-api"
        })
    )
    .get('/', () => 'hi')
    .listen(3003)

console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);
  1. Use the following GraphQL setup in graphql.ts:
import { apollo, gql } from "@elysiajs/apollo";
import { Article, PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const typeDefsGql = gql`
type User {
    id: String
    name: String
    email: String
    password: String
    file_avatar: String
    created_at: String
    updated_at: String
    deleted_at: String
    articles: [Article]
}

type Article {
    id: ID
    name: String
    content: String
    user_id: ID
}

input ArticleInput {
    name: String
    content: String
    user_id: ID
}

type Query {
    getUsers: [User]
    getArticles: [Article]
    getUserByName(name: String): [User]
}

type Mutation {
    createArticle(article: ArticleInput!): Article
    deleteArticle(id: String!): Article
}
`;

const resolversGql = {
Query: {
    getUsers: async () => {
    return prisma.user.findMany({
        include: {
        articles: true,
        },
    });
    },
    getArticles: async () => {
    return prisma.article.findMany({
        include: {
        user: true,
        },
    });
    },
    getUserByName: async (_parent: any, { name }: { name: string }) => {
    return prisma.user.findMany({
        where: {
        name: {
            contains: name,
            mode: "insensitive",
        },
        },
    });
    },
},
Mutation: {
    createArticle: async (_parent: any, { article }: { article: Article }) => {
    const { name, content, user_id } = article;
    return prisma.article.create({
        data: { name, content, user_id },
    });
    },
    deleteArticle: async (_parent: any, { id }: { id: string }) => {
    return prisma.article.delete({
        where: { id: id },
    });
    },
},
};

export { typeDefsGql, resolversGql };
  1. Run the server with bun run dev.
  2. The error is thrown, and the server crashes with the message: "ReadableStream has already been used."

What is the expected behavior?

The server should start successfully and handle requests without any errors, including handling GraphQL queries and mutations via the Apollo plugin.

What do you see instead?

The server fails to start with the error:

error: ReadableStream has already been used

Additional information

  • I have tried starting the server with both bun run dev and bun run --watch src/index.ts, but the error persists.
  • I am using a clean and minimal setup based on the ElysiaJS documentation and following the instructions provided.

package.json:

{
  "name": "app",
  "version": "1.0.50",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "bun run --watch src/index.ts"
  },
  "dependencies": {
    "@apollo/server": "^4.11.3",
    "@elysiajs/apollo": "^1.2.0",
    "@elysiajs/cors": "^1.2.0",
    "@prisma/client": "^6.3.1",
    "elysia": "latest",
    "graphql": "^16.10.0"
  },
  "devDependencies": {
    "bun-types": "latest",
    "prisma": "^6.3.1"
  },
  "module": "src/index.js"
}

Have you try removing the node_modules and bun.lockb and try again yet?

Yes

@baranozdemir1 baranozdemir1 added the bug Something isn't working label Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant