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

Bun + Typescript type mismatch: Argument of type 'YogaServerInstance<{}, {}>' is not assignable to parameter of type 'Serve<unknown>' #3003

Closed
Tracked by #2700 ...
d6u opened this issue Sep 18, 2023 · 4 comments
Assignees

Comments

@d6u
Copy link

d6u commented Sep 18, 2023

Describe the bug

I'm following the instruction to create a minimum GraphQL server locally https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-bun

import { createSchema, createYoga } from "graphql-yoga";

const yoga = createYoga({
  schema: createSchema<{}>({
    typeDefs: /* GraphQL */ `
      type Query {
        greetings: String
      }
    `,
    resolvers: {
      Query: {
        greetings: () => "Hello from Yoga in a Bun app!",
      },
    },
  }),
});

const server = Bun.serve(yoga);

console.info(
  `Server is running on ${new URL(
    yoga.graphqlEndpoint,
    `http://${server.hostname}:${server.port}`
  )}`
);

It runs successfully and I can see graphiql UI at http://localhost:3000/graphql

However, I'm encountering this type error when performing typescript type checking:

index.ts:18:26 - error TS2345: Argument of type 'YogaServerInstance<{}, {}>' is not assignable to parameter of type 'Serve<unknown>'.
  Type 'YogaServerInstance<{}, {}>' is not assignable to type 'UnixTLSWebSocketServeOptions<unknown>'.

18 const server = Bun.serve(yoga);
                            ~~~~


Found 1 error in index.ts:18

Your Example Website or App

https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-bun

Steps to Reproduce the Bug or Issue

  1. Install Bun by following https://bun.sh/docs/installation
  2. Create a new directory and bun init
  3. bun add graphql-yoga graphql typescript
  4. Copy the example code from https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-bun to index.ts
  5. bun run index.ts should work just fine
  6. tsc will show the error.

Expected behavior

I expect there is no TS errors.

Screenshots or Videos

No response

Platform

  • macOS
  • Bun 1.0.2
  • graphql-yoga 4.0.4, typescript 5.2.2

Additional context

Thanks!

@EmrysMyrddin
Copy link
Collaborator

It seems the types has changed on 1.0.0 release.
A workaround is to give an option object to Bun.serve instead of the yoga instance directly. This also allows you to give other options like development or tls if this is something you need.

Bun.serve({
  fetch: yoga
})

@EmrysMyrddin
Copy link
Collaborator

This seems to be fixed now

This was referenced May 7, 2024
This was referenced May 23, 2024
@DuckThom
Copy link

I just ran into an issue similar to this one after updating some packages to the latest (stable) versions

src/index.ts:121:13 - error TS2322: Type 'YogaServerInstance<{}, GlobalContext>' is not assignable to type '((this: Server, request: Request, server: Server) => Response | Promise<Response>) | ((this: Server, request: Request, server: Server) => Response | Promise<...>) | ((this: Server, request: Request, server: Server) => void | ... 2 more ... | undefined) | ((this: Server, request: Request, server: Server) => Response ...'.
  Type 'YogaServerInstance<{}, GlobalContext>' is not assignable to type '(this: Server, request: Request, server: Server) => Response | Promise<Response>'.
    Types of parameters 'res' and 'server' are incompatible.
      Type 'Server' is not assignable to type 'NodeResponse'.

121             fetch: yoga,
                ~~~~~

  node_modules/bun-types/bun.d.ts:2425:3
    2425   fetch(
           ~~~~~~
    2426    this: Server,
         ~~~~~~~~~~~~~~~~
     ... 
    2428    server: Server,
         ~~~~~~~~~~~~~~~~~~
    2429   ): Response | Promise<Response>;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The expected type comes from property 'fetch' which is declared here on type 'Serve<unknown>'

Changing the following resolved this error for me:

         const server = Bun.serve({
             port: config.server.port,
             hostname: config.server.hostname,
-            fetch: yoga,
+            fetch: (request) => yoga(request),
         })

@mdugue
Copy link

mdugue commented Aug 8, 2024

thanks @DuckThom , same issue here. Could be fixed with you change! I will add a comment to https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-bun

yamcodes added a commit to yamcodes/graphql-yoga that referenced this issue Aug 16, 2024
Fix a TypeScript issue resulting from a breaking change in either Bun or Yoga. 

The error is detailed here:
- dotansimha#3003 (comment)
- dotansimha#2644 (comment)
- dotansimha#3003 (comment)

This PR fixes the error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants