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

apollo-server-fastify's context function now receives reply object. #3895

Merged
merged 1 commit into from
Jul 15, 2020
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
2 changes: 1 addition & 1 deletion docs/source/api/apollo-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ new ApolloServer({
| Google Cloud Functions | <code>{ 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) }</code> |
| Cloudflare | <code>{ req: [`Request`](https://github.com/apollographql/apollo-server/blob/04fe6aa1314ca84de26b4dc26e9b29dda16b81bc/packages/apollo-server-env/src/fetch.d.ts#L37-L45) }</code> |
| Express | <code>{<br/>&nbsp;&nbsp;req: [`express.Request`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/express-serve-static-core/index.d.ts),<br/>&nbsp;&nbsp;res: [`express.Response`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/express-serve-static-core/index.d.ts#L490-L861)<br/>}</code> |
| Fastify | <code>{}</code> |
| Fastify | <code>{<br/>&nbsp;&nbsp;request: [`FastifyRequest`](https://github.com/fastify/fastify/blob/1d4dcf2bcde46256c72e96c2cafc843a461c721e/types/request.d.ts#L15),<br/>&nbsp;&nbsp;reply: [`FastifyReply`](https://github.com/fastify/fastify/blob/1d4dcf2bcde46256c72e96c2cafc843a461c721e/types/reply.d.ts#L10)</br>}</code> |
| hapi | <code>{<br/>&nbsp;&nbsp;request: [`hapi.Request`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/hapi/index.d.ts#L396-L605),<br/>&nbsp;&nbsp;h: [`hapi.ResponseToolkit`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/hapi/index.d.ts#L979-L1100)<br/>}</code> |
| Koa | <code>{ ctx: [`Koa.Context`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/koa/index.d.ts#L724-L731) }</code> |
| AWS Lambda | <code>{<br/>&nbsp;&nbsp;event: [`APIGatewayProxyEvent`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/aws-lambda/index.d.ts#L78-L92),<br/>&nbsp;&nbsp;context: [`LambdaContext`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/50adc95acf873e714256074311353232fcc1b5ed/types/aws-lambda/index.d.ts#L510-L534)<br/>}</code> |
Expand Down
12 changes: 10 additions & 2 deletions packages/apollo-server-fastify/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -70,6 +71,13 @@ export class ApolloServer extends ApolloServerBase {
return true;
}

async createGraphQLServerOptions(
request?: FastifyRequest<IncomingMessage>,
reply?: FastifyReply<OutgoingMessage>,
): Promise<GraphQLOptions> {
return this.graphQLServerOptions({ request, reply });
}

public createHandler({
path,
cors,
Expand Down Expand Up @@ -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)),
});
},
{
Expand Down
44 changes: 42 additions & 2 deletions packages/apollo-server-fastify/src/__tests__/ApolloServer.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<ServerRegistration> = {},
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);

Expand All @@ -82,6 +93,35 @@ describe('apollo-server-fastify', () => {
});
});

describe('createGraphQLServerOptions', () => {
it('provides FastifyRequest and FastifyReply to ContextFunction', async () => {
interface ContextArgs {
request: FastifyRequest<IncomingMessage> & {
requestDecorator: () => any;
};
reply: FastifyReply<OutgoingMessage> & { 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({
Expand Down