implement a navigable book->topic->module query

This commit is contained in:
Pawel Kowalski 2018-08-08 14:43:49 +02:00
parent e21ba0a232
commit 91c81b13c0
7 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,32 @@
query BookQuery {
books {
edges {
node {
id
title
topics(first:3) {
edges {
node {
id
title
slug
teaser
description
modules(first:3) {
edges {
node {
id
title
slug
teaser
heroImage
}
}
}
}
}
}
}
}
}
}

View File

@ -0,0 +1,31 @@
query BooksQuery {
books {
edges {
node {
id
slug
title
}
}
}
topics {
edges {
node {
id
}
}
}
modules {
edges {
node {
title
slug
heroImage
}
}
}
}

View File

@ -27,3 +27,6 @@ class Book(Page):
template = 'generic_page.html'
subpage_types = ['book.Topic']
def get_child_ids(self):
return self.get_children().values_list('id', flat=True)

View File

@ -52,3 +52,7 @@ class Module(Page):
template = 'generic_page.html'
parent_page_types = ['book.Topic']
@classmethod
def get_topic_modules(cls, topic):
return cls.objects.filter(id__in=topic.get_child_ids()).live()

View File

@ -38,3 +38,10 @@ class Topic(Page):
parent_page_types = ['book.Book']
subpage_types = ['book.Module']
def get_child_ids(self):
return self.get_children().values_list('id', flat=True)
@classmethod
def get_book_topics(cls, book):
return cls.objects.filter(id__in=book.get_child_ids()).live()

View File

@ -31,6 +31,7 @@ class ModuleNode(DjangoObjectType):
class TopicNode(DjangoObjectType):
pk = graphene.Int()
modules = DjangoFilterConnectionField(ModuleNode)
class Meta:
model = Topic
@ -46,9 +47,13 @@ class TopicNode(DjangoObjectType):
def resolve_pk(self, *args, **kwargs):
return self.id
def resolve_modules(self, *args, **kwargs):
return Module.get_topic_modules(self)
class BookNode(DjangoObjectType):
pk = graphene.Int()
topics = DjangoFilterConnectionField(TopicNode)
class Meta:
model = Book
@ -64,6 +69,9 @@ class BookNode(DjangoObjectType):
def resolve_pk(self, *args, **kwargs):
return self.id
def resolve_topics(self, *args, **kwargs):
return Topic.get_book_topics(self)
class ModulesQuery(object):
books = DjangoFilterConnectionField(BookNode)