skillbox/server/books/schema/connections.py

70 lines
1.8 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
class SnapshotChapterEdge:
description_visible = graphene.Boolean()
title_visible = graphene.Boolean()
@staticmethod
def resolve_description_visible(parent, info, **kwargs):
return parent.node.description_visible
@staticmethod
def resolve_title_visible(parent, info, **kwargs):
return parent.node.title_visible
@staticmethod
def resolve_node(parent, info, **kwargs):
return parent.node.chapter
class ChapterSnapshotConnection(NodeConnection):
class Meta:
node = SnapshotChapterNode
Edge = SnapshotChapterEdge