@@ -19,118 +19,51 @@ const Templates = TemplateList();
|
||||
|
||||
|
||||
function applyFilters(ast, filters) {
|
||||
const struct = filters.field.split('.');
|
||||
return filters.reduce((modifiedAst, filter) => {
|
||||
const struct = filter.field.split('.');
|
||||
|
||||
return visit(ast, {
|
||||
OperationDefinition: {
|
||||
enter(node) {
|
||||
// Traverse through the operation definitions to find the correct query and its arguments
|
||||
node.selectionSet.selections.forEach((selection) => {
|
||||
if (selection.name.value ===struct[0]) {
|
||||
// Find the existing 'where' argument, if it exists
|
||||
let whereArg = selection.arguments.find(arg => arg.name.value === 'where');
|
||||
return visit(modifiedAst, {
|
||||
OperationDefinition: {
|
||||
enter(node) {
|
||||
// Traverse through the operation definitions to find the correct query and its arguments
|
||||
node.selectionSet.selections.forEach((selection) => {
|
||||
if (selection.name.value === struct[0]) {
|
||||
// Find the existing 'where' argument, if it exists
|
||||
let whereArg = selection.arguments.find(arg => arg.name.value === 'where');
|
||||
|
||||
if (!whereArg) {
|
||||
// If 'where' argument doesn't exist, create it with an _and structure
|
||||
whereArg = {
|
||||
kind: Kind.ARGUMENT,
|
||||
name: { kind: Kind.NAME, value: 'where' },
|
||||
value: {
|
||||
kind: Kind.OBJECT,
|
||||
fields: [{
|
||||
kind: Kind.OBJECT_FIELD,
|
||||
name: { kind: Kind.NAME, value: '_and' },
|
||||
value: { kind: Kind.LIST, values: [] } // Initialize an empty list for '_and' conditions
|
||||
}]
|
||||
}
|
||||
};
|
||||
selection.arguments.push(whereArg); // Add 'where' argument to the 'jobs' field
|
||||
} else {
|
||||
// Ensure the _and structure exists if the where argument already exists
|
||||
let andField = whereArg.value.fields.find(field => field.name.value === '_and');
|
||||
if (!andField) {
|
||||
andField = {
|
||||
kind: Kind.OBJECT_FIELD,
|
||||
name: { kind: Kind.NAME, value: '_and' },
|
||||
value: { kind: Kind.LIST, values: [] }
|
||||
if (!whereArg) {
|
||||
// If 'where' argument doesn't exist, create it
|
||||
whereArg = {
|
||||
kind: Kind.ARGUMENT,
|
||||
name: { kind: Kind.NAME, value: 'where' },
|
||||
value: { kind: Kind.OBJECT, fields: [] } // Initialize an empty object for 'where' clause
|
||||
};
|
||||
whereArg.value.fields.push(andField);
|
||||
selection.arguments.push(whereArg); // Add 'where' argument to the 'jobs' field
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare the filter condition to be added to the _and list
|
||||
const filterConditions = filters.map(filter => ({
|
||||
kind: Kind.OBJECT,
|
||||
fields: [{
|
||||
// Assuming the filter should be added to the 'where' clause
|
||||
const filterField = {
|
||||
kind: Kind.OBJECT_FIELD,
|
||||
name: { kind: Kind.NAME, value: struct[1] },
|
||||
value: {
|
||||
kind: Kind.OBJECT,
|
||||
fields: [{
|
||||
kind: Kind.OBJECT_FIELD,
|
||||
name: { kind: Kind.NAME, value: filter.operator }, // Adjust based on filter.operator if necessary
|
||||
value: { kind: Kind.STRING, value: filter.value } // Adjust based on the type of filter.value
|
||||
name: { kind: Kind.NAME, value: filter.operator }, // Assuming equality operator; adjust as necessary
|
||||
value: { kind: Kind.STRING, value: filter.value }
|
||||
}]
|
||||
}
|
||||
}]
|
||||
}));
|
||||
};
|
||||
|
||||
// Locate the _and field and add the filter conditions
|
||||
const andField = whereArg.value.fields.find(field => field.name.value === '_and');
|
||||
andField.value.values.push(...filterConditions);
|
||||
}
|
||||
});
|
||||
// Add the filter to the 'where' argument's value
|
||||
whereArg.value.fields.push(filterField);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}, ast);
|
||||
}
|
||||
|
||||
// function applyFilters(ast, filters) {
|
||||
// return filters.reduce((modifiedAst, filter) => {
|
||||
// const struct = filter.field.split('.');
|
||||
//
|
||||
// return visit(modifiedAst, {
|
||||
// OperationDefinition: {
|
||||
// enter(node) {
|
||||
// // Traverse through the operation definitions to find the correct query and its arguments
|
||||
// node.selectionSet.selections.forEach((selection) => {
|
||||
// if (selection.name.value === struct[0]) {
|
||||
// // Find the existing 'where' argument, if it exists
|
||||
// let whereArg = selection.arguments.find(arg => arg.name.value === 'where');
|
||||
//
|
||||
// if (!whereArg) {
|
||||
// // If 'where' argument doesn't exist, create it
|
||||
// whereArg = {
|
||||
// kind: Kind.ARGUMENT,
|
||||
// name: { kind: Kind.NAME, value: 'where' },
|
||||
// value: { kind: Kind.OBJECT, fields: [] } // Initialize an empty object for 'where' clause
|
||||
// };
|
||||
// selection.arguments.push(whereArg); // Add 'where' argument to the 'jobs' field
|
||||
// }
|
||||
//
|
||||
// // Assuming the filter should be added to the 'where' clause
|
||||
// const filterField = {
|
||||
// kind: Kind.OBJECT_FIELD,
|
||||
// name: { kind: Kind.NAME, value: struct[1] },
|
||||
// value: {
|
||||
// kind: Kind.OBJECT,
|
||||
// fields: [{
|
||||
// kind: Kind.OBJECT_FIELD,
|
||||
// name: { kind: Kind.NAME, value: filter.operator }, // Assuming equality operator; adjust as necessary
|
||||
// value: { kind: Kind.STRING, value: filter.value }
|
||||
// }]
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// // Add the filter to the 'where' argument's value
|
||||
// whereArg.value.fields.push(filterField);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }, ast);
|
||||
// }
|
||||
export default async function RenderTemplate(
|
||||
templateObject,
|
||||
bodyshop,
|
||||
|
||||
Reference in New Issue
Block a user