Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ariadne_codegen/client_generators/result_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,15 @@ def _resolve_selection_set(
self._fragments_used_as_mixins = self._fragments_used_as_mixins.union(
set(fragments)
)
return fields, fragments

seen_names: set[str] = set()
deduplicated: list[FieldNode] = []
for field in fields:
name = field.alias.value if field.alias else field.name.value
if name not in seen_names:
seen_names.add(name)
deduplicated.append(field)
return deduplicated, fragments

def _get_inline_fragment_root_type(
self, selection_value: str, root_type: str
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import ast

from graphql import OperationDefinitionNode, build_schema, parse

from ariadne_codegen.client_generators.constants import TYPENAME_ALIAS
from ariadne_codegen.client_generators.result_types import ResultTypesGenerator

SCHEMA = """
type Query {
foo: Foo
}

interface Bar {
id: ID!
}

interface Baz {
name: String!
}

type Foo implements Bar & Baz {
id: ID!
name: String!
}
"""


def test_no_duplicate_typename_when_type_implements_multiple_interfaces():
schema = build_schema(SCHEMA)

query_str = """
query GetFoo {
foo {
... on Bar {
__typename
id
}
... on Baz {
__typename
name
}
}
}
"""

document = parse(query_str)
operation = document.definitions[0]
assert isinstance(operation, OperationDefinitionNode)

generator = ResultTypesGenerator(
schema=schema,
operation_definition=operation,
enums_module_name="enums",
)

class_defs = generator.get_classes()
foo_class = next(c for c in class_defs if c.name == "GetFooFoo")

typename_fields = [
node
for node in foo_class.body
if isinstance(node, ast.AnnAssign)
and isinstance(node.target, ast.Name)
and node.target.id == TYPENAME_ALIAS
]

assert len(typename_fields) == 1