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

[BUG] Unable to correctly type graphql context in express server #362

Closed
chalupagrande opened this issue Feb 20, 2025 · 2 comments
Closed

Comments

@chalupagrande
Copy link

chalupagrande commented Feb 20, 2025

Describe the bug
I am unable to correctly type the GraphQL Context in my Yoga instance. I'm attempting to pass my Prisma Client to the context to use on resolvers, but in doing so I am getting a typescript error.

Here is an example git repo. Checkout the README for more replication details
Github Repo

The code of interest:

const graphqlSchema = createSchema({ typeDefs, resolvers })
interface GraphQLContext extends YogaInitialContext {
  prisma: PrismaClient;
}

const yoga = createYoga<GraphQLContext>({
  schema: graphqlSchema,
  context: ({ request }) => {
    return {
      request,
      prisma
    }
  }
});

To Reproduce
Typescript Error:

Type 'GraphQLSchemaWithContext<GraphQLResolveInfo & YogaInitialContext>' is not assignable to type 'YogaSchemaDefinition<GraphQLContext, {}> | undefined'.
  Type 'GraphQLSchemaWithContext<GraphQLResolveInfo & YogaInitialContext>' is not assignable to type 'GraphQLSchemaWithContext<GraphQLContext & YogaInitialContext>'.
    Type 'GraphQLSchemaWithContext<GraphQLResolveInfo & YogaInitialContext>' is not assignable to type '{ _context?: (GraphQLContext & YogaInitialContext) | undefined; }'.
      Types of property '_context' are incompatible.
        Type '(GraphQLResolveInfo & YogaInitialContext) | undefined' is not assignable to type '(GraphQLContext & YogaInitialContext) | undefined'.
          Type 'GraphQLResolveInfo & YogaInitialContext' is not assignable to type '(GraphQLContext & YogaInitialContext) | undefined'.
            Type 'GraphQLResolveInfo & YogaInitialContext' is not assignable to type 'GraphQLContext & YogaInitialContext'.
              Property 'prisma' is missing in type 'GraphQLResolveInfo & YogaInitialContext' but required in type 'GraphQLContext'.ts(2322)
server.ts(15, 3): 'prisma' is declared here.
server.d.cts(59, 5): The expected type comes from property 'schema' which is declared here on type 'YogaServerOptions<GraphQLContext, {}>'

Expected behavior

  • No typescript errors
  • Context would be correctly typed and available in resolvers

Screenshots
Image

Versions

  • Package name and version: "^0.12.0"
@eddeee888
Copy link
Owner

eddeee888 commented Feb 20, 2025

Hi @chalupagrande ,

This error is saying that createSchema doesn't know about prisma, so you'd have to do this:

const graphqlSchema = createSchema<GraphQLContext>({ typeDefs, resolvers })


For Issue 1 - Unable to pass yoga to Express Router
This looks like a GraphQL Yoga issue, could you please ask in https://github.com/dotansimha/graphql-yoga/?


For Issue 2 - Mapper name mismatch not generating matches
This is working as intended. If your file is called schema.graphql, then the mapper file must be called schema.mappers.ts
I think you mentioned documentation issue? Could you help point it out to me? Thanks!

@chalupagrande
Copy link
Author

chalupagrande commented Feb 20, 2025

@eddeee888 - Yeah. Sorry for the confusion. Here is the bit of documentation I feel could use a bit more explanation: https://github.com/eddeee888/graphql-code-generator-plugins/tree/master/packages/typescript-resolver-files#mappersrelativetargetdir

After re-reading it I suppose it does say <schemaName><mappersFileExtension> but I found this confusing. No real change needed. just a suggestion. Closing issue.

For anyone looking here in the future. Here is the graphql-yoga issue: dotansimha/graphql-yoga#3796

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

2 participants