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

feat: type Fastify context properly #5743

Merged
merged 2 commits into from
Sep 29, 2021
Merged

Conversation

SimenB
Copy link
Contributor

@SimenB SimenB commented Sep 22, 2021

This way a context function provided to new ApolloServer is typed correctly.

I copied this approach from the Express plugin:

export interface ExpressContext {
req: express.Request;
res: express.Response;
}
export type ApolloServerExpressConfig = Config<ExpressContext>;
export class ApolloServer<
ContextFunctionParams = ExpressContext,
> extends ApolloServerBase<ContextFunctionParams> {
// This translates the arguments from the middleware into graphQL options It
// provides typings for the integration specific behavior, ideally this would
// be propagated with a generic to the super class
async createGraphQLServerOptions(
req: express.Request,
res: express.Response,
): Promise<GraphQLOptions> {
const contextParams: ExpressContext = { req, res };
return super.graphQLServerOptions(contextParams);
}

Comment on lines -31 to +42
request?: FastifyRequest,
reply?: FastifyReply,
request: FastifyRequest,
reply: FastifyReply,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they're not actually optional:

this.createGraphQLServerOptions(request, reply),

@@ -103,7 +114,7 @@ export class ApolloServer extends ApolloServerBase {
method: ['GET', 'POST'],
url: '/',
preHandler,
handler: async (request: FastifyRequest, reply: FastifyReply) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already typed

@glasser
Copy link
Member

glasser commented Sep 28, 2021

This looks good. It should get a CHANGELOG.md entry mentioning the new type exports; we'll merge it once that is added, or will write it ourselves eventually.

@glasser glasser merged commit 164b1d8 into apollographql:main Sep 29, 2021
@SimenB SimenB deleted the fastify-context branch September 29, 2021 07:27
@SimenB
Copy link
Contributor Author

SimenB commented Sep 29, 2021

Thanks @glasser!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants