-
-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Helper + docs for selectionSet arguments (#1802)
* helper for selection set with arguments. * add tests and docs. * add missing type declaration. * fix GraphQL v15 test. * new selectionSet via shallow copies. * fix typing? * refactor args helper. * fix type name in documentation.
- Loading branch information
Greg MacWilliam
authored
Jul 21, 2020
1 parent
53f62bd
commit ad34939
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { stitchSchemas } from './stitchSchemas'; | ||
export { forwardArgsToSelectionSet } from './selectionSetArgs'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { parseSelectionSet } from '@graphql-tools/utils'; | ||
import { SelectionSetNode, SelectionNode, FieldNode, Kind } from 'graphql'; | ||
|
||
export const forwardArgsToSelectionSet: ( | ||
selectionSet: string, | ||
mapping?: Record<string, string[]> | ||
) => (field: FieldNode) => SelectionSetNode = (selectionSet: string, mapping?: Record<string, string[]>) => { | ||
const selectionSetDef = parseSelectionSet(selectionSet); | ||
return (field: FieldNode): SelectionSetNode => { | ||
const selections = selectionSetDef.selections.map( | ||
(selectionNode): SelectionNode => { | ||
if (selectionNode.kind === Kind.FIELD) { | ||
if (!mapping) { | ||
return { ...selectionNode, arguments: field.arguments.slice() }; | ||
} else if (selectionNode.name.value in mapping) { | ||
const selectionArgs = mapping[selectionNode.name.value]; | ||
return { | ||
...selectionNode, | ||
arguments: field.arguments.filter((arg): boolean => selectionArgs.includes(arg.name.value)), | ||
}; | ||
} | ||
} | ||
return selectionNode; | ||
} | ||
); | ||
|
||
return { ...selectionSetDef, selections }; | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { parseSelectionSet } from '@graphql-tools/utils'; | ||
import { forwardArgsToSelectionSet } from '../src'; | ||
|
||
describe('forwardArgsToSelectionSet', () => { | ||
|
||
const GATEWAY_FIELD = parseSelectionSet('{ posts(pageNumber: 1, perPage: 7) }').selections[0]; | ||
|
||
test('passes all arguments to a hint selection set', () => { | ||
const buildSelectionSet = forwardArgsToSelectionSet('{ postIds }'); | ||
const result = buildSelectionSet(GATEWAY_FIELD).selections[0]; | ||
|
||
expect(result.name.value).toEqual('postIds'); | ||
expect(result.arguments.length).toEqual(2); | ||
expect(result.arguments[0].name.value).toEqual('pageNumber'); | ||
expect(result.arguments[0].value.value).toEqual('1'); | ||
expect(result.arguments[1].name.value).toEqual('perPage'); | ||
expect(result.arguments[1].value.value).toEqual('7'); | ||
}); | ||
|
||
test('passes mapped arguments to a hint selection set', () => { | ||
const buildSelectionSet = forwardArgsToSelectionSet('{ id postIds }', { postIds: ['pageNumber'] }); | ||
const result = buildSelectionSet(GATEWAY_FIELD); | ||
|
||
expect(result.selections.length).toEqual(2); | ||
expect(result.selections[0].name.value).toEqual('id'); | ||
expect(result.selections[0].arguments.length).toEqual(0); | ||
|
||
expect(result.selections[1].name.value).toEqual('postIds'); | ||
expect(result.selections[1].arguments.length).toEqual(1); | ||
expect(result.selections[1].arguments[0].name.value).toEqual('pageNumber'); | ||
expect(result.selections[1].arguments[0].value.value).toEqual('1'); | ||
}); | ||
}); |
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