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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The version headers in this history reflect the versions of Apollo Server itself
## vNEXT

- `apollo-server-koa`: The peer dependency on `koa` (added in v3.0.0) should be a `^` range dependency rather than depending on exactly one version, and it should not be automatically increased when new versions of `koa` are released. [PR #5759](https://github.com/apollographql/apollo-server/pull/5759)
- `apollo-server-fastify`: Export `ApolloServerFastifyConfig` and `FastifyContext` TypeScript types. [PR #5743](https://github.com/apollographql/apollo-server/pull/5743)

## v3.3.0

Expand Down
21 changes: 16 additions & 5 deletions packages/apollo-server-fastify/src/ApolloServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ApolloServerBase,
Config,
convertNodeHttpToRequest,
GraphQLOptions,
isHttpQueryError,
Expand All @@ -17,6 +18,13 @@ export interface ServerRegistration {
disableHealthCheck?: boolean;
}

export interface FastifyContext {
request: FastifyRequest;
reply: FastifyReply;
}

export type ApolloServerFastifyConfig = Config<FastifyContext>;

const stringifyHealthCheck = fastJson({
type: 'object',
properties: {
Expand All @@ -26,12 +34,15 @@ const stringifyHealthCheck = fastJson({
},
});

export class ApolloServer extends ApolloServerBase {
export class ApolloServer<
ContextFunctionParams = FastifyContext,
> extends ApolloServerBase<ContextFunctionParams> {
async createGraphQLServerOptions(
request?: FastifyRequest,
reply?: FastifyReply,
request: FastifyRequest,
reply: FastifyReply,
Comment on lines -31 to +42
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),

): Promise<GraphQLOptions> {
return this.graphQLServerOptions({ request, reply });
const contextParams: FastifyContext = { request, reply };
return this.graphQLServerOptions(contextParams);
}

public createHandler({
Expand Down Expand Up @@ -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

handler: async (request, reply) => {
try {
const { graphqlResponse, responseInit } = await runHttpQuery(
[],
Expand Down
7 changes: 6 additions & 1 deletion packages/apollo-server-fastify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ export {
} from 'apollo-server-core';

// ApolloServer integration.
export { ApolloServer, ServerRegistration } from './ApolloServer';
export {
ApolloServer,
ApolloServerFastifyConfig,
FastifyContext,
ServerRegistration,
} from './ApolloServer';