@actonate/mirkwood
Version:
GraphQL based Rapid Server-side Development framework
222 lines (188 loc) • 6.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _graphql = require('graphql');
var _apolloErrors = require('apollo-errors');
var _lodash = require('lodash');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var GQL = function () {
function GQL(_ref) {
var schema = _ref.schema,
req = _ref.req,
utils = _ref.utils;
_classCallCheck(this, GQL);
this._schema = schema;
this._req = req;
this._utils = utils;
}
_createClass(GQL, [{
key: 'query',
value: function query(queryStr, traversePath) {
var _this = this;
var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
variables = _ref2.variables,
context = _ref2.context;
// clean queryStr append query wrapper if not added
queryStr = queryStr.trim();
if (!queryStr.startsWith('query')) {
queryStr = '\n\t\t\t\tquery {\n\t\t\t\t\t' + queryStr + '\n\t\t\t\t}\n\t\t\t';
}
var query = queryStr;
return new Promise(function (resolve, reject) {
(0, _graphql.graphql)(_this._schema, query, null, _extends({}, context, {
gql: _this,
req: _this._req,
utils: _this._utils,
gqlQuery: true
}), variables).then(function (res) {
// throw handled exception
if (res.error || res.errors) {
reject(res);
}
if (traversePath) {
resolve(traversePath.split('.').reduce(function (obj, i) {
return obj[i];
}, res.data));
} else {
resolve(res.data);
}
}).catch(function (err) {
reject(err);
});
});
}
}, {
key: 'mutation',
value: function mutation(queryStr, traversePath) {
var _this2 = this;
var _ref3 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
variables = _ref3.variables,
context = _ref3.context;
// clean string and append mutation wrapper if not added
queryStr = queryStr.trim();
if (!queryStr.startsWith('mutation')) {
queryStr = '\n\t\t\t\tmutation {\n\t\t\t\t\t' + queryStr + '\n\t\t\t\t}\n\t\t\t';
}
var mutation = queryStr;
return new Promise(function (resolve, reject) {
(0, _graphql.graphql)(_this2._schema, mutation, null, _extends({}, context, {
gql: _this2,
req: _this2._req,
utils: _this2._utils,
gqlQuery: true
}), variables).then(function (res) {
// throw handled exception
if (res.error || res.errors) {
reject(res);
}
if (traversePath) {
resolve(traversePath.split('.').reduce(function (obj, i) {
return obj[i];
}, res.data));
} else {
resolve(res.data);
}
}).catch(function (err) {
reject(err);
});
});
}
}, {
key: 'fields',
value: function fields(obj) {
var objString = '';
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = Object.keys(obj)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;
if (_typeof(obj[key]) === 'object' && obj[key] !== null) {
objString += [key, ' {\n', this.fields(obj[key]), '}\n'].join('');
} else {
objString += [key, '\n'].join('');
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return objString;
}
}, {
key: 'esc',
value: function esc(str) {
return str.replace('"', '\"').replace('`', '\`');
return str;
}
}, {
key: 'encode',
value: function encode(obj) {
var self = this;
var encodedObj = '';
// Case when array
if (Array.isArray(obj)) {
var arr = obj;
var encodedArr = (0, _lodash.map)(arr, function (arrIter) {
return self.encode(arrIter);
}).join(',');
return '[' + encodedArr + ']';
}
// case when object
else if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && obj !== null && !Array.isArray(obj)) {
var encodedObjKeyValues = [];
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = Object.keys(obj)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var key = _step2.value;
if (obj[key] !== null) {
encodedObjKeyValues.push(key + ':' + self.encode(obj[key]));
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
var _encodedObj = encodedObjKeyValues.join(',');
return '{' + _encodedObj + '}';
}
// case when String
else if (typeof obj === 'string') {
var str = obj;
return '"' + self.esc(str) + '"';
} else if (obj === null) {
return '';
} else {
return obj.toString();
}
}
}]);
return GQL;
}();
exports.default = GQL;