1 | import { __extends } from "tslib";
|
2 | import { validate, execute } from 'graphql';
|
3 | import { ApolloLink } from "../core/index.js";
|
4 | import { Observable } from "../../utilities/index.js";
|
5 | var SchemaLink = (function (_super) {
|
6 | __extends(SchemaLink, _super);
|
7 | function SchemaLink(options) {
|
8 | var _this = _super.call(this) || this;
|
9 | _this.schema = options.schema;
|
10 | _this.rootValue = options.rootValue;
|
11 | _this.context = options.context;
|
12 | _this.validate = !!options.validate;
|
13 | return _this;
|
14 | }
|
15 | SchemaLink.prototype.request = function (operation) {
|
16 | var _this = this;
|
17 | return new Observable(function (observer) {
|
18 | new Promise(function (resolve) { return resolve(typeof _this.context === 'function'
|
19 | ? _this.context(operation)
|
20 | : _this.context); }).then(function (context) {
|
21 | if (_this.validate) {
|
22 | var validationErrors = validate(_this.schema, operation.query);
|
23 | if (validationErrors.length > 0) {
|
24 | return { errors: validationErrors };
|
25 | }
|
26 | }
|
27 | return execute({
|
28 | schema: _this.schema,
|
29 | document: operation.query,
|
30 | rootValue: _this.rootValue,
|
31 | contextValue: context,
|
32 | variableValues: operation.variables,
|
33 | operationName: operation.operationName,
|
34 | });
|
35 | }).then(function (data) {
|
36 | if (!observer.closed) {
|
37 | observer.next(data);
|
38 | observer.complete();
|
39 | }
|
40 | }).catch(function (error) {
|
41 | if (!observer.closed) {
|
42 | observer.error(error);
|
43 | }
|
44 | });
|
45 | });
|
46 | };
|
47 | return SchemaLink;
|
48 | }(ApolloLink));
|
49 | export { SchemaLink };
|
50 |
|
\ | No newline at end of file |