apollo-server-hapi: update README to reflect Apollo-server 2 changes

This commit is contained in:
Evans Hauser 2018-05-10 22:12:40 -07:00
parent 6b80ac96a1
commit e10bf1d590
No known key found for this signature in database
GPG key ID: 88AF586817F52EEC

View file

@ -13,46 +13,37 @@ npm install apollo-server-hapi
## Usage ## Usage
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. After constructing Apollo server, a hapi server can be enabled with a call to `registerServer`. Ensure that `autoListen` is set to false in the `Hapi.server` constructor.
The code below requires Hapi 17 or higher. The code below requires Hapi 17 or higher.
```js ```js
import Hapi from 'hapi'; const Hapi = require('hapi');
import { graphqlHapi } from 'apollo-server-hapi'; const { ApolloServer } = require('apollo-server');
const { registerServer } = require('apollo-server-hapi');
const HOST = 'localhost'; const HOST = 'localhost';
const PORT = 3000;
async function StartServer() { async function StartServer() {
const server = new Hapi.server({ const server = new ApolloServer({ typeDefs, resolvers });
//Note: autoListen is required, since Apollo Server will start the listener
const app = new Hapi.server({
autoListen: false,
host: HOST, host: HOST,
port: PORT,
}); });
await server.register({ //apply other plugins
plugin: graphqlHapi,
options: { await registerServer({ server, app });
path: '/graphql',
graphqlOptions: { //port is optional and defaults to 4000
schema: myGraphQLSchema, server.listen({ port: 4000 }).then(({ url }) => {
}, console.log(`🚀 Server ready at ${url}`);
route: {
cors: true,
},
},
}); });
try {
await server.start();
} catch (err) {
console.log(`Error while starting server: ${err.message}`);
} }
console.log(`Server running at: ${server.info.uri}`); StartServer().catch(error => console.log(e));
}
StartServer();
``` ```
## Principles ## Principles