apollo-client-code-first-request
Version:
61 lines • 2.72 kB
JavaScript
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
exports.__esModule = true;
exports.createGql = void 0;
var metadata_util_1 = require("./metadata.util");
function serializeArguments(argsRefPrototype, args) {
var propertyNames = metadata_util_1.Metadata.getPropertyNames(argsRefPrototype);
if (!propertyNames.length)
return args;
var serializedArguments = {};
propertyNames.forEach(function (propertyName) {
var _a = metadata_util_1.Metadata.getFieldInfo(argsRefPrototype, propertyName), type = _a.type, isArray = _a.isArray, scalar = _a.scalar;
var propertyValues = isArray ? __spreadArray([], args[propertyName], true) : [args[propertyName]];
propertyValues.forEach(function (value, index) {
propertyValues[index] = serializeArguments(type.prototype, propertyValues[index]);
if (value && scalar) {
propertyValues[index] = new scalar().serialize(propertyValues[index]);
}
});
serializedArguments[propertyName] = isArray ? propertyValues : propertyValues[0];
});
return serializedArguments;
}
function createGqlArgument(request) {
if (!request)
return '';
var serializedArguments = serializeArguments(request.argsRef.prototype, request.args);
return "(".concat(JSON.stringify(serializedArguments)
.replace(/,/g, ', ')
.replace(/"([^"]+)":/g, '$1: ')
.slice(1, -1), ")");
}
function createGqlBody(responseRef) {
var resRef = Array.isArray(responseRef) ? responseRef[0] : responseRef;
var propertyNames = metadata_util_1.Metadata.getPropertyNames(resRef.prototype);
if (!propertyNames.length)
return '';
var body = propertyNames
.reduce(function (prev, curr) {
var type = metadata_util_1.Metadata.getFieldInfo(resRef.prototype, curr).type;
var nestedField = createGqlBody(type);
return "".concat(prev).concat(curr, " ").concat(nestedField ? "".concat(nestedField, " ") : '');
}, '')
.slice(0, -1);
return "{".concat(body, "}");
}
function createGql(query) {
var gqlQueryArgument = createGqlArgument(query.request);
var gqlQueryBody = createGqlBody(query.responseRef);
return "".concat(query.name).concat(gqlQueryArgument, " ").concat(gqlQueryBody);
}
exports.createGql = createGql;
//# sourceMappingURL=create-gql.util.js.map
;