-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnodes.py
More file actions
188 lines (152 loc) · 6.2 KB
/
nodes.py
File metadata and controls
188 lines (152 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""Defines plugins for AiiDA nodes."""
# pylint: disable=redefined-builtin,too-few-public-methods,unused-argument
from typing import Any, Dict, List, Optional
import graphene as gr
from aiida import orm
from aiida_restapi.graphql.filter_syntax import parse_filter_str
from aiida_restapi.graphql.plugins import QueryPlugin
from .comments import CommentsQuery
from .logs import LogsQuery
from .orm_factories import (
ENTITY_DICT_TYPE,
fields_from_name,
multirow_cls_factory,
resolve_entity,
single_cls_factory,
)
from .utils import JSON, FilterString
Link = type('LinkObjectType', (gr.ObjectType,), fields_from_name('Link'))
class LinkQuery(gr.ObjectType):
"""A link and its end node."""
link = gr.Field(Link)
# note: we must refer to this query using a string, to prevent circular dependencies
node = gr.Field('aiida_restapi.graphql.nodes.NodeQuery')
class LinksQuery(multirow_cls_factory(LinkQuery, orm.nodes.Node, 'nodes')): # type: ignore[misc]
"""Query all AiiDA Links."""
class NodeQuery(
single_cls_factory(orm.nodes.Node, exclude_fields=('attributes', 'extras')) # type: ignore[misc]
):
"""Query an AiiDA Node"""
attributes = JSON(
description='Variable attributes of the node',
filter=gr.List(
gr.String,
description='return an exact set of attributes keys (non-existent will return null)',
),
)
extras = JSON(
description='Variable extras (unsealed) of the node',
filter=gr.List(
gr.String,
description='return an exact set of extras keys (non-existent will return null)',
),
)
# TODO it would be ideal if the attributes/extras were filtered via the SQL query
@staticmethod
def resolve_attributes(parent: Any, info: gr.ResolveInfo, filter: Optional[List[str]] = None) -> Dict[str, Any]:
"""Resolution function."""
attributes = parent.get('attributes')
if filter is None or attributes is None:
return attributes
return {key: attributes.get(key) for key in filter}
@staticmethod
def resolve_extras(parent: Any, info: gr.ResolveInfo, filter: Optional[List[str]] = None) -> Dict[str, Any]:
"""Resolution function."""
extras = parent.get('extras')
if filter is None or extras is None:
return extras
return {key: extras.get(key) for key in filter}
comments = gr.Field(CommentsQuery, description='Comments attached to a node')
@staticmethod
def resolve_comments(parent: Any, info: gr.ResolveInfo) -> dict:
"""Resolution function."""
# pass filter specification to CommentsQuery
filters = {}
filters['dbnode_id'] = parent['id']
return {'filters': filters}
logs = gr.Field(LogsQuery, description='Logs attached to a process node')
@staticmethod
def resolve_logs(parent: Any, info: gr.ResolveInfo) -> dict:
"""Resolution function."""
# pass filter specification to CommentsQuery
filters = {}
filters['dbnode_id'] = parent['id']
return {'filters': filters}
incoming = gr.Field(LinksQuery, description='Query for incoming nodes', filters=FilterString())
@staticmethod
def resolve_incoming(parent: Any, info: gr.ResolveInfo, filters: Optional[str] = None) -> dict:
"""Resolution function."""
# pass edge specification to LinksQuery
return {
'parent_id': parent['id'],
# this node is outgoing relative to the incoming nodes
'edge_type': 'outgoing',
'project_edge': True,
'filters': parse_filter_str(filters),
}
outgoing = gr.Field(LinksQuery, description='Query for outgoing nodes', filters=FilterString())
@staticmethod
def resolve_outgoing(parent: Any, info: gr.ResolveInfo, filters: Optional[str] = None) -> dict:
"""Resolution function."""
# pass edge specification to LinksQuery
return {
'parent_id': parent['id'],
# this node is incoming relative to the outgoing nodes
'edge_type': 'incoming',
'project_edge': True,
'filters': parse_filter_str(filters),
}
ancestors = gr.Field(
'aiida_restapi.graphql.nodes.NodesQuery',
description='Query for ancestor nodes',
filters=FilterString(),
)
@staticmethod
def resolve_ancestors(parent: Any, info: gr.ResolveInfo, filters: Optional[str] = None) -> dict:
"""Resolution function."""
# pass edge specification to LinksQuery
return {
'parent_id': parent['id'],
# this node is a descendant relative to the ancestor nodes
'edge_type': 'descendants',
'filters': parse_filter_str(filters),
}
descendants = gr.Field(
'aiida_restapi.graphql.nodes.NodesQuery',
description='Query for descendant nodes',
filters=FilterString(),
)
@staticmethod
def resolve_descendants(parent: Any, info: gr.ResolveInfo, filters: Optional[str] = None) -> dict:
"""Resolution function."""
# pass edge specification to LinksQuery
return {
'parent_id': parent['id'],
# this node is an ancestor relative to the descendant nodes
'edge_type': 'ancestors',
'filters': parse_filter_str(filters),
}
class NodesQuery(multirow_cls_factory(NodeQuery, orm.nodes.Node, 'nodes')): # type: ignore[misc]
"""Query all AiiDA Nodes"""
def resolve_Node(
parent: Any,
info: gr.ResolveInfo,
id: Optional[int] = None,
uuid: Optional[str] = None,
) -> ENTITY_DICT_TYPE:
"""Resolution function."""
return resolve_entity(orm.nodes.Node, info, id, uuid)
def resolve_Nodes(parent: Any, info: gr.ResolveInfo, filters: Optional[str] = None) -> dict:
"""Resolution function."""
# pass filter to NodesQuery
return {'filters': parse_filter_str(filters)}
NodeQueryPlugin = QueryPlugin(
'node',
gr.Field(NodeQuery, id=gr.Int(), uuid=gr.String(), description='Query for a single Node'),
resolve_Node,
)
NodesQueryPlugin = QueryPlugin(
'nodes',
gr.Field(NodesQuery, description='Query for multiple Nodes', filters=FilterString()),
resolve_Nodes,
)