Created
September 18, 2021 15:41
-
-
Save geongeorge/154801a65ea73eba7915e0f2b898726b to your computer and use it in GitHub Desktop.
Clean graphql response : Remove edges, node and __typename from graphql response
This file contains 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
/** | |
* Remove edges, node and __typename from graphql response | |
* | |
* @param {Object} input - The graphql response | |
* @returns {Object} Clean graphql response | |
*/ | |
const cleanGraphQLResponse = function (input) { | |
if (!input) return null; | |
const isPrimitiveType = (test) => { | |
return test !== Object(test); | |
}; | |
if (isPrimitiveType(input)) return input; | |
const output = {}; | |
const isObject = (obj) => { | |
return obj !== null && typeof obj === 'object' && !Array.isArray(obj); | |
}; | |
Object.keys(input).forEach((key) => { | |
if (input[key] && input[key].edges) { | |
output[key] = input[key].edges.map((edge) => | |
cleanGraphQLResponse(edge.node), | |
); | |
} else if (isObject(input[key])) { | |
output[key] = cleanGraphQLResponse(input[key]); | |
} else if (key !== '__typename') { | |
output[key] = input[key]; | |
} | |
}); | |
return output; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on https://gist.github.com/ManUtopiK/469aec75b655d6a4d912aeb3b75af3c9