1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var globals = require('../../utilities/globals');
|
6 | var utilities = require('../../utilities');
|
7 | var tslib = require('tslib');
|
8 | var graphql = require('graphql');
|
9 |
|
10 | function fromError(errorValue) {
|
11 | return new utilities.Observable(function (observer) {
|
12 | observer.error(errorValue);
|
13 | });
|
14 | }
|
15 |
|
16 | function toPromise(observable) {
|
17 | var completed = false;
|
18 | return new Promise(function (resolve, reject) {
|
19 | observable.subscribe({
|
20 | next: function (data) {
|
21 | if (completed) {
|
22 | globalThis.__DEV__ !== false && globals.invariant.warn(43);
|
23 | }
|
24 | else {
|
25 | completed = true;
|
26 | resolve(data);
|
27 | }
|
28 | },
|
29 | error: reject,
|
30 | });
|
31 | });
|
32 | }
|
33 |
|
34 | function fromPromise(promise) {
|
35 | return new utilities.Observable(function (observer) {
|
36 | promise
|
37 | .then(function (value) {
|
38 | observer.next(value);
|
39 | observer.complete();
|
40 | })
|
41 | .catch(observer.error.bind(observer));
|
42 | });
|
43 | }
|
44 |
|
45 | var throwServerError = function (response, result, message) {
|
46 | var error = new Error(message);
|
47 | error.name = "ServerError";
|
48 | error.response = response;
|
49 | error.statusCode = response.status;
|
50 | error.result = result;
|
51 | throw error;
|
52 | };
|
53 |
|
54 | function validateOperation(operation) {
|
55 | var OPERATION_FIELDS = [
|
56 | "query",
|
57 | "operationName",
|
58 | "variables",
|
59 | "extensions",
|
60 | "context",
|
61 | ];
|
62 | for (var _i = 0, _a = Object.keys(operation); _i < _a.length; _i++) {
|
63 | var key = _a[_i];
|
64 | if (OPERATION_FIELDS.indexOf(key) < 0) {
|
65 | throw globals.newInvariantError(44, key);
|
66 | }
|
67 | }
|
68 | return operation;
|
69 | }
|
70 |
|
71 | function createOperation(starting, operation) {
|
72 | var context = tslib.__assign({}, starting);
|
73 | var setContext = function (next) {
|
74 | if (typeof next === "function") {
|
75 | context = tslib.__assign(tslib.__assign({}, context), next(context));
|
76 | }
|
77 | else {
|
78 | context = tslib.__assign(tslib.__assign({}, context), next);
|
79 | }
|
80 | };
|
81 | var getContext = function () { return (tslib.__assign({}, context)); };
|
82 | Object.defineProperty(operation, "setContext", {
|
83 | enumerable: false,
|
84 | value: setContext,
|
85 | });
|
86 | Object.defineProperty(operation, "getContext", {
|
87 | enumerable: false,
|
88 | value: getContext,
|
89 | });
|
90 | return operation;
|
91 | }
|
92 |
|
93 | function transformOperation(operation) {
|
94 | var transformedOperation = {
|
95 | variables: operation.variables || {},
|
96 | extensions: operation.extensions || {},
|
97 | operationName: operation.operationName,
|
98 | query: operation.query,
|
99 | };
|
100 | if (!transformedOperation.operationName) {
|
101 | transformedOperation.operationName =
|
102 | typeof transformedOperation.query !== "string" ?
|
103 | utilities.getOperationName(transformedOperation.query) || undefined
|
104 | : "";
|
105 | }
|
106 | return transformedOperation;
|
107 | }
|
108 |
|
109 | function filterOperationVariables(variables, query) {
|
110 | var result = tslib.__assign({}, variables);
|
111 | var unusedNames = new Set(Object.keys(variables));
|
112 | graphql.visit(query, {
|
113 | Variable: function (node, _key, parent) {
|
114 | if (parent &&
|
115 | parent.kind !== "VariableDefinition") {
|
116 | unusedNames.delete(node.name.value);
|
117 | }
|
118 | },
|
119 | });
|
120 | unusedNames.forEach(function (name) {
|
121 | delete result[name];
|
122 | });
|
123 | return result;
|
124 | }
|
125 |
|
126 | exports.createOperation = createOperation;
|
127 | exports.filterOperationVariables = filterOperationVariables;
|
128 | exports.fromError = fromError;
|
129 | exports.fromPromise = fromPromise;
|
130 | exports.throwServerError = throwServerError;
|
131 | exports.toPromise = toPromise;
|
132 | exports.transformOperation = transformOperation;
|
133 | exports.validateOperation = validateOperation;
|
134 |
|