-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexample.js
48 lines (36 loc) · 1.06 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
const Hapi = require('@hapi/hapi');
const Graphi = require('.');
const internals = {};
const schema = `
type Person {
firstname: String!
lastname: String!
}
type Query {
person(firstname: String!): Person!
}
`;
const getPerson = function(args, request) {
return new Promise((resolve) => {
resolve({ firstname: 'billy', lastname: 'jean' });
});
};
const resolvers = {
person: getPerson
};
internals.init = async () => {
const server = new Hapi.Server({ port: 8000 });
await server.register({ plugin: Graphi, options: { schema, resolvers } });
await server.start();
return server;
};
internals.init()
.then((server) => {
console.log('server.info.uri ' + server.info.uri);
// open http://localhost:8000/graphiql?query=%7B%20person(firstname%3A%20%22billy%22)%20%7B%20lastname%20%7D%20%7D&variables=%7B%7D
// curl -X POST -H "Content-Type: application/json" -d '{"query":"{person(firstname:\"billy\"){lastname}}"}' http://127.0.0.1:8000/graphql
})
.catch((error) => {
console.log('Error: ' + error);
});