UNPKG

3.96 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var globals = require('../../utilities/globals');
6var utilities = require('../../utilities');
7var tslib = require('tslib');
8var graphql = require('graphql');
9
10function fromError(errorValue) {
11 return new utilities.Observable(function (observer) {
12 observer.error(errorValue);
13 });
14}
15
16function 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
34function 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
45var 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
54function 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
71function 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
93function 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
109function 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
126exports.createOperation = createOperation;
127exports.filterOperationVariables = filterOperationVariables;
128exports.fromError = fromError;
129exports.fromPromise = fromPromise;
130exports.throwServerError = throwServerError;
131exports.toPromise = toPromise;
132exports.transformOperation = transformOperation;
133exports.validateOperation = validateOperation;
134//# sourceMappingURL=utils.cjs.map