From a87473c3928695771586def77be2cabe07e44f35 Mon Sep 17 00:00:00 2001 From: Dave Richer Date: Thu, 29 Feb 2024 21:31:10 -0500 Subject: [PATCH] - Progress Commit Signed-off-by: Dave Richer --- client/src/utils/graphQLmodifier.js | 59 ++++++++++++++++++++++++++++- server/firebase/firebase-handler.js | 2 +- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/client/src/utils/graphQLmodifier.js b/client/src/utils/graphQLmodifier.js index d164382a9..38c2f6b1b 100644 --- a/client/src/utils/graphQLmodifier.js +++ b/client/src/utils/graphQLmodifier.js @@ -211,6 +211,54 @@ export function applySorters(ast, sorters) { }); } +/** + * Apply Top Level Sub to the AST + * @param node + * @param fieldPath + * @param filterField + */ +function applyTopLevelSub(node, fieldPath, filterField) { + // Find or create the where argument for the top-level subfield + let whereArg = node.selectionSet.selections + .find(selection => selection.name.value === fieldPath[0]) + ?.arguments.find(arg => arg.name.value === 'where'); + + if (!whereArg) { + whereArg = { + kind: Kind.ARGUMENT, + name: {kind: Kind.NAME, value: 'where'}, + value: {kind: Kind.OBJECT, fields: []}, + }; + const topLevelSubSelection = node.selectionSet.selections.find(selection => + selection.name.value === fieldPath[0] + ); + if (topLevelSubSelection) { + topLevelSubSelection.arguments = topLevelSubSelection.arguments || []; + topLevelSubSelection.arguments.push(whereArg); + } + } + + // Correctly position the nested filter without an extra 'where' + if (fieldPath.length > 2) { // More than one level deep + let currentField = whereArg.value; + fieldPath.slice(1, -1).forEach((path, index) => { + let existingField = currentField.fields.find(f => f.name.value === path); + if (!existingField) { + existingField = { + kind: Kind.OBJECT_FIELD, + name: {kind: Kind.NAME, value: path}, + value: {kind: Kind.OBJECT, fields: []} + }; + currentField.fields.push(existingField); + } + currentField = existingField.value; + }); + currentField.fields.push(filterField); + } else { // Directly under the top level + whereArg.value.fields.push(filterField); + } +} + /** * Apply filters to the AST * @param ast @@ -224,12 +272,17 @@ export function applyFilters(ast, filters) { filters.forEach(filter => { const fieldPath = filter.field.split('.'); let topLevel = false; + let topLevelSub = false; // Determine if the filter should be applied at the top level - if (fieldPath[0].startsWith('[') && fieldPath[0].endsWith(']')) { - fieldPath[0] = fieldPath[0].substring(1, fieldPath[0].length - 1); // Strip the brackets + if (fieldPath.length === 2) { topLevel = true; } + + if ( fieldPath.length > 2 && fieldPath[0].startsWith('[') && fieldPath[0].endsWith(']')) { + fieldPath[0] = fieldPath[0].substring(1, fieldPath[0].length - 1); // Strip the brackets + topLevelSub = true; + } // Construct the filter for a top-level application const targetFieldName = fieldPath[fieldPath.length - 1]; @@ -239,6 +292,8 @@ export function applyFilters(ast, filters) { if (topLevel) { applyTopLevelFilter(node, fieldPath, filterField); + } else if (topLevelSub) { + applyTopLevelSub(node, fieldPath, filterField); } else { applyNestedFilter(node, fieldPath, filterField); } diff --git a/server/firebase/firebase-handler.js b/server/firebase/firebase-handler.js index 049569881..d04879bd1 100644 --- a/server/firebase/firebase-handler.js +++ b/server/firebase/firebase-handler.js @@ -160,7 +160,7 @@ exports.sendNotification = async (req, res) => { .send({ topic: "PRD_PATRICK-messaging", notification: { - title: `ImEX Online Message - , + title: `ImEX Online Message -` , body: "Test Noti.", //imageUrl: "https://thinkimex.com/img/io-fcm.png", },