Skip to content
Merged
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
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 <ezequiel.grondona@gmail.com>"]
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