Skip to content

Commit

Permalink
fix(hapi16_support): add hapi 16 next() invocation [closes #744] (#743)
Browse files Browse the repository at this point in the history
* chore(hapi16_support): add hapi 16 next() invocation

* run lint fix

* update changelog
  • Loading branch information
najeeb-rifaat authored and evans committed Jul 13, 2018
1 parent 8bcb666 commit d3aedd3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 62 additions & 1 deletion packages/apollo-server-hapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 15 additions & 3 deletions packages/apollo-server-hapi/src/hapiApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.');
}
Expand Down Expand Up @@ -75,6 +75,10 @@ const graphqlHapi: IPlugin = {
}
},
});

if (next) {
next();
}
},
};

Expand All @@ -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.');
}
Expand All @@ -111,6 +119,10 @@ const graphiqlHapi: IPlugin = {
return response;
},
});

if (next) {
next();
}
},
};

Expand Down

0 comments on commit d3aedd3

Please sign in to comment.