oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.05k stars 2.67k forks source link

Express with GraphQl Yoga throwing internal server error #6554

Open filipmilo opened 11 months ago

filipmilo commented 11 months ago

What version of Bun is running?

1.0.6+969da088f5db3258a803ec186012e30f992829b4

What platform is your computer?

Darwin 22.6.0 arm64 arm

What steps can reproduce the bug?

This configuration, express with graphql yoga should work right out of the box. When trying to access the graphiql playground on route '/graphql' internal server error is thrown without any logs inside the console. The same configuration works when running with node.

import express, { Request, Response, Router } from "express";
import { createYoga } from 'graphql-yoga'
import cors from 'cors';
import { makeExecutableSchema } from "@graphql-tools/schema";

const port = process.env.PORT || 4000;

class Application {
  private host: express.Application;

  constructor() {
    this.host = express();
    this.setup();
  }

  private setup() {
    this.host.use(cors());
    this.host.get("/hi", (req: Request, res: Response) => { res.send("Hello World!") })
    this.host.use(this.buildYoga());

    this.host.use((error: Error, req: express.Request, res: express.Response, next: express.NextFunction): void => {
      console.error('πŸ“Œ Something went wrong', error);
      res.status(400).send(error);
    });
  }

  private buildYoga(): Router {
    const yoga = createYoga({
      schema: makeExecutableSchema({
        typeDefs: "
          type Query {
            greetings: String
          }
        ",
        resolvers: {
          Query: {
            greetings: () => 'Hello from Yoga in a Bun app!'
          }
        }
      }),
      graphiql: true,
    })

    return express.Router().use("/graphql", yoga);
  }

  public start() {
    this.host.listen(port, () => {
      console.log(`πŸš€ Started listening on port:${port}`);
    });
  }
}

const application = new Application();

try {
  application.start();
} catch (e) {
  console.error(e);
}

What is the expected behavior?

We should enter the playground without an error.

What do you see instead?

Internal server error (500) is thrown.

Additional information

No response

filipmilo commented 10 months ago

I have created a repro example repository

github-actions[bot] commented 1 month ago

This issue is stale and may be closed due to inactivity. If you're still running into this, please leave a comment.