fix(hapi16_support): add hapi 16 next() invocation [closes #744] (#743)

* chore(hapi16_support): add hapi 16 next() invocation

* run lint fix

* update changelog
This commit is contained in:
Najeeb Rifaat 2018-07-14 02:30:47 +08:00 committed by Evans Hauser
parent 8bcb66622d
commit d3aedd31e6
3 changed files with 78 additions and 4 deletions

View file

@ -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

View file

@ -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:

View file

@ -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();
}
},
};