-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
413 additions
and
24 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
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
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,53 @@ | ||
from typing import Dict, Sequence, List, cast | ||
|
||
from graphql import FieldNode, SelectionNode, SelectionSetNode | ||
|
||
|
||
def merge_selection_sets( | ||
set_a: SelectionSetNode, set_b: SelectionSetNode | ||
) -> SelectionSetNode: | ||
return SelectionSetNode( | ||
selections=tuple(merge_selections(set_a.selections, set_b.selections)), | ||
) | ||
|
||
|
||
def merge_selections( | ||
set_a: Sequence[SelectionNode], set_b: Sequence[SelectionNode] | ||
) -> List[SelectionNode]: | ||
final_set: List[SelectionNode] = list(set_a) | ||
|
||
index: Dict[str, int] = {} | ||
for i, field in enumerate(final_set): | ||
if isinstance(field, FieldNode): | ||
index[(field.alias or field.name).value] = i | ||
|
||
for field in set_b: | ||
if isinstance(field, FieldNode): | ||
field_name = (field.alias or field.name).value | ||
if field_name in index: | ||
field_index = index[field_name] | ||
other_field = cast(FieldNode, final_set[field_index]) | ||
if other_field.selection_set and field.selection_set: | ||
final_set[field_index] = FieldNode( | ||
directives=other_field.directives, | ||
alias=other_field.alias, | ||
name=field.name, | ||
arguments=other_field.arguments, | ||
selection_set=merge_selection_sets( | ||
other_field.selection_set, field.selection_set | ||
), | ||
) | ||
elif other_field.selection_set or field.selection_set: | ||
final_set[field_index] = FieldNode( | ||
directives=other_field.directives, | ||
alias=other_field.alias, | ||
name=field.name, | ||
arguments=other_field.arguments, | ||
selection_set=( | ||
other_field.selection_set or field.selection_set | ||
), | ||
) | ||
else: | ||
final_set.append(field) | ||
|
||
return final_set |
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,82 @@ | ||
from textwrap import dedent | ||
|
||
from graphql import parse, print_ast | ||
|
||
from ariadne_graphql_proxy import merge_selection_sets | ||
|
||
|
||
def test_merge_selection_sets_merges_two_flat_sets(): | ||
set_a = parse("{ hello }").definitions[0].selection_set | ||
set_b = parse("{ world }").definitions[0].selection_set | ||
|
||
result = merge_selection_sets(set_a, set_b) | ||
assert ( | ||
print_ast(result) | ||
== dedent( | ||
""" | ||
{ | ||
hello | ||
world | ||
} | ||
""" | ||
).strip() | ||
) | ||
|
||
|
||
def test_merge_selection_sets_merges_two_overlapping_flat_sets(): | ||
set_a = parse("{ hello world }").definitions[0].selection_set | ||
set_b = parse("{ world }").definitions[0].selection_set | ||
|
||
result = merge_selection_sets(set_a, set_b) | ||
assert ( | ||
print_ast(result) | ||
== dedent( | ||
""" | ||
{ | ||
hello | ||
world | ||
} | ||
""" | ||
).strip() | ||
) | ||
|
||
|
||
def test_merge_selection_sets_keeps_nested_selections(): | ||
set_a = parse("{ hello { sub } }").definitions[0].selection_set | ||
set_b = parse("{ world }").definitions[0].selection_set | ||
|
||
result = merge_selection_sets(set_a, set_b) | ||
assert ( | ||
print_ast(result) | ||
== dedent( | ||
""" | ||
{ | ||
hello { | ||
sub | ||
} | ||
world | ||
} | ||
""" | ||
).strip() | ||
) | ||
|
||
|
||
def test_merge_selection_sets_merges_selection_sets_recursively(): | ||
set_a = parse("{ hello { sub } }").definitions[0].selection_set | ||
set_b = parse("{ hello { set } world }").definitions[0].selection_set | ||
|
||
result = merge_selection_sets(set_a, set_b) | ||
assert ( | ||
print_ast(result) | ||
== dedent( | ||
""" | ||
{ | ||
hello { | ||
sub | ||
set | ||
} | ||
world | ||
} | ||
""" | ||
).strip() | ||
) |
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,82 @@ | ||
from textwrap import dedent | ||
|
||
from graphql import SelectionSetNode, parse, print_ast | ||
|
||
from ariadne_graphql_proxy import merge_selections | ||
|
||
|
||
def test_merge_selections_merges_two_flat_sets(): | ||
set_a = parse("{ hello }").definitions[0].selection_set.selections | ||
set_b = parse("{ world }").definitions[0].selection_set.selections | ||
|
||
result = merge_selections(set_a, set_b) | ||
assert ( | ||
print_ast(SelectionSetNode(selections=result)) | ||
== dedent( | ||
""" | ||
{ | ||
hello | ||
world | ||
} | ||
""" | ||
).strip() | ||
) | ||
|
||
|
||
def test_merge_selections_merges_two_overlapping_flat_sets(): | ||
set_a = parse("{ hello world }").definitions[0].selection_set.selections | ||
set_b = parse("{ world }").definitions[0].selection_set.selections | ||
|
||
result = merge_selections(set_a, set_b) | ||
assert ( | ||
print_ast(SelectionSetNode(selections=result)) | ||
== dedent( | ||
""" | ||
{ | ||
hello | ||
world | ||
} | ||
""" | ||
).strip() | ||
) | ||
|
||
|
||
def test_merge_selections_keeps_nested_selections(): | ||
set_a = parse("{ hello { sub } }").definitions[0].selection_set.selections | ||
set_b = parse("{ world }").definitions[0].selection_set.selections | ||
|
||
result = merge_selections(set_a, set_b) | ||
assert ( | ||
print_ast(SelectionSetNode(selections=result)) | ||
== dedent( | ||
""" | ||
{ | ||
hello { | ||
sub | ||
} | ||
world | ||
} | ||
""" | ||
).strip() | ||
) | ||
|
||
|
||
def test_merge_selections_merges_selection_sets_recursively(): | ||
set_a = parse("{ hello { sub } }").definitions[0].selection_set.selections | ||
set_b = parse("{ hello { set } world }").definitions[0].selection_set.selections | ||
|
||
result = merge_selections(set_a, set_b) | ||
assert ( | ||
print_ast(SelectionSetNode(selections=result)) | ||
== dedent( | ||
""" | ||
{ | ||
hello { | ||
sub | ||
set | ||
} | ||
world | ||
} | ||
""" | ||
).strip() | ||
) |
Oops, something went wrong.