196 lines
7.1 KiB
JavaScript
196 lines
7.1 KiB
JavaScript
import {Kind, visit, parse, print} from "graphql";
|
|
|
|
/* eslint-disable no-loop-func */
|
|
|
|
/**
|
|
* Parse a GraphQL query into an AST
|
|
* @param query
|
|
* @returns {DocumentNode}
|
|
*/
|
|
export function parseQuery(query) {
|
|
return parse(query);
|
|
}
|
|
|
|
/**
|
|
* Print an AST back into a GraphQL query
|
|
* @param query
|
|
* @returns {string}
|
|
*/
|
|
export function printQuery(query) {
|
|
return print(query);
|
|
}
|
|
/**
|
|
* Apply sorters to the AST
|
|
* @param ast
|
|
* @param sorters
|
|
*/
|
|
export function applySorters(ast, sorters) {
|
|
sorters.forEach((sorter) => {
|
|
const fieldPath = sorter.field.split('.');
|
|
visit(ast, {
|
|
OperationDefinition: {
|
|
enter(node) {
|
|
// Loop through each sorter to apply it
|
|
// noinspection DuplicatedCode
|
|
|
|
let currentSelection = node; // Start with the root operation
|
|
|
|
// Navigate down the field path to the correct location
|
|
for (let i = 0; i < fieldPath.length - 1; i++) {
|
|
let found = false;
|
|
visit(currentSelection, {
|
|
Field: {
|
|
enter(node) {
|
|
if (node.name.value === fieldPath[i]) {
|
|
currentSelection = node; // Move down to the next level
|
|
found = true;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
if (!found) break; // Stop if we can't find the next field in the path
|
|
}
|
|
|
|
// Apply the sorter at the correct level
|
|
if (currentSelection) {
|
|
const targetFieldName = fieldPath[fieldPath.length - 1];
|
|
let orderByArg = currentSelection.arguments.find(arg => arg.name.value === 'order_by');
|
|
if (!orderByArg) {
|
|
orderByArg = {
|
|
kind: Kind.ARGUMENT,
|
|
name: { kind: Kind.NAME, value: 'order_by' },
|
|
value: { kind: Kind.OBJECT, fields: [] },
|
|
};
|
|
currentSelection.arguments.push(orderByArg);
|
|
}
|
|
|
|
const sorterField = {
|
|
kind: Kind.OBJECT_FIELD,
|
|
name: { kind: Kind.NAME, value: targetFieldName },
|
|
value: { kind: Kind.ENUM, value: sorter.direction }, // Adjust if your schema uses a different type for sorting directions
|
|
};
|
|
|
|
// Add the new sorter condition
|
|
orderByArg.value.fields.push(sorterField);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Apply filters to the AST
|
|
* @param ast
|
|
* @param filters
|
|
*/
|
|
export function applyFilters(ast, filters) {
|
|
return visit(ast, {
|
|
OperationDefinition: {
|
|
enter(node) {
|
|
// Loop through each filter to apply it
|
|
filters.forEach(filter => {
|
|
const fieldPath = filter.field.split('.');
|
|
let currentSelection = node; // Start with the root operation
|
|
|
|
// Navigate down the field path to the correct location
|
|
for (let i = 0; i < fieldPath.length - 1; i++) {
|
|
let found = false;
|
|
visit(currentSelection, {
|
|
Field: {
|
|
enter(node) {
|
|
if (node.name.value === fieldPath[i]) {
|
|
currentSelection = node; // Move down to the next level
|
|
found = true;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
if (!found) break; // Stop if we can't find the next field in the path
|
|
}
|
|
|
|
// Apply the filter at the correct level
|
|
if (currentSelection) {
|
|
const targetFieldName = fieldPath[fieldPath.length - 1];
|
|
let whereArg = currentSelection.arguments.find(arg => arg.name.value === 'where');
|
|
if (!whereArg) {
|
|
whereArg = {
|
|
kind: Kind.ARGUMENT,
|
|
name: { kind: Kind.NAME, value: 'where' },
|
|
value: { kind: Kind.OBJECT, fields: [] },
|
|
};
|
|
currentSelection.arguments.push(whereArg);
|
|
}
|
|
|
|
const filterValue = {
|
|
kind: getGraphQLKind(filter.value),
|
|
value: filter.value,
|
|
};
|
|
|
|
const filterField = {
|
|
kind: Kind.OBJECT_FIELD,
|
|
name: { kind: Kind.NAME, value: targetFieldName },
|
|
value: {
|
|
kind: Kind.OBJECT,
|
|
fields: [{
|
|
kind: Kind.OBJECT_FIELD,
|
|
name: { kind: Kind.NAME, value: filter.operator },
|
|
value: filterValue,
|
|
}],
|
|
},
|
|
};
|
|
|
|
// Add the new filter condition
|
|
whereArg.value.fields.push(filterField);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the GraphQL kind for a value
|
|
* @param value
|
|
* @returns {Kind|Kind.INT}
|
|
*/
|
|
function getGraphQLKind(value) {
|
|
if (typeof value === 'number') {
|
|
return value % 1 === 0 ? Kind.INT : Kind.FLOAT;
|
|
} else if (typeof value === 'boolean') {
|
|
return Kind.BOOLEAN;
|
|
} else if (typeof value === 'string') {
|
|
return Kind.STRING;
|
|
}
|
|
// Extend with more types as needed
|
|
}
|
|
|
|
/**
|
|
* Wrap filters in an 'and' object
|
|
* @param ast
|
|
* @param filterFields
|
|
*/
|
|
export function wrapFiltersInAnd(ast, filterFields) {
|
|
visit(ast, {
|
|
OperationDefinition: {
|
|
enter(node) {
|
|
node.selectionSet.selections.forEach((selection) => {
|
|
let whereArg = selection.arguments.find(arg => arg.name.value === 'where');
|
|
if (filterFields.length > 1) {
|
|
const andFilter = {
|
|
kind: Kind.OBJECT_FIELD,
|
|
name: {kind: Kind.NAME, value: '_and'},
|
|
value: {kind: Kind.LIST, values: filterFields}
|
|
};
|
|
whereArg.value.fields.push(andFilter);
|
|
} else if (filterFields.length === 1) {
|
|
whereArg.value.fields.push(filterFields[0].fields[0]);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/* eslint-enable no-loop-func */
|