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

Unable to correctly extend and type GraphQL Context #3796

Open
chalupagrande opened this issue Feb 20, 2025 · 0 comments
Open

Unable to correctly extend and type GraphQL Context #3796

chalupagrande opened this issue Feb 20, 2025 · 0 comments

Comments

@chalupagrande
Copy link

Describe the bug

I am unable to correctly type the context in the following code:

const graphqlSchema = createSchema({ typeDefs, resolvers })

interface GraphQLContext extends YogaInitialContext {
  something: number
}

const yoga = createYoga<GraphQLContext>({
  schema: graphqlSchema,
  context: {
    something: 10
  }
});

const server = createServer<(yoga)
server.listen(port, () => {
  console.log(`[server]: Server is running at http://localhost:${port}`);
});

I get the following Typescript Error. Even if I do or do not pass the custom GraphQLContext generic.

Type 'GraphQLResolveInfo & YogaInitialContext' is not assignable to type 'YogaInitialContext & { something: number; }'.
              Property 'something' is missing in type 'GraphQLResolveInfo & YogaInitialContext' but required in type '{ something: number; }'.ts(2322)

Your Example Website or App

https://github.com/chalupagrande/yoga-graphql-server

Steps to Reproduce the Bug or Issue

  1. Checkout the main branch.
  2. Inspect src/server.ts
  3. see Typescript error on line 21

Potentially related issue:

  1. checkout branch issue1
  2. see error when trying to pass yoga to an Express Router intance

Expected behavior

  1. Context would be typed correctly and there would be no errors
  2. I would be able to pass the Yoga instance to the express router as documented here

Screenshots or Videos

Image

Platform

  • OS: MacOS Sanoma 14.5
  • NodeJS: v20.17.0
  • @graphql-yoga/* version(s): "^5.12.0"

Additional context

No response

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

1 participant