UNPKG

5.91 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.apolloServer = undefined;
7
8var _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
10exports.default = apolloServer;
11
12var _schemaGenerator = require('./schemaGenerator');
13
14var _mock = require('./mock');
15
16var _expressGraphql = require('express-graphql');
17
18var _expressGraphql2 = _interopRequireDefault(_expressGraphql);
19
20var _graphql = require('graphql');
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24// TODO this implementation could use a bit of refactoring.
25// it turned from a simple function into something promise-based,
26// which means the structure is now quite awkward.
27
28function 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 // Resolve the Options to get OptionsData.
36 return function (req, res) {
37 new Promise(function (resolve) {
38 resolve(typeof options === 'function' ? options(req) : options);
39 }).then(function (optionsData) {
40 // Assert that optionsData is in fact an Object.
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 // Assert that schema is required.
46 if (!optionsData.schema) {
47 throw new Error('GraphQL middleware options must contain a schema.');
48 }
49 var schema = // pass through
50 optionsData.schema;
51 var // required
52 resolvers = optionsData.resolvers;
53 var // required if mocks is not false and schema is not GraphQLSchema
54 connectors = optionsData.connectors;
55 var // required if mocks is not false and schema is not GraphQLSchema
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 // pass through
65 graphiql = _optionsData$graphiql === undefined ? false : _optionsData$graphiql;
66 var // pass through
67 validationRules = optionsData.validationRules;
68 var _optionsData$context = optionsData.context;
69 var // pass through
70 context = _optionsData$context === undefined ? {} : _optionsData$context;
71 var // pass through
72 rootValue = optionsData.rootValue;
73
74 // would collide with formatError from graphql otherwise
75
76 var formatErrorFn = optionsData.formatError;
77
78 var executableSchema = void 0;
79 if (mocks) {
80 // TODO: mocks doesn't yet work with a normal GraphQL schema, but it should!
81 // have to rewrite these functions
82 var myMocks = mocks || {};
83 executableSchema = (0, _schemaGenerator.buildSchemaFromTypeDefinitions)(schema);
84 (0, _mock.addMockFunctionsToSchema)({
85 schema: executableSchema,
86 mocks: myMocks
87 });
88 } else {
89 // this is just basics, makeExecutableSchema should catch the rest
90 // TODO: should be able to provide a GraphQLschema and still use resolvers
91 // and connectors if you want, but at the moment this is not possible.
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 // TODO: test this error
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 // graphQLHTTPOptions
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 // express-graphql catches its own errors, this is just for
138 // errors in Apollo-server.
139 // XXX we should probably care about formatErrorFn and pretty.
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
148exports.apolloServer = apolloServer;
\No newline at end of file