Skip to content

Commit 8aa621f

Browse files
authored
Merge pull request #10 from Checho3388/9-consider-__typename-as-metafield-with-0-complexity
9 consider typename as metafield with 0 complexity
2 parents 5fdfdd1 + 751f3a7 commit 8aa621f

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "graphql_complexity"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
description = "A python library that provides complexity calculation helpers for GraphQL"
55
authors = ["Checho3388 <ezequiel.grondona@gmail.com>"]
66
packages = [

src/graphql_complexity/evaluator/nodes.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
)
1111

1212
from graphql_complexity.config import Config
13-
from graphql_complexity.evaluator.utils import get_node_argument_value
13+
from graphql_complexity.evaluator.utils import get_node_argument_value, is_meta_type
1414

1515
logger = logging.getLogger(__name__)
1616

@@ -107,8 +107,7 @@ def build_node(
107107
) -> ComplexityNode:
108108
"""Build a complexity node from a field node."""
109109
type_ = type_info.get_type()
110-
unwrapped_type = get_named_type(type_)
111-
if unwrapped_type is not None and is_introspection_type(unwrapped_type):
110+
if is_meta_type(type_, node):
112111
return MetaField(name=node.name.value)
113112
if isinstance(type_, GraphQLList):
114113
return build_list_node(node, complexity, variables, config)
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
from typing import Any
22

3-
from graphql import DirectiveNode, FieldNode, VariableNode
3+
from graphql import (
4+
DirectiveNode,
5+
FieldNode,
6+
VariableNode,
7+
get_named_type,
8+
is_introspection_type,
9+
)
410

511

612
def get_node_argument_value(node: FieldNode | DirectiveNode, arg_name: str, variables: dict[str, Any]) -> Any:
713
"""Returns the value of the argument given by parameter."""
8-
arg = next(
9-
(arg for arg in node.arguments if arg.name.value == arg_name),
10-
None
11-
)
14+
arg = next((arg for arg in node.arguments if arg.name.value == arg_name), None)
1215
if not arg:
1316
raise ValueError(f"Value for {arg_name!r} not found in {node.name.value!r} arguments")
1417

1518
if isinstance(arg.value, VariableNode):
1619
return variables.get(arg.value.name.value)
1720

1821
return arg.value.value
22+
23+
24+
def is_meta_type(type_, node) -> bool:
25+
"""Check if the field is a meta field."""
26+
unwrapped_type = get_named_type(type_)
27+
return any(
28+
(
29+
unwrapped_type is not None and is_introspection_type(unwrapped_type),
30+
unwrapped_type is not None and node.name.value == "__typename",
31+
)
32+
)

tests/test_complexity_visitor.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ def test_one_field_simple_complexity_calculation():
2424
assert complexity == 1
2525

2626

27+
def test_typename_has_zero_complexity():
28+
query = """query {
29+
__typename
30+
}"""
31+
32+
complexity = _evaluate_complexity(query)
33+
34+
assert complexity == 0
35+
36+
2737
def test_two_fields_simple_complexity_calculation():
2838
query = """
2939
query Something {

0 commit comments

Comments
 (0)