Created
March 12, 2018 21:30
-
-
Save lastmjs/416693c798b165a246329b3b92801c02 to your computer and use it in GitHub Desktop.
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
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