UNPKG

@dillonkearns/elm-graphql

Version:

<img src="https://cdn.jsdelivr.net/gh/martimatix/logo-graphqelm/logo.svg" alt="dillonearns/elm-graphql logo" width="40%" align="right">

206 lines (163 loc) 5.04 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (factory((global.UtilsGraphql = {}))); }(this, (function (exports) { 'use strict'; function unwrapExports (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var newArrowCheck = createCommonjsModule(function (module, exports) { "use strict"; exports.__esModule = true; exports.default = function (innerThis, boundThis) { if (innerThis !== boundThis) { throw new TypeError("Cannot instantiate an arrow function"); } }; }); var _newArrowCheck = unwrapExports(newArrowCheck); var _this = undefined; var locationsToString = function (locations) { _newArrowCheck(this, _this); return locations.map(function (_ref) { var column = _ref.column, line = _ref.line; _newArrowCheck(this, _this); return String(line) + ":" + String(column); }.bind(this)).join("; "); }.bind(undefined); var errorToString = function (_ref2) { var message = _ref2.message, locations = _ref2.locations; _newArrowCheck(this, _this); return message + (locations ? " (" + String(locationsToString(locations)) + ")" : ""); }.bind(undefined); /** * Transforms an array of GqlError into a string. * * @example * * const gqlRespose = { * errors: [ * {message: "First Error", locations: [{column: 10, line: 2}]}, * {message: "Second Error", locations: [{column: 2, line: 4}]} * ] * } * * const error = errorsToString(gqlRespose.errors); * // string with the following: * // First Error (2:10) * // Second Error (4:2) */ var errorsToString = function (gqlErrors) { _newArrowCheck(this, _this); return gqlErrors.map(errorToString).join("\n"); }.bind(undefined); var _this$1 = undefined; var operationTypeRe = /^\s*(query|mutation|subscription|\{)/; var getOperationTypeFromMatched = function (matched) { _newArrowCheck(this, _this$1); return matched === "{" ? "query" : matched; }.bind(undefined); /** * Returns the type (query, mutation, or subscription) of the given operation * * @example * * const operation = ` * subscription userSubscription($userId: ID!) { * user(userId: $userId) { * id * name * } * } * `; * * const operationType = getOperationType(operation); * * console.log(operationType); // "subscription" */ var getOperationType = function (operation) { _newArrowCheck(this, _this$1); var result = operation.match(operationTypeRe); if (!result) { throw new TypeError("Invalid operation:\n" + String(operation)); } return getOperationTypeFromMatched(result[1]); }.bind(undefined); var _this$2 = undefined; var isSubscription = function (definition) { _newArrowCheck(this, _this$2); return definition.kind === "OperationDefinition" && definition.operation === "subscription"; }.bind(undefined); /** * Returns true if documentNode has a subscription or false otherwise */ var hasSubscription = function (documentNode) { _newArrowCheck(this, _this$2); return documentNode.definitions.some(isSubscription); }.bind(undefined); var _this$3 = undefined; /** * Creates a GqlRequest using given GqlRequestCompat * * @param {GqlRequestCompat<Variables>} gqlRequestCompat * * @return {GqlRequest<Variables>} * * @example * const query = ` * query userQuery($userId: ID!) { * user(userId: $userId) { * id * email * } * } * `; * * console.log(requestFromCompat({query, variables: {userId: 10}})); * // {operation: "...", variables: {userId: 10}} */ var requestFromCompat = function (_ref) { var operation = _ref.query, variables = _ref.variables; _newArrowCheck(this, _this$3); return variables ? { operation: operation, variables: variables } : { operation: operation }; }.bind(undefined); var _this$4 = undefined; /** * Creates a GqlRequest using given GqlRequestCompat * * @param {GqlRequest<Variables>} gqlRequest * * @return {GqlRequestCompat<Variables>} * * @example * const operation = ` * query userQuery($userId: ID!) { * user(userId: $userId) { * id * email * } * } * `; * * console.log(requestToCompat({operation, variables: {userId: 10}})); * // {query: "...", variables: {userId: 10}} */ var requestToCompat = function (_ref) { var query = _ref.operation, variables = _ref.variables; _newArrowCheck(this, _this$4); return variables ? { query: query, variables: variables } : { query: query }; }.bind(undefined); exports.errorsToString = errorsToString; exports.getOperationType = getOperationType; exports.hasSubscription = hasSubscription; exports.requestFromCompat = requestFromCompat; exports.requestToCompat = requestToCompat; Object.defineProperty(exports, '__esModule', { value: true }); })));