From 632ec966f04cd50d29e1225a96bb2aaacf38ce52 Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Sun, 19 Nov 2023 17:34:35 +0100 Subject: [PATCH] Fix missing empty directives in DSL nodes (#448) --- gql/dsl.py | 13 +++- .../test_dsl_directives.py | 59 +++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 tests/regressions/issue_447_dsl_missing_directives/test_dsl_directives.py diff --git a/gql/dsl.py b/gql/dsl.py index adc48bea..0c834b33 100644 --- a/gql/dsl.py +++ b/gql/dsl.py @@ -503,6 +503,7 @@ def executable_ast(self) -> OperationDefinitionNode: selection_set=self.selection_set, variable_definitions=self.variable_definitions.get_ast_definitions(), **({"name": NameNode(value=self.name)} if self.name else {}), + directives=(), ) def __repr__(self) -> str: @@ -597,6 +598,7 @@ def get_ast_definitions(self) -> Tuple[VariableDefinitionNode, ...]: default_value=None if var.default_value is None else ast_from_value(var.default_value, var.type), + directives=(), ) for var in self.variables.values() if var.type is not None # only variables used @@ -818,7 +820,11 @@ def __init__( """ self.parent_type = parent_type self.field = field - self.ast_field = FieldNode(name=NameNode(value=name), arguments=()) + self.ast_field = FieldNode( + name=NameNode(value=name), + arguments=(), + directives=(), + ) self.dsl_type = dsl_type log.debug(f"Creating {self!r}") @@ -950,7 +956,7 @@ def __init__( log.debug(f"Creating {self!r}") - self.ast_field = InlineFragmentNode() + self.ast_field = InlineFragmentNode(directives=()) DSLSelector.__init__(self, *fields, **fields_with_alias) @@ -1018,7 +1024,7 @@ def ast_field(self) -> FragmentSpreadNode: # type: ignore `issue #4125 of mypy `_. """ - spread_node = FragmentSpreadNode() + spread_node = FragmentSpreadNode(directives=()) spread_node.name = NameNode(value=self.name) return spread_node @@ -1067,6 +1073,7 @@ def executable_ast(self) -> FragmentDefinitionNode: selection_set=self.selection_set, variable_definitions=self.variable_definitions.get_ast_definitions(), name=NameNode(value=self.name), + directives=(), ) def __repr__(self) -> str: diff --git a/tests/regressions/issue_447_dsl_missing_directives/test_dsl_directives.py b/tests/regressions/issue_447_dsl_missing_directives/test_dsl_directives.py new file mode 100644 index 00000000..61cc21e9 --- /dev/null +++ b/tests/regressions/issue_447_dsl_missing_directives/test_dsl_directives.py @@ -0,0 +1,59 @@ +from gql import Client +from gql.dsl import DSLFragment, DSLQuery, DSLSchema, dsl_gql + +schema_str = """ +type MonsterForm { + sprites: MonsterFormSprites! +} + +union SpriteUnion = Sprite | CopyOf + +type Query { + monster: [Monster!]! +} + +type MonsterFormSprites { + actions: [SpriteUnion!]! +} + +type CopyOf { + action: String! +} + +type Monster { + manual(path: String!): MonsterForm +} + +type Sprite { + action: String! +} +""" + + +def test_issue_447(): + + client = Client(schema=schema_str) + ds = DSLSchema(client.schema) + + sprite = DSLFragment("SpriteUnionAsSprite") + sprite.on(ds.Sprite) + sprite.select( + ds.Sprite.action, + ) + copy_of = DSLFragment("SpriteUnionAsCopyOf") + copy_of.on(ds.CopyOf) + copy_of.select( + ds.CopyOf.action, + ) + + query = ds.Query.monster.select( + ds.Monster.manual(path="").select( + ds.MonsterForm.sprites.select( + ds.MonsterFormSprites.actions.select(sprite, copy_of), + ), + ), + ) + + q = dsl_gql(sprite, copy_of, DSLQuery(query)) + + client.validate(q)