Skip to content

Commit

Permalink
Merge pull request #10 from Checho3388/9-consider-__typename-as-metaf…
Browse files Browse the repository at this point in the history
…ield-with-0-complexity

9 consider   typename as metafield with 0 complexity
  • Loading branch information
Checho3388 committed Mar 14, 2024
2 parents 5fdfdd1 + 751f3a7 commit 8aa621f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "graphql_complexity"
version = "0.3.1"
version = "0.3.2"
description = "A python library that provides complexity calculation helpers for GraphQL"
authors = ["Checho3388 <[email protected]>"]
packages = [
Expand Down
5 changes: 2 additions & 3 deletions src/graphql_complexity/evaluator/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)

from graphql_complexity.config import Config
from graphql_complexity.evaluator.utils import get_node_argument_value
from graphql_complexity.evaluator.utils import get_node_argument_value, is_meta_type

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -107,8 +107,7 @@ def build_node(
) -> ComplexityNode:
"""Build a complexity node from a field node."""
type_ = type_info.get_type()
unwrapped_type = get_named_type(type_)
if unwrapped_type is not None and is_introspection_type(unwrapped_type):
if is_meta_type(type_, node):
return MetaField(name=node.name.value)
if isinstance(type_, GraphQLList):
return build_list_node(node, complexity, variables, config)
Expand Down
24 changes: 19 additions & 5 deletions src/graphql_complexity/evaluator/utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
from typing import Any

from graphql import DirectiveNode, FieldNode, VariableNode
from graphql import (
DirectiveNode,
FieldNode,
VariableNode,
get_named_type,
is_introspection_type,
)


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

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

return arg.value.value


def is_meta_type(type_, node) -> bool:
"""Check if the field is a meta field."""
unwrapped_type = get_named_type(type_)
return any(
(
unwrapped_type is not None and is_introspection_type(unwrapped_type),
unwrapped_type is not None and node.name.value == "__typename",
)
)
10 changes: 10 additions & 0 deletions tests/test_complexity_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def test_one_field_simple_complexity_calculation():
assert complexity == 1


def test_typename_has_zero_complexity():
query = """query {
__typename
}"""

complexity = _evaluate_complexity(query)

assert complexity == 0


def test_two_fields_simple_complexity_calculation():
query = """
query Something {
Expand Down

0 comments on commit 8aa621f

Please sign in to comment.