Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
export function addFragmentToFieldResolvers(schemaAST, fragmentSelection) {
return schemaAST.definitions.reduce((result, schemaDefinition) => {
if (schemaDefinition.kind === 'ObjectTypeDefinition') {
return {
...result,
[schemaDefinition.name.value]: schemaDefinition.fields.reduce((result, fieldDefinition) => {
//TODO this includes check is naive and will break for some strings
if (fragmentSelection.includes(fieldDefinition.name.value)) {
return result;
}
return {
...result,
[fieldDefinition.name.value]: {
fragment: `fragment Fragment on ${schemaDefinition.name.value} ${fragmentSelection}`,
resolve: (parent, args, context, info) => {
return parent[fieldDefinition.name.value];
}
}
};
}, {})
};
}
else {
return result;
}
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.