1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const debug = require('debug')('huncwot:graphql');
|
15 |
|
16 | const Promise = require('bluebird');
|
17 | const fs = Promise.promisifyAll(require('fs-extra'));
|
18 | const path = require('path');
|
19 | const assert = require('assert');
|
20 | const mime = require('mime-types');
|
21 |
|
22 | const { OK, html } = require('./response.js');
|
23 |
|
24 | const { runHttpQuery } = require('apollo-server-core');
|
25 | const { resolveGraphiQLString } = require('apollo-server-module-graphiql');
|
26 | const { makeExecutableSchema } = require('graphql-tools');
|
27 |
|
28 | function graphql(options) {
|
29 | return async request => {
|
30 | let method = request.request.method;
|
31 | let query = request.params;
|
32 | let { graphqlResponse, responseInit } = await runHttpQuery([], {
|
33 | method,
|
34 | options,
|
35 | query
|
36 | });
|
37 |
|
38 | return OK(graphqlResponse);
|
39 | };
|
40 | }
|
41 |
|
42 | function graphiql(options) {
|
43 | return async request => {
|
44 | let query = request.params;
|
45 | let response = await resolveGraphiQLString(query, options, request);
|
46 |
|
47 | return html(response);
|
48 | };
|
49 | }
|
50 |
|
51 | module.exports = {
|
52 | graphiql,
|
53 | graphql,
|
54 | makeSchema: makeExecutableSchema
|
55 | };
|