Skip to content

Instantly share code, notes, and snippets.

@Jalle19
Created December 21, 2018 11:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jalle19/1ca5081f220e83e1015fd661ee4e877c to your computer and use it in GitHub Desktop.
Save Jalle19/1ca5081f220e83e1015fd661ee4e877c to your computer and use it in GitHub Desktop.
createSelectionSetAppendingTransform
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
}
@iDVB
Copy link

iDVB commented Oct 21, 2021

Thanks @Jalle19 ,
Is there a way I could use something like this to solve this issue of ours?

@Jalle19
Copy link
Author

Jalle19 commented Oct 22, 2021

I have absolutely no idea 😄🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment