From 12ecc5639836ddefc80e34b9cd5463523ec27628 Mon Sep 17 00:00:00 2001 From: Regi Date: Thu, 11 Apr 2024 16:19:08 +0200 Subject: [PATCH] Update to extract nested objects Updating graphQL to extract nested objects like response.data.allmovies.special.flims.names --- .../noodl-graphql-module/module/src/index.js | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/modules/noodl-graphql-module/module/src/index.js b/modules/noodl-graphql-module/module/src/index.js index e070f73..84e5043 100644 --- a/modules/noodl-graphql-module/module/src/index.js +++ b/modules/noodl-graphql-module/module/src/index.js @@ -124,10 +124,36 @@ const GraphQLQueryNode = Noodl.defineNode({ getResult: function(name) { return this.results[name]; }, - extractResult:function(name,json) { - const result = Noodl.Array.get(); - result.set(json[name]); - return result; + extractResult: function(name, json) { + function findArray(obj, targetName) { + let resultArray = null; + + for (const key in obj) { + if (key === targetName && Array.isArray(obj[key])) { + resultArray = obj[key]; + } + + if (typeof obj[key] === 'object') { + const nestedArray = findArray(obj[key], targetName); + if (nestedArray) { + resultArray = nestedArray; // Update the result only if a nested array is found + } + } + } + + return resultArray; + } + + const arrayToExtract = findArray(json, name); + + if (arrayToExtract) { + const result = Noodl.Array.get(); + result.set(arrayToExtract); + return result; + } else { + console.error(`No array found for name '${name}'`); + return null; + } } }, setup: function (context, graphModel) { @@ -195,4 +221,4 @@ Noodl.defineModule({ setup() { //this is called once on startup } -}); \ No newline at end of file +});