Skip to content

Instantly share code, notes, and snippets.

@psdcoder
Created January 23, 2019 15:31
Show Gist options
  • Save psdcoder/ac8f0e11f1f515df5b351d4d8a14fea1 to your computer and use it in GitHub Desktop.
Save psdcoder/ac8f0e11f1f515df5b351d4d8a14fea1 to your computer and use it in GitHub Desktop.
Fragment generator
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