From 0b76e9ef00021ab3263cfc03b46dfc797c07619d Mon Sep 17 00:00:00 2001 From: Max Nachlinger Date: Fri, 27 Jul 2018 16:49:55 -0700 Subject: [PATCH] Hapi: Pass toolkit to context function (#1407) * Fix for #1382 * fix typo in changelog * Added a few tests proving the response toolkit is passed into the context function --- CHANGELOG.md | 1 + .../src/ApolloServer.test.ts | 27 +++++++++++++++++++ packages/apollo-server-hapi/src/hapiApollo.ts | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a68c6d2f7f..b1c20bcb9e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All of the packages in the `apollo-server` repo are released with the same versi ### vNEXT +- Hapi: Pass the response toolkit to the context function. [#1407](https://github.com/apollographql/apollo-server/pull/1407) - update apollo-engine-reporting-protobuf to non-beta [#1429](https://github.com/apollographql/apollo-server/pull/1429) ### rc.10 diff --git a/packages/apollo-server-hapi/src/ApolloServer.test.ts b/packages/apollo-server-hapi/src/ApolloServer.test.ts index 6b51b5d4c8c..8140fd7bae6 100644 --- a/packages/apollo-server-hapi/src/ApolloServer.test.ts +++ b/packages/apollo-server-hapi/src/ApolloServer.test.ts @@ -223,6 +223,33 @@ describe('apollo-server-hapi', () => { await apolloFetch({ query: '{hello}' }); }); + it('passes each request and response toolkit through to the context function', async () => { + const context = async ({ request, h }) => { + expect(request, 'request argument should exist').to.exist; + expect(h, 'response toolkit argument should exist').to.exist; + return {}; + }; + + server = new ApolloServer({ + typeDefs, + resolvers, + context, + }); + app = new Server({ port: 4000 }); + + await server.applyMiddleware({ app }); + await app.start(); + + httpServer = app.listener; + const uri = app.info.uri + '/graphql'; + + const apolloFetch = createApolloFetch({ uri }); + const result = await apolloFetch({ query: '{hello}' }); + + expect(result.data).to.deep.equal({ hello: 'hi' }); + expect(result.errors, 'errors should exist').not.to.exist; + }); + describe('healthchecks', () => { afterEach(async () => { await server.stop(); diff --git a/packages/apollo-server-hapi/src/hapiApollo.ts b/packages/apollo-server-hapi/src/hapiApollo.ts index b338f8e6e76..08377632084 100644 --- a/packages/apollo-server-hapi/src/hapiApollo.ts +++ b/packages/apollo-server-hapi/src/hapiApollo.ts @@ -41,7 +41,7 @@ const graphqlHapi: IPlugin = { handler: async (request, h) => { try { const { graphqlResponse, responseInit } = await runHttpQuery( - [request], + [request, h], { method: request.method.toUpperCase(), options: options.graphqlOptions,