UNPKG

2.04 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var tslib = require('tslib');
6var graphql = require('graphql');
7var core = require('../core');
8var utilities = require('../../utilities');
9
10var SchemaLink = (function (_super) {
11 tslib.__extends(SchemaLink, _super);
12 function SchemaLink(options) {
13 var _this = _super.call(this) || this;
14 _this.schema = options.schema;
15 _this.rootValue = options.rootValue;
16 _this.context = options.context;
17 _this.validate = !!options.validate;
18 return _this;
19 }
20 SchemaLink.prototype.request = function (operation) {
21 var _this = this;
22 return new utilities.Observable(function (observer) {
23 new Promise(function (resolve) { return resolve(typeof _this.context === 'function'
24 ? _this.context(operation)
25 : _this.context); }).then(function (context) {
26 if (_this.validate) {
27 var validationErrors = graphql.validate(_this.schema, operation.query);
28 if (validationErrors.length > 0) {
29 return { errors: validationErrors };
30 }
31 }
32 return graphql.execute({
33 schema: _this.schema,
34 document: operation.query,
35 rootValue: _this.rootValue,
36 contextValue: context,
37 variableValues: operation.variables,
38 operationName: operation.operationName,
39 });
40 }).then(function (data) {
41 if (!observer.closed) {
42 observer.next(data);
43 observer.complete();
44 }
45 }).catch(function (error) {
46 if (!observer.closed) {
47 observer.error(error);
48 }
49 });
50 });
51 };
52 return SchemaLink;
53}(core.ApolloLink));
54
55exports.SchemaLink = SchemaLink;
56//# sourceMappingURL=schema.cjs.map