diff --git a/docs/source/api/apollo-server.md b/docs/source/api/apollo-server.md index 7bbc5c61553..9ade4592ab0 100644 --- a/docs/source/api/apollo-server.md +++ b/docs/source/api/apollo-server.md @@ -62,7 +62,7 @@ new ApolloServer({ | Google Cloud Functions | { req: [`Request`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/express-serve-static-core/index.d.ts), res: [`Response`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/express-serve-static-core/index.d.ts#L490-L861) } | | Cloudflare | { req: [`Request`](https://github.com/apollographql/apollo-server/blob/04fe6aa1314ca84de26b4dc26e9b29dda16b81bc/packages/apollo-server-env/src/fetch.d.ts#L37-L45) } | | Express | {
  req: [`express.Request`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/express-serve-static-core/index.d.ts),
  res: [`express.Response`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/express-serve-static-core/index.d.ts#L490-L861)
}
| - | Fastify | {} | + | Fastify | {
  request: [`FastifyRequest`](https://github.com/fastify/fastify/blob/1d4dcf2bcde46256c72e96c2cafc843a461c721e/types/request.d.ts#L15),
  reply: [`FastifyReply`](https://github.com/fastify/fastify/blob/1d4dcf2bcde46256c72e96c2cafc843a461c721e/types/reply.d.ts#L10)
}
| | hapi | {
  request: [`hapi.Request`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/hapi/index.d.ts#L396-L605),
  h: [`hapi.ResponseToolkit`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/hapi/index.d.ts#L979-L1100)
}
| | Koa | { ctx: [`Koa.Context`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/koa/index.d.ts#L724-L731) } | | AWS Lambda | {
  event: [`APIGatewayProxyEvent`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/aws-lambda/index.d.ts#L78-L92),
  context: [`LambdaContext`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/aws-lambda/index.d.ts#L510-L534)
}
| diff --git a/packages/apollo-server-fastify/src/ApolloServer.ts b/packages/apollo-server-fastify/src/ApolloServer.ts index e4e487cbd21..9e8db6d2465 100644 --- a/packages/apollo-server-fastify/src/ApolloServer.ts +++ b/packages/apollo-server-fastify/src/ApolloServer.ts @@ -6,9 +6,10 @@ import { formatApolloErrors, PlaygroundRenderPageOptions, processFileUploads, + GraphQLOptions, } from 'apollo-server-core'; import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'; -import { IncomingMessage, ServerResponse, Server } from 'http'; +import { IncomingMessage, OutgoingMessage, ServerResponse, Server } from 'http'; import { graphqlFastify } from './fastifyApollo'; import { GraphQLOperation } from 'graphql-upload'; @@ -70,6 +71,13 @@ export class ApolloServer extends ApolloServerBase { return true; } + async createGraphQLServerOptions( + request?: FastifyRequest, + reply?: FastifyReply, + ): Promise { + return this.graphQLServerOptions({ request, reply }); + } + public createHandler({ path, cors, @@ -174,7 +182,7 @@ export class ApolloServer extends ApolloServerBase { method: ['GET', 'POST'], url: '/', beforeHandler: beforeHandlers, - handler: await graphqlFastify(this.graphQLServerOptions.bind(this)), + handler: await graphqlFastify(this.createGraphQLServerOptions.bind(this)), }); }, { diff --git a/packages/apollo-server-fastify/src/__tests__/ApolloServer.test.ts b/packages/apollo-server-fastify/src/__tests__/ApolloServer.test.ts index 47f07d95e0a..c11b3cf5343 100644 --- a/packages/apollo-server-fastify/src/__tests__/ApolloServer.test.ts +++ b/packages/apollo-server-fastify/src/__tests__/ApolloServer.test.ts @@ -1,7 +1,7 @@ -import { FastifyInstance } from 'fastify'; +import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'; import fastify from 'fastify'; -import http from 'http'; +import http, { IncomingMessage, OutgoingMessage } from 'http'; import request from 'request'; import FormData from 'form-data'; @@ -56,14 +56,25 @@ describe('apollo-server-fastify', () => { let server: ApolloServer; let app: FastifyInstance; let httpServer: http.Server; + let replyDecorator: jest.Mock | undefined; + let requestDecorator: jest.Mock | undefined; async function createServer( serverOptions: Config, options: Partial = {}, + mockDecorators: boolean = false, ) { server = new ApolloServer(serverOptions); app = fastify(); + if (mockDecorators) { + replyDecorator = jest.fn(); + requestDecorator = jest.fn(); + + app.decorateReply('replyDecorator', replyDecorator); + app.decorateRequest('requestDecorator', requestDecorator); + } + app.register(server.createHandler(options)); await app.listen(port); @@ -82,6 +93,35 @@ describe('apollo-server-fastify', () => { }); }); + describe('createGraphQLServerOptions', () => { + it('provides FastifyRequest and FastifyReply to ContextFunction', async () => { + interface ContextArgs { + request: FastifyRequest & { + requestDecorator: () => any; + }; + reply: FastifyReply & { replyDecorator: () => any }; + } + + const context = ({ request, reply }: ContextArgs) => { + request!.requestDecorator(); + reply!.replyDecorator(); + return {}; + }; + + const { url: uri } = await createServer( + { typeDefs, resolvers, context }, + {}, + true, + ); + + const apolloFetch = createApolloFetch({ uri }); + await apolloFetch({ query: '{hello}' }); + + expect(requestDecorator!.mock.calls.length).toEqual(1); + expect(replyDecorator!.mock.calls.length).toEqual(1); + }); + }); + describe('applyMiddleware', () => { it('can be queried', async () => { const { url: uri } = await createServer({