Skip to content

Commit 7c00c57

Browse files
committed
Handle no child edge case
1 parent beefad4 commit 7c00c57

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies = [
1717
"pvi~=0.10.0",
1818
"pytango",
1919
"softioc",
20+
"strawberry-graphql[fastapi]",
2021
]
2122
dynamic = ["version"]
2223
license.file = "LICENSE"
@@ -43,7 +44,6 @@ dev = [
4344
"types-mock",
4445
"aioca",
4546
"p4p",
46-
"strawberry-graphql[fastapi]",
4747
]
4848

4949
[project.scripts]

src/fastcs/backends/graphQL/graphQL.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ def _dynamic_field():
9999
return strawberry.field(_dynamic_field)
100100

101101

102+
class NodeNotFoundError(Exception):
103+
pass
104+
105+
102106
class FieldsTree:
103107
def __init__(self, name: str):
104108
self.name = name
@@ -108,28 +112,34 @@ def __init__(self, name: str):
108112
"mutation": [],
109113
}
110114

111-
def insert(self, path: list[str]):
115+
def insert(self, path: list[str]) -> "FieldsTree":
112116
# Create child if not exist
113117
name = path.pop(0)
114-
if not (child := self.get_child(name)):
118+
if self.is_child(name):
119+
child = self.get_child(name)
120+
else:
115121
child = FieldsTree(name)
116122
self.children.append(child)
117-
else:
118-
child = self.get_child(name)
119123

120124
# Recurse if needed
121125
if path:
122126
return child.insert(path) # type: ignore
123127
else:
124128
return child
125129

126-
def get_child(self, name: str):
130+
def is_child(self, name: str) -> bool:
131+
for child in self.children:
132+
if child.name == name:
133+
return True
134+
return False
135+
136+
def get_child(self, name: str) -> "FieldsTree":
127137
for child in self.children:
128138
if child.name == name:
129139
return child
130-
return None
140+
raise NodeNotFoundError
131141

132-
def create_type(self, strawberry_type: str):
142+
def create_type(self, strawberry_type: str) -> type:
133143
for child in self.children:
134144
child_field = _wrap_as_field(
135145
child.name,

0 commit comments

Comments
 (0)