@ace-fetch/graphql
Version:
Fetch Provider.
112 lines (111 loc) • 5.08 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registGraphql = exports.GraphQLInnerError = void 0;
var constants_1 = require("../constants");
var parser_1 = require("./parser");
var GraphQLInnerError = /** @class */ (function (_super) {
__extends(GraphQLInnerError, _super);
function GraphQLInnerError(message, graphQLErrors) {
var _this = _super.call(this, message) || this;
Object.setPrototypeOf(_this, GraphQLInnerError.prototype);
_this.graphQLErrors = graphQLErrors;
_this.name = 'GraphQLInnerError';
return _this;
}
return GraphQLInnerError;
}(Error));
exports.GraphQLInnerError = GraphQLInnerError;
/**
* register graphql
* @param client ApolloClient
* @param definition regist graphqls definition
*/
function registGraphql(client, definition) {
return Object.keys(definition).reduce(function (prev, key) {
var doc = (0, parser_1.parser)(definition[key]);
if (doc.type === constants_1.DocumentType.Query) {
prev[key] = function (options) {
return client.query(__assign(__assign({}, options), { query: definition[key] })).then(function (_a) {
var data = _a.data, error = _a.error, errors = _a.errors;
// "all" errorPolicy
// https://www.apollographql.com/docs/react/data/error-handling#graphql-error-policies
if (error || (errors === null || errors === void 0 ? void 0 : errors.length)) {
throw error ? error : new GraphQLInnerError('Check details in inner error(s)', errors);
}
return data;
});
};
}
else if (doc.type === constants_1.DocumentType.Mutation) {
prev[key] = function (options) {
return client.mutate(__assign(__assign({}, options), { mutation: definition[key] })).then(function (_a) {
var data = _a.data, errors = _a.errors;
// "all" errorPolicy
// https://www.apollographql.com/docs/react/data/error-handling#graphql-error-policies
if (errors === null || errors === void 0 ? void 0 : errors.length) {
throw new GraphQLInnerError('Check details in inner error(s)', errors);
}
return data;
});
};
}
else if (doc.type === constants_1.DocumentType.Subscription) {
prev[key] = function (_a) {
var onData = _a.onData, onError = _a.onError, onComplete = _a.onComplete, subscribeOptions = __rest(_a, ["onData", "onError", "onComplete"]);
var observer = client.subscribe(__assign(__assign({}, subscribeOptions), { query: definition[key] }));
var subscription = observer.subscribe({
next: function (_a) {
var data = _a.data;
return onData(data);
},
error: onError,
complete: onComplete,
});
return function () { return subscription.unsubscribe(); };
};
}
else {
// TODO
prev[key] = function () { return Promise.resolve(); };
}
return prev;
}, {});
}
exports.registGraphql = registGraphql;