Created
September 2, 2021 21:39
-
-
Save harrisoncramer/cbca46a5b9af84a58932d4f22ef4c7c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
query { | |
characters { | |
info { | |
count | |
} | |
results { | |
id | |
name | |
} | |
} | |
character(id: "1") { | |
name | |
id | |
} | |
} | |
*/ | |
function normalizeResult(result) { | |
const rootQueryObject = {}; | |
const denestedObjects = []; | |
for (key in result) { | |
rootQueryObject[key] = `loql__${key}`; | |
recurse({ data: result[key], parentKey: key }); | |
} | |
function recurse({ data, parentKey }) { | |
const result = {}; | |
for (key in data) { | |
const value = data[key]; | |
if (Array.isArray(value)) { | |
let childKey = parentKey + '_' + key; | |
result[key] = value.map((subArrayVal) => { | |
if (typeof subArrayVal === 'object' && subArrayVal !== null) { | |
const uniqueKey = childKey + '_' + subArrayVal.id; | |
return recurse({ data: subArrayVal, parentKey: uniqueKey }); | |
} | |
return subArrayVal; | |
}); | |
} else if (typeof value === 'object' && value !== null) { | |
let childKey = parentKey + '_' + key; | |
result[key] = recurse({ data: value, parentKey: childKey }); | |
} else { | |
result[key] = value; | |
} | |
} | |
denestedObjects.push({ [parentKey]: result }); | |
return 'loql__' + parentKey; | |
} | |
return { rootQueryObject, denestedObjects }; | |
} | |
const data = { | |
data: { | |
characters: { | |
info: { | |
count: 671, | |
}, | |
results: [ | |
{ | |
id: '1', | |
name: 'Rick Sanchez', | |
}, | |
{ | |
id: '2', | |
name: 'Morty Smith', | |
}, | |
{ | |
id: '3', | |
name: 'Summer Smith', | |
}, | |
{ | |
id: '4', | |
name: 'Beth Smith', | |
}, | |
{ | |
id: '5', | |
name: 'Jerry Smith', | |
}, | |
{ | |
id: '6', | |
name: 'Abadango Cluster Princess', | |
}, | |
{ | |
id: '7', | |
name: 'Abradolf Lincler', | |
}, | |
{ | |
id: '8', | |
name: 'Adjudicator Rick', | |
}, | |
{ | |
id: '9', | |
name: 'Agency Director', | |
}, | |
{ | |
id: '10', | |
name: 'Alan Rails', | |
}, | |
{ | |
id: '11', | |
name: 'Albert Einstein', | |
}, | |
{ | |
id: '12', | |
name: 'Alexander', | |
}, | |
{ | |
id: '13', | |
name: 'Alien Googah', | |
}, | |
{ | |
id: '14', | |
name: 'Alien Morty', | |
}, | |
{ | |
id: '15', | |
name: 'Alien Rick', | |
}, | |
{ | |
id: '16', | |
name: 'Amish Cyborg', | |
}, | |
{ | |
id: '17', | |
name: 'Annie', | |
}, | |
{ | |
id: '18', | |
name: 'Antenna Morty', | |
}, | |
{ | |
id: '19', | |
name: 'Antenna Rick', | |
}, | |
{ | |
id: '20', | |
name: 'Ants in my Eyes Johnson', | |
}, | |
], | |
}, | |
character: { | |
name: 'Rick Sanchez', | |
id: '1', | |
}, | |
}, | |
}; | |
const { rootQueryObject, denestedObjects } = normalizeResult(data.data); | |
console.log('ROOT QUERY: ', rootQueryObject); | |
denestedObjects.forEach((o) => console.log(o)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment