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

type mismatch #89

Open
Code-With-TalhaBhai opened this issue Apr 2, 2023 · 7 comments
Open

type mismatch #89

Code-With-TalhaBhai opened this issue Apr 2, 2023 · 7 comments

Comments

@Code-With-TalhaBhai
Copy link

const handler = startServerAndCreateNextHandler(server, { context: async req => ({ req }) });
In typescript:

--> ApolloServer is not assignable to type ApolloServer

Another thing is,

return handler required two parameters

@martinnabhan
Copy link
Collaborator

I can't seem to replicate. Could you share your code, or even better, a reproduction?

@Code-With-TalhaBhai
Copy link
Author

Here is the code

Screenshot from 2023-04-03 16-23-13

And,
Screenshot from 2023-04-03 16-23-21

@martinnabhan
Copy link
Collaborator

Thank you! The error happens because you're setting request's type to any when it should be either NextRequest or Request.

@Code-With-TalhaBhai
Copy link
Author

I tried both of them, but it didn't work either.

Screenshot from 2023-04-03 23-45-16

@trevor-scheer
Copy link
Member

It looks like your context function returns a NextRequest type. You need to tell ApolloServer what the shape of your context value is by passing the type parameter to it:
new ApolloServer<NextRequest>({ ... })

@ChristianIvicevic
Copy link

ChristianIvicevic commented Apr 4, 2023

I just had the same error and I fixed it by duplicating the context type definition of my server as follows:

                                   // v-- previously always been there
const apolloServer = new ApolloServer<GraphQLContext>({
  // ...
});

                                                          // v-- has to be added explicitly now
const handler = startServerAndCreateNextHandler<NextRequest, GraphQLContext>(apolloServer, {
  context: createGraphQLContext
});

My error message was that ApolloServer<GraphQLContext> cannot be assigned to ApolloServer<object>.

@martinnabhan
Copy link
Collaborator

Thank you! I've managed to reproduce it.

With API Routes it seems you don't need to pass the context type to the handler, but if you're using context with Route Handlers you'll need to specify both the request type and the context type.

Since we're using the same handler for both API Routes and Route Handlers I don't think we can make it so you don't need to pass the context type, but what we can do is add a note to the README explaining it's necessary.

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

4 participants