mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 10:11:40 -05:00
apollo-server-hapi: update README to reflect Apollo-server 2 changes
This commit is contained in:
parent
6b80ac96a1
commit
e10bf1d590
1 changed files with 17 additions and 26 deletions
|
@ -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();
|
StartServer().catch(error => console.log(e));
|
||||||
```
|
```
|
||||||
|
|
||||||
## Principles
|
## Principles
|
||||||
|
|
Loading…
Add table
Reference in a new issue