Skip to content

Commit

Permalink
apollo-server-fastify's context function now receives reply object. (#1)
Browse files Browse the repository at this point in the history
- Add applyContextArgs function to format spread args into an object.
  • Loading branch information
HW13 committed Jun 25, 2020
1 parent 753e60c commit 0ea239e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/apollo-server-fastify/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
processFileUploads,
} 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 @@ -170,11 +170,17 @@ export class ApolloServer extends ApolloServerBase {
beforeHandlers.push(fileUploadMiddleware(this.uploadsConfig, this));
}

const graphQLServerOptions = this.graphQLServerOptions.bind(this);
const applyContextArgs = async (
request?: FastifyRequest<IncomingMessage>,
reply?: FastifyReply<OutgoingMessage>,
) => graphQLServerOptions({ request, reply });

instance.route({
method: ['GET', 'POST'],
url: '/',
beforeHandler: beforeHandlers,
handler: await graphqlFastify(this.graphQLServerOptions.bind(this)),
handler: await graphqlFastify(applyContextArgs),
});
},
{
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('applyContextArgs', () => {
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

0 comments on commit 0ea239e

Please sign in to comment.