molstar
Version:
A comprehensive macromolecular library.
67 lines • 3.1 kB
JavaScript
/**
* Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*
* Adapted from https://github.com/prisma/graphql-request, Copyright (c) 2017 Graphcool, MIT
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphQLClient = exports.ClientError = void 0;
var tslib_1 = require("tslib");
var assets_1 = require("./assets");
var ClientError = /** @class */ (function (_super) {
(0, tslib_1.__extends)(ClientError, _super);
function ClientError(response, request) {
var _this = this;
var message = ClientError.extractMessage(response) + ": " + JSON.stringify({ response: response, request: request });
_this = _super.call(this, message) || this;
_this.response = response;
_this.request = request;
// this is needed as Safari doesn't support .captureStackTrace
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(_this, ClientError);
}
return _this;
}
ClientError.extractMessage = function (response) {
return response.errors ? response.errors[0].message : "GraphQL Error (Code: " + response.status + ")";
};
return ClientError;
}(Error));
exports.ClientError = ClientError;
var GraphQLClient = /** @class */ (function () {
function GraphQLClient(url, assetManager) {
this.url = url;
this.assetManager = assetManager;
}
GraphQLClient.prototype.request = function (ctx, query, variables) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var body, url, result, errorResult;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
body = JSON.stringify({ query: query, variables: variables }, null, 2);
url = assets_1.Asset.getUrlAsset(this.assetManager, this.url, body);
return [4 /*yield*/, this.assetManager.resolve(url, 'json').runInContext(ctx)];
case 1:
result = _a.sent();
if (!result.data.errors && result.data.data) {
return [2 /*return*/, {
data: result.data.data,
dispose: result.dispose
}];
}
else {
errorResult = typeof result.data === 'string' ? { error: result.data } : result.data;
throw new ClientError((0, tslib_1.__assign)({}, errorResult), { query: query, variables: variables });
}
return [2 /*return*/];
}
});
});
};
return GraphQLClient;
}());
exports.GraphQLClient = GraphQLClient;
//# sourceMappingURL=graphql-client.js.map
;