1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.apolloServer = undefined;
|
7 |
|
8 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
|
9 |
|
10 | exports.default = apolloServer;
|
11 |
|
12 | var _schemaGenerator = require('./schemaGenerator');
|
13 |
|
14 | var _mock = require('./mock');
|
15 |
|
16 | var _expressGraphql = require('express-graphql');
|
17 |
|
18 | var _expressGraphql2 = _interopRequireDefault(_expressGraphql);
|
19 |
|
20 | var _graphql = require('graphql');
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function apolloServer(options) {
|
29 | if (!options) {
|
30 | throw new Error('GraphQL middleware requires options.');
|
31 | }
|
32 | if (arguments.length - 1 > 0) {
|
33 | throw new Error('apolloServer expects exactly one argument, got ' + (arguments.length - 1 + 1));
|
34 | }
|
35 |
|
36 | return function (req, res) {
|
37 | new Promise(function (resolve) {
|
38 | resolve(typeof options === 'function' ? options(req) : options);
|
39 | }).then(function (optionsData) {
|
40 |
|
41 | if (!optionsData || (typeof optionsData === 'undefined' ? 'undefined' : _typeof(optionsData)) !== 'object') {
|
42 | throw new Error('GraphQL middleware option function must return an options object.');
|
43 | }
|
44 |
|
45 |
|
46 | if (!optionsData.schema) {
|
47 | throw new Error('GraphQL middleware options must contain a schema.');
|
48 | }
|
49 | var schema =
|
50 | optionsData.schema;
|
51 | var
|
52 | resolvers = optionsData.resolvers;
|
53 | var
|
54 | connectors = optionsData.connectors;
|
55 | var
|
56 | logger = optionsData.logger;
|
57 | var printErrors = optionsData.printErrors;
|
58 | var _optionsData$mocks = optionsData.mocks;
|
59 | var mocks = _optionsData$mocks === undefined ? false : _optionsData$mocks;
|
60 | var _optionsData$allowUnd = optionsData.allowUndefinedInResolve;
|
61 | var allowUndefinedInResolve = _optionsData$allowUnd === undefined ? true : _optionsData$allowUnd;
|
62 | var pretty = optionsData.pretty;
|
63 | var _optionsData$graphiql = optionsData.graphiql;
|
64 | var
|
65 | graphiql = _optionsData$graphiql === undefined ? false : _optionsData$graphiql;
|
66 | var
|
67 | validationRules = optionsData.validationRules;
|
68 | var _optionsData$context = optionsData.context;
|
69 | var
|
70 | context = _optionsData$context === undefined ? {} : _optionsData$context;
|
71 | var
|
72 | rootValue = optionsData.rootValue;
|
73 |
|
74 |
|
75 |
|
76 | var formatErrorFn = optionsData.formatError;
|
77 |
|
78 | var executableSchema = void 0;
|
79 | if (mocks) {
|
80 |
|
81 |
|
82 | var myMocks = mocks || {};
|
83 | executableSchema = (0, _schemaGenerator.buildSchemaFromTypeDefinitions)(schema);
|
84 | (0, _mock.addMockFunctionsToSchema)({
|
85 | schema: executableSchema,
|
86 | mocks: myMocks
|
87 | });
|
88 | } else {
|
89 |
|
90 |
|
91 |
|
92 | if (schema instanceof _graphql.GraphQLSchema) {
|
93 | if (logger) {
|
94 | (0, _schemaGenerator.addErrorLoggingToSchema)(schema, logger);
|
95 | }
|
96 | if (printErrors) {
|
97 | (0, _schemaGenerator.addErrorLoggingToSchema)(schema, { log: function log(e) {
|
98 | return console.error(e.stack);
|
99 | } });
|
100 | }
|
101 | if (!allowUndefinedInResolve) {
|
102 | (0, _schemaGenerator.addCatchUndefinedToSchema)(schema);
|
103 | }
|
104 | executableSchema = schema;
|
105 | } else {
|
106 | if (!resolvers) {
|
107 |
|
108 | throw new Error('resolvers is required option if mocks is not provided');
|
109 | }
|
110 | executableSchema = (0, _schemaGenerator.makeExecutableSchema)({
|
111 | typeDefs: schema,
|
112 | resolvers: resolvers,
|
113 | connectors: connectors,
|
114 | logger: logger,
|
115 | allowUndefinedInResolve: allowUndefinedInResolve
|
116 | });
|
117 | if (printErrors) {
|
118 | (0, _schemaGenerator.addErrorLoggingToSchema)(executableSchema, { log: function log(e) {
|
119 | return console.error(e.stack);
|
120 | } });
|
121 | }
|
122 | }
|
123 | }
|
124 |
|
125 | return {
|
126 | schema: executableSchema,
|
127 | pretty: pretty,
|
128 | formatError: formatErrorFn,
|
129 | validationRules: validationRules,
|
130 | context: context,
|
131 | rootValue: rootValue,
|
132 | graphiql: graphiql
|
133 | };
|
134 | }).then(function (graphqlHTTPOptions) {
|
135 | return (0, _expressGraphql2.default)(graphqlHTTPOptions)(req, res);
|
136 | }).catch(function (error) {
|
137 |
|
138 |
|
139 |
|
140 | res.status(error.status || 500);
|
141 | var result = { errors: [error] };
|
142 | result.errors = result.errors.map(_graphql.formatError);
|
143 | res.set('Content-Type', 'application/json').send(JSON.stringify(result));
|
144 | });
|
145 | };
|
146 | }
|
147 |
|
148 | exports.apolloServer = apolloServer; |
\ | No newline at end of file |