Last active
September 2, 2021 21:58
-
-
Save harrisoncramer/58e67741695ed70f4f82ceeae7401b34 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
/* | |
https://rickandmortyapi.com/graphql | |
I'm trying various queries, but they all seem to be working. Here's an example: | |
*/ | |
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; | |
} | |
} | |
// This is to check if the top-level value is an array or not. | |
if (rootQueryObject[parentKey]) { | |
denestedObjects.push({ [parentKey]: Object.values(result) }); | |
} else { | |
denestedObjects.push({ [parentKey]: result }); | |
} | |
return 'loql__' + parentKey; | |
} | |
return { rootQueryObject, denestedObjects }; | |
} | |
// const data1 = { | |
// data: { | |
// locationsByIds: [ | |
// { | |
// name: 'Earth (Unknown dimension)', | |
// id: '30', | |
// residents: [ | |
// { | |
// name: 'Diane Sanchez', | |
// id: '94', | |
// episode: [ | |
// { | |
// id: '22', | |
// name: 'The Rickshank Rickdemption', | |
// air_date: 'April 1, 2017', | |
// }, | |
// ], | |
// }, | |
// ], | |
// }, | |
// { | |
// name: 'Gazorpazorp', | |
// id: '40', | |
// residents: [ | |
// { | |
// name: 'Jackie', | |
// id: '168', | |
// episode: [ | |
// { | |
// id: '7', | |
// name: 'Raising Gazorpazorp', | |
// air_date: 'March 10, 2014', | |
// }, | |
// ], | |
// }, | |
// { | |
// name: 'Ma-Sha', | |
// id: '211', | |
// episode: [ | |
// { | |
// id: '7', | |
// name: 'Raising Gazorpazorp', | |
// air_date: 'March 10, 2014', | |
// }, | |
// ], | |
// }, | |
// { | |
// name: 'Veronica Ann Bennet', | |
// id: '376', | |
// episode: [ | |
// { | |
// id: '7', | |
// name: 'Raising Gazorpazorp', | |
// air_date: 'March 10, 2014', | |
// }, | |
// ], | |
// }, | |
// ], | |
// }, | |
// ], | |
// }, | |
// }; | |
// const data2 = { | |
// data: { | |
// locationsByIds: [ | |
// { | |
// name: 'Earth (Unknown dimension)', | |
// id: '30', | |
// }, | |
// { | |
// name: 'Gazorpazorp', | |
// id: '40', | |
// }, | |
// ], | |
// characters: { | |
// info: { | |
// count: 671, | |
// }, | |
// }, | |
// }, | |
// }; | |
// const { rootQueryObject, denestedObjects } = normalizeResult(data1.data); | |
// console.log('ROOT QUERY: ', rootQueryObject); | |
// denestedObjects.forEach((o) => console.log(o)); | |
// const { rootQueryObject, denestedObjects } = normalizeResult(data2.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