Debian Codegraph @

src/graphql/language/predicates.py

raw
1 from .ast import (
2 ArgumentCoordinateNode,
3 DirectiveArgumentCoordinateNode,
4 DirectiveCoordinateNode,
5 MemberCoordinateNode,
6 Node,
7 DefinitionNode,
8 DirectiveExtensionNode,
9 ExecutableDefinitionNode,
10 ListValueNode,
11 ObjectValueNode,
12 SchemaExtensionNode,
13 SelectionNode,
14 TypeCoordinateNode,
15 TypeDefinitionNode,
16 TypeExtensionNode,
17 TypeNode,
18 TypeSystemDefinitionNode,
19 ValueNode,
20 VariableNode,
21 )
22
23 __all__ = [
24 "is_definition_node",
25 "is_executable_definition_node",
26 "is_selection_node",
27 "is_value_node",
28 "is_const_value_node",
29 "is_type_node",
30 "is_type_system_definition_node",
31 "is_type_definition_node",
32 "is_type_system_extension_node",
33 "is_type_extension_node",
34 "is_schema_coordinate_node",
35 ]
36
37
38 def is_definition_node(node: Node) -> bool:
39 """Check whether the given node represents a definition."""
40 return isinstance(node, DefinitionNode)
41
42
43 def is_executable_definition_node(node: Node) -> bool:
44 """Check whether the given node represents an executable definition."""
45 return isinstance(node, ExecutableDefinitionNode)
46
47
48 def is_selection_node(node: Node) -> bool:
49 """Check whether the given node represents a selection."""
50 return isinstance(node, SelectionNode)
51
52
53 def is_value_node(node: Node) -> bool:
54 """Check whether the given node represents a value."""
55 return isinstance(node, ValueNode)
56
57
58 def is_const_value_node(node: Node) -> bool:
59 """Check whether the given node represents a constant value."""
60 return is_value_node(node) and (
61 any(is_const_value_node(value) for value in node.values)
62 if isinstance(node, ListValueNode)
63 else (
64 any(is_const_value_node(field.value) for field in node.fields)
65 if isinstance(node, ObjectValueNode)
66 else not isinstance(node, VariableNode)
67 )
68 )
69
70
71 def is_type_node(node: Node) -> bool:
72 """Check whether the given node represents a type."""
73 return isinstance(node, TypeNode)
74
75
76 def is_type_system_definition_node(node: Node) -> bool:
77 """Check whether the given node represents a type system definition."""
78 return isinstance(node, TypeSystemDefinitionNode)
79
80
81 def is_type_definition_node(node: Node) -> bool:
82 """Check whether the given node represents a type definition."""
83 return isinstance(node, TypeDefinitionNode)
84
85
86 def is_type_system_extension_node(node: Node) -> bool:
87 """Check whether the given node represents a type system extension."""
88 return isinstance(
89 node, (SchemaExtensionNode, DirectiveExtensionNode, TypeExtensionNode)
90 )
91
92
93 def is_type_extension_node(node: Node) -> bool:
94 """Check whether the given node represents a type extension."""
95 return isinstance(node, TypeExtensionNode)
96
97
98 def is_schema_coordinate_node(node: Node) -> bool:
99 """Check whether the given node represents a schema coordinate."""
100 return isinstance(
101 node,
102 (
103 TypeCoordinateNode,
104 MemberCoordinateNode,
105 ArgumentCoordinateNode,
106 DirectiveCoordinateNode,
107 DirectiveArgumentCoordinateNode,
108 ),
109 )