From b49125a7d0ae92a34b6913ad6683172b8d23b8ee Mon Sep 17 00:00:00 2001 From: Najeeb Rifaat Date: Wed, 3 Jan 2018 14:55:38 +0700 Subject: [PATCH 1/3] chore(hapi16_support): add hapi 16 next() invocation --- packages/apollo-server-hapi/README.md | 63 ++++++++++++++++++- packages/apollo-server-hapi/src/hapiApollo.ts | 16 ++++- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/packages/apollo-server-hapi/README.md b/packages/apollo-server-hapi/README.md index 3c980b26966..61dbeb080de 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 8e0737fdd05..3c9d4a4def2 100644 --- a/packages/apollo-server-hapi/src/hapiApollo.ts +++ b/packages/apollo-server-hapi/src/hapiApollo.ts @@ -4,7 +4,7 @@ import * as GraphiQL from 'apollo-server-module-graphiql'; import { GraphQLOptions, runHttpQuery, HttpQueryError } from 'apollo-server-core'; export interface IRegister { - (server: Server, options: any): void; + (server: Server, options: any, next?: Function): void; } export interface IPlugin { @@ -13,6 +13,7 @@ export interface IPlugin { register: IRegister; } + export interface HapiOptionsFunction { (req?: Request): GraphQLOptions | Promise; } @@ -26,7 +27,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.'); } @@ -71,6 +72,10 @@ const graphqlHapi: IPlugin = { } }, }); + + if (next) { + next(); + } }, }; @@ -86,7 +91,7 @@ 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.'); } @@ -103,7 +108,12 @@ const graphiqlHapi: IPlugin = { return response; }, }); + + if (next) { + next(); + } }, }; export { graphqlHapi, graphiqlHapi }; + From 8d4204597d14cebafcd95381d1a7e7416e3b873a Mon Sep 17 00:00:00 2001 From: Evans Hauser Date: Fri, 13 Jul 2018 11:23:10 -0700 Subject: [PATCH 2/3] run lint fix --- packages/apollo-server-hapi/README.md | 82 +++++++++---------- packages/apollo-server-hapi/src/hapiApollo.ts | 10 ++- 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/packages/apollo-server-hapi/README.md b/packages/apollo-server-hapi/README.md index b3ffd14649c..14747dc9a21 100644 --- a/packages/apollo-server-hapi/README.md +++ b/packages/apollo-server-hapi/README.md @@ -56,61 +56,61 @@ 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 + name: graphqlHapi.name, }; // if you happen to use graphiql then add attributes for hapi 16 graphiqlHapi.attributes = { - name: graphiqlHapi.name + 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}`); + 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(); diff --git a/packages/apollo-server-hapi/src/hapiApollo.ts b/packages/apollo-server-hapi/src/hapiApollo.ts index b5516a20179..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, next?: Function): void; + (server: Server, options: any, next?: Function): void; } export interface IPlugin { @@ -17,7 +17,6 @@ export interface IPlugin { register: IRegister; } - export interface HapiOptionsFunction { (req?: Request): GraphQLOptions | Promise; } @@ -95,7 +94,11 @@ export interface HapiGraphiQLPluginOptions { const graphiqlHapi: IPlugin = { name: 'graphiql', - register: (server: Server, options: HapiGraphiQLPluginOptions, next?: Function) => { + register: ( + server: Server, + options: HapiGraphiQLPluginOptions, + next?: Function, + ) => { if (!options || !options.graphiqlOptions) { throw new Error('Apollo Server GraphiQL requires options.'); } @@ -124,4 +127,3 @@ const graphiqlHapi: IPlugin = { }; export { graphqlHapi, graphiqlHapi }; - From 468b42d9bf3ca44bf54c64f7dead7c9ff16873a8 Mon Sep 17 00:00:00 2001 From: Evans Hauser Date: Fri, 13 Jul 2018 11:25:21 -0700 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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