Created
January 23, 2019 15:31
-
-
Save psdcoder/ac8f0e11f1f515df5b351d4d8a14fea1 to your computer and use it in GitHub Desktop.
Fragment generator
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
function pad(lvl) { | |
return ' '.repeat(2 * lvl); | |
} | |
function buildGraphStructure(data, lvl = 0) { | |
return Object.keys(data).reduce((acc, key) => { | |
if (data[key]) { | |
return ( | |
acc + | |
`${pad(lvl)}${key} {\n` + | |
`${buildGraphStructure(data[key], lvl + 1)}` + | |
`${pad(lvl)}}` + | |
'\n' | |
); | |
} | |
return acc + `${pad(lvl)}${key}\n`; | |
}, ''); | |
} | |
function buildGqlFragment(fragmentName, onType, pathsToUpdate) { | |
const dataStructure = pathsToUpdate.reduce( | |
(acc, path) => assocPath(path, null, acc), | |
{} | |
); | |
return gql` | |
fragment ${fragmentName} on ${onType} { | |
${buildGraphStructure(dataStructure)} | |
} | |
`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment