Skip to content

Commit

Permalink
Handle FragmentSpread selections nested inside InlineFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
sgarner committed Apr 15, 2021
1 parent f73e4f7 commit 2a10e33
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/RelationMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class RelationMapper {
return;
}

const foundNode = this.findFieldInSelection(fieldName, selectionSet);
const foundNode = this.findFieldInSelection(fieldName, selectionSet, info.fragments);

if (foundNode == null) {
fieldPathNodes.push(null);
Expand Down Expand Up @@ -186,16 +186,26 @@ export class RelationMapper {
}
}

private findFieldInSelection(fieldName: string, selectionSet: SelectionSetNode): SelectionNode | undefined {
private findFieldInSelection(
fieldName: string,
selectionSet: SelectionSetNode,
fragments?: Record<string, FragmentDefinitionNode>,
): SelectionNode | undefined {
let foundNode: SelectionNode | undefined = undefined;

for (const selectionNode of selectionSet.selections) {
if (foundNode !== undefined) {
break;
}

if (selectionNode.kind === 'InlineFragment') {
foundNode = this.findFieldInSelection(fieldName, selectionNode.selectionSet);
if (selectionNode.kind === 'InlineFragment' || selectionNode.kind === 'FragmentSpread') {
const selectionSet = this.getSelectionSetFromNode(selectionNode, fragments);

if (selectionSet === undefined) {
break;
}

foundNode = this.findFieldInSelection(fieldName, selectionSet, fragments);

break;
}
Expand Down

0 comments on commit 2a10e33

Please sign in to comment.