Skip to content

Instantly share code, notes, and snippets.

@lastmjs
Created March 12, 2018 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lastmjs/416693c798b165a246329b3b92801c02 to your computer and use it in GitHub Desktop.
Save lastmjs/416693c798b165a246329b3b92801c02 to your computer and use it in GitHub Desktop.
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