skillbox/server/books/schema/connections.py

46 lines
1.2 KiB
Python

import graphene
from graphene import relay
from books.schema.nodes import ModuleNode, TopicNode, ChapterNode, SnapshotChapterNode
class NodeConnection(relay.Connection):
"""
Custom connection type, so we don't need to deal with the edges and nodes if we don't want to, while still
adhering to the relay specification
Idea from: https://javascript.plainenglish.io/graphql-pagination-using-edges-vs-nodes-in-connections-f2ddb8edffa0
Always has property called `nodes` which is filled with the Meta node class.
Example:
class TopicConnection(NodeConnection):
class Meta:
node = TopicNode
# will have
# nodes = graphene.List(TopicNode)
"""
class Meta:
abstract = True
def resolve_nodes(self, *args, **kwargs):
return [edge.node for edge in self.edges]
@classmethod
def __init_subclass_with_meta__(cls, node=None, name=None, **options):
cls.nodes = graphene.List(node)
return super().__init_subclass_with_meta__(
node, name, **options
)
class TopicConnection(NodeConnection):
class Meta:
node = TopicNode
class ModuleConnection(NodeConnection):
class Meta:
node = ModuleNode