From d3aedd31e6443566aba6a4222504c095c393985a Mon Sep 17 00:00:00 2001 From: Najeeb Rifaat Date: Sat, 14 Jul 2018 02:30:47 +0800 Subject: [PATCH] fix(hapi16_support): add hapi 16 next() invocation [closes #744] (#743) * chore(hapi16_support): add hapi 16 next() invocation * run lint fix * update changelog --- CHANGELOG.md | 1 + packages/apollo-server-hapi/README.md | 63 ++++++++++++++++++- packages/apollo-server-hapi/src/hapiApollo.ts | 18 +++++- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0157bbc139..0a6646b0ebd 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 +* add hapi 16 next() invocation [PR #743](https://github.com/apollographql/apollo-server/pull/743) * Add skipValidation option [PR #839](https://github.com/apollographql/apollo-server/pull/839) * `apollo-server-module-graphiql`: adds an option to the constructor to disable url rewriting when editing a query [PR #1047](https://github.com/apollographql/apollo-server/pull/1047) * Upgrade `subscription-transport-ws` to 0.9.9 for Graphiql diff --git a/packages/apollo-server-hapi/README.md b/packages/apollo-server-hapi/README.md index 2df55c5787f..14747dc9a21 100644 --- a/packages/apollo-server-hapi/README.md +++ b/packages/apollo-server-hapi/README.md @@ -15,7 +15,7 @@ npm install apollo-server-hapi With the Hapi plugins `graphqlHapi` and `graphiqlHapi` you can pass a route object that includes options to be applied to the route. The example below enables CORS on the `/graphql` route. -The code below requires Hapi 17 or higher. +The code below requires Hapi 17, See below for Hapi 16 support. ```js import Hapi from 'hapi'; @@ -55,6 +55,67 @@ async function StartServer() { StartServer(); ``` +## Hapi 16 + +Imports must be monkey patched for Hapi 16 support (mainly attributes). + +```js +import { graphqlHapi, graphiqlHapi } from 'apollo-server-hapi'; + +// add attributes for hapi 16 +graphqlHapi.attributes = { + name: graphqlHapi.name, +}; + +// if you happen to use graphiql then add attributes for hapi 16 +graphiqlHapi.attributes = { + name: graphiqlHapi.name, +}; + +async function StartServer() { + const server = new Hapi.server({ + host: HOST, + port: PORT, + }); + + await server.register({ + plugin: graphqlHapi, + options: { + path: '/graphql', + graphqlOptions: { + schema: myGraphQLSchema, + }, + route: { + cors: true, + }, + }, + }); + + await server.register({ + plugin: graphiqlHapi, + options: { + path: '/graphiql', + route: { + cors: true, + }, + graphiqlOptions: { + endpointURL: 'graphql', + }, + }, + }); + + try { + await server.start(); + } catch (err) { + console.log(`Error while starting server: ${err.message}`); + } + + console.log(`Server running at: ${server.info.uri}`); +} + +StartServer(); +``` + ## Principles Apollo Server is built with the following principles in mind: diff --git a/packages/apollo-server-hapi/src/hapiApollo.ts b/packages/apollo-server-hapi/src/hapiApollo.ts index 22e0e6bcff3..7e14de1018d 100644 --- a/packages/apollo-server-hapi/src/hapiApollo.ts +++ b/packages/apollo-server-hapi/src/hapiApollo.ts @@ -8,7 +8,7 @@ import { } from 'apollo-server-core'; export interface IRegister { - (server: Server, options: any): void; + (server: Server, options: any, next?: Function): void; } export interface IPlugin { @@ -30,7 +30,7 @@ export interface HapiPluginOptions { const graphqlHapi: IPlugin = { name: 'graphql', - register: (server: Server, options: HapiPluginOptions) => { + register: (server: Server, options: HapiPluginOptions, next?: Function) => { if (!options || !options.graphqlOptions) { throw new Error('Apollo Server requires options.'); } @@ -75,6 +75,10 @@ const graphqlHapi: IPlugin = { } }, }); + + if (next) { + next(); + } }, }; @@ -90,7 +94,11 @@ export interface HapiGraphiQLPluginOptions { const graphiqlHapi: IPlugin = { name: 'graphiql', - register: (server: Server, options: HapiGraphiQLPluginOptions) => { + register: ( + server: Server, + options: HapiGraphiQLPluginOptions, + next?: Function, + ) => { if (!options || !options.graphiqlOptions) { throw new Error('Apollo Server GraphiQL requires options.'); } @@ -111,6 +119,10 @@ const graphiqlHapi: IPlugin = { return response; }, }); + + if (next) { + next(); + } }, };