Skip to content

Commit

Permalink
feat: type Fastify context properly (#5743)
Browse files Browse the repository at this point in the history
Co-authored-by: David Glasser <glasser@davidglasser.net>
  • Loading branch information
SimenB and glasser committed Sep 29, 2021
1 parent 72718a8 commit 164b1d8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
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,
): 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) => {
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';

0 comments on commit 164b1d8

Please sign in to comment.