@elrondnetwork/erdjs-data-api-client
Version:
MultiversX Data API client
79 lines • 3.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataApiBaseQueryBuilder = exports.QueryInput = void 0;
const tslib_1 = require("tslib");
const prettier_1 = tslib_1.__importDefault(require("prettier"));
const moment_1 = tslib_1.__importDefault(require("moment"));
const queries_1 = require("../../queries");
const QUERY_PLACEHOLDER = '%REPLACE_ME%';
class QueryInput {
toGraphQlInput() {
let query = `query: {
${this.resolution ? `resolution: ${this.resolution}` : ''}
${this.range ? `range: ${this.range}` : ''}
${this.start_date ? `start_date: "${(0, moment_1.default)(this.start_date).format('yyyy-MM-DD HH:mm:ss')}"` : ''}
${this.end_date ? `end_date: "${(0, moment_1.default)(this.end_date).format('yyyy-MM-DD HH:mm:ss')}"` : ''}
${this.date ? `date: "${(0, moment_1.default)(this.date).format('yyyy-MM-DD')}"` : ''}
${this.fill_data_gaps ? `fill_data_gaps: ${this.fill_data_gaps}` : ''}
}`;
query = query.replace(/^\s*\n/gm, '');
return query;
}
}
exports.QueryInput = QueryInput;
class DataApiBaseQueryBuilder {
constructor() {
this.path = [];
this.values = [];
}
addToPath(path) {
this.path.push(path);
}
addOption(key, value) {
if (this.queryInput === undefined) {
this.queryInput = new QueryInput();
}
this.queryInput[key] = value;
}
addValues(...values) {
this.values.push(...values);
}
copyProps(query) {
this.path = query.path;
this.queryInput = query.queryInput;
this.values = query.values;
}
buildQuery(queryType) {
const variables = {};
let query = QUERY_PLACEHOLDER;
const argsDefinition = [];
for (const { name, args, hasQuery } of this.path) {
const inputArgs = [];
for (const arg of args) {
variables[arg.name] = arg.value;
inputArgs.push(`${arg.name}: $${arg.name}`);
argsDefinition.push(`$${arg.name}: ${arg.type}`);
}
if (hasQuery && this.queryInput) {
const queryInput = this.queryInput.toGraphQlInput();
inputArgs.push(queryInput);
}
const inputQuery = inputArgs.length > 0 ? `(${inputArgs.join()})` : '';
query = query.replace(QUERY_PLACEHOLDER, `${name}${inputQuery} {
${QUERY_PLACEHOLDER}
}`);
}
query = this.addQueryValues(query, this.values);
const argsDefinitionString = argsDefinition.length > 0 ? `(${argsDefinition.join()})` : '';
const queryId = (0, moment_1.default)().unix();
query = `query clientQuery_${queryId}${argsDefinitionString} { ${query} }`;
query = prettier_1.default.format(query, { parser: 'graphql' });
const responsePath = this.path.map(p => p.name);
return new queries_1.DataApiBaseQuery(queryType, query, variables, responsePath);
}
addQueryValues(query, values) {
return query.replace(QUERY_PLACEHOLDER, [...new Set(values)].join());
}
}
exports.DataApiBaseQueryBuilder = DataApiBaseQueryBuilder;
//# sourceMappingURL=base.query.builder.js.map