UNPKG

molstar

Version:

A comprehensive macromolecular library.

64 lines (63 loc) 2.89 kB
/** * 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 */ import { __assign, __awaiter, __extends, __generator } from "tslib"; import { Asset } from './assets'; var ClientError = /** @class */ (function (_super) { __extends(ClientError, _super); function ClientError(response, request) { var _this = this; var message = "".concat(ClientError.extractMessage(response), ": ").concat(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: ".concat(response.status, ")"); }; return ClientError; }(Error)); export { ClientError }; var GraphQLClient = /** @class */ (function () { function GraphQLClient(url, assetManager) { this.url = url; this.assetManager = assetManager; } GraphQLClient.prototype.request = function (ctx, query, variables) { return __awaiter(this, void 0, void 0, function () { var body, url, result, errorResult; return __generator(this, function (_a) { switch (_a.label) { case 0: body = JSON.stringify({ query: query, variables: variables }, null, 2); url = 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(__assign({}, errorResult), { query: query, variables: variables }); } return [2 /*return*/]; } }); }); }; return GraphQLClient; }()); export { GraphQLClient };