import graphene from graphene import relay from graphene_django import DjangoObjectType from api.graphene_wagtail import GenericStreamFieldType from api.utils import get_object from notes.models import InstrumentBookmark from notes.schema import InstrumentBookmarkNode from .models import BasicKnowledge, InstrumentType class InstrumentTypeNode(DjangoObjectType): type = graphene.String(required=True) class Meta: model = InstrumentType only_fields = [ 'name', 'category', 'type', 'id' ] @staticmethod def resolve_type(root: InstrumentType, info, **kwargs): return root.type class InstrumentNode(DjangoObjectType): bookmarks = graphene.List(InstrumentBookmarkNode) type = graphene.Field(InstrumentTypeNode) contents = GenericStreamFieldType() class Meta: model = BasicKnowledge filter_fields = ['slug'] interfaces = (relay.Node,) only_fields = [ 'slug', 'title', 'intro', 'contents', ] @staticmethod def resolve_type(root: BasicKnowledge, info, **kwargs): return root.new_type def resolve_bookmarks(self, info, **kwargs): return InstrumentBookmark.objects.filter( user=info.context.user, instrument=self ) class InstrumentQuery(object): instrument = graphene.Field(InstrumentNode, slug=graphene.String(), id=graphene.ID()) instruments = graphene.List(InstrumentNode) instrument_types = graphene.List(InstrumentTypeNode) def resolve_instrument(self, info, **kwargs): slug = kwargs.get('slug') instrument_id = kwargs.get('id') if instrument_id is not None: return get_object(BasicKnowledge, instrument_id) if slug is not None: return BasicKnowledge.objects.get(slug=slug) return None def resolve_instruments(self, info, **kwargs): return BasicKnowledge.objects.all().order_by('title').live() def resolve_instrument_types(self, info, **kwargs): return InstrumentType.objects.filter(instruments__isnull=False).order_by('name').distinct()