Created
December 21, 2018 11:29
-
-
Save Jalle19/1ca5081f220e83e1015fd661ee4e877c to your computer and use it in GitHub Desktop.
createSelectionSetAppendingTransform
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
import { WrapQuery } from 'graphql-tools' | |
import { SelectionSetNode, Kind } from 'graphql' | |
/** | |
* Creates a WrapQuery schema transform that appends the specified field to the selection set of the | |
* specified parent field name. This can be used to assure queries used by dataloader include the | |
* field used to align the results (usually some "id" field) | |
* @param parentFieldName | |
* @param appendedFieldName | |
*/ | |
const createSelectionSetAppendingTransform = (parentFieldName: string, appendedFieldName: string) => { | |
return new WrapQuery( | |
// Modify the specified field's selection set | |
[parentFieldName], | |
(selectionSet: SelectionSetNode) => { | |
const newSelection = { | |
kind: Kind.FIELD, | |
name: { | |
kind: Kind.NAME, | |
value: appendedFieldName | |
} | |
}; | |
// @ts-ignore the selection set is technically read only | |
selectionSet.selections.push(newSelection) | |
return selectionSet | |
}, | |
result => result, | |
) | |
} | |
export { | |
createSelectionSetAppendingTransform as createSelectionSetAlteringTransform | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @Jalle19 ,
Is there a way I could use something like this to solve this issue of ours?