Node
Node interface for GraphQL types.
Subclasses must type the id field using NodeID . It will be private to the
schema because it will be converted to a global ID and exposed as id: GlobalID!
The following methods can also be implemented:
resolve_id:
(Optional) Called to resolve the node’s id. Can be overriden to
customize how the id is retrieved (e.g. in case you don’t want
to define a NodeID field)
resolve_nodes:
Called to retrieve an iterable of node given their ids
resolve_node:
(Optional) Called to retrieve a node given its id. If not defined
the default implementation will call .resolve_nodes with that
single node id.
Example:
import strawberry
@strawberry.typeclass Fruit(strawberry.relay.Node): id: strawberry.relay.NodeID[int] name: str
@classmethod def resolve_nodes(cls, *, info, node_ids, required=False): # Return an iterable of fruits in here ...Methods:
-
resolve_id_attr
Signature:
def resolve_id_attr(cls) -> str:... -
resolve_id
Resolve the node id.
By default this will return
getattr(root, <id_attr>), whereis the field typed with NodeID.You can override this method to provide a custom implementation.
Returns:
The resolved id (which is expected to be str)
Signature:
def resolve_id(cls, root: Self, info: Info) -> AwaitableOrValue[str]:...Parameters:
-
root:The node to resolve.
- Type
-
Self
-
info:The strawberry execution info resolve the type name from.
- Type
-
Info
-
-
resolve_typename
Signature:
def resolve_typename(cls, root: Self, info: Info) -> str:...Parameters:
-
root:- Type
-
Self
-
info:- Type
-
Info
-
-
resolve_nodes
Resolve a list of nodes.
This method should be defined by anyone implementing the
Nodeinterface.The nodes should be returned in the same order as the provided ids. Also, if
requiredisTrue, all ids must be resolved or an error should be raised. IfrequiredisFalse, missing nodes should be returned asNone.Returns:
An iterable of resolved nodes.
Signature:
def resolve_nodes(cls, info: Info, node_ids: Iterable[str], required: bool = False) -> :...Parameters:
-
info:The strawberry execution info resolve the type name from.
- Type
-
Info
-
node_ids:List of node ids that should be returned.
- Type
-
Iterable[str]
-
required:If
True, allnode_idsrequested must exist. If they don’t, an error must be raised. IfFalse, missing nodes should be returned asNone. It only makes sense when passing a list ofnode_ids, otherwise it will should ignored.- Type
-
bool - Default
-
False
-
-
resolve_node
Resolve a node given its id.
This method is a convenience method that calls
resolve_nodesfor a single node id.Returns:
The resolved node or None if it was not found
Signature:
def resolve_node(cls, node_id: str, info: Info, required: bool = False) -> AwaitableOrValue[Self | None]:...Parameters:
-
node_id:The id of the node to be retrieved.
- Type
-
str
-
info:The strawberry execution info resolve the type name from.
- Type
-
Info
-
required:if the node is required or not to exist. If not, then None should be returned if it doesn’t exist. Otherwise an exception should be raised.
- Type
-
bool - Default
-
False
-