Initial graphql endpoint. Only the program for now.

This commit is contained in:
Víðir Valberg Guðmundsson 2019-03-11 20:26:36 +01:00
parent 0b6cc60bb8
commit d972ef2cef
5 changed files with 156 additions and 3 deletions

48
src/bornhack/schema.py Normal file
View file

@ -0,0 +1,48 @@
from graphene import relay, Schema, ObjectType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from camps.models import Camp
from program.schema import ProgramQuery
class CampNode(DjangoObjectType):
class Meta:
model = Camp
interfaces = (relay.Node, )
filter_fields = {
'title': ['icontains', 'iexact'],
}
only_fields = (
'title',
'slug',
'tagline',
'shortslug',
'buildup',
'camp',
'teardown',
'colour',
)
def resolve_buildup(self, info):
return [self.buildup.lower, self.buildup.upper]
def resolve_camp(self, info):
return [self.camp.lower, self.camp.upper]
def resolve_teardown(self, info):
return [self.teardown.lower, self.teardown.upper]
class CampQuery(object):
camp = relay.Node.Field(CampNode)
all_camps = DjangoFilterConnectionField(CampNode)
class Query(CampQuery, ProgramQuery, ObjectType):
pass
schema = Schema(query=Query)

View file

@ -43,6 +43,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
'django.contrib.sites',
'graphene_django',
'channels',
'profiles',
@ -190,4 +191,6 @@ LOGGING = {
},
}
GRAPHENE = {
'SCHEMA': 'bornhack.schema.schema'
}

View file

@ -1,9 +1,13 @@
from django.urls import include, path
from django.contrib import admin
from django.conf import settings
from allauth.account.views import (
LoginView,
LogoutView,
)
from django.urls import include, path
from django.contrib import admin
from graphene_django.views import GraphQLView
from camps.views import *
from feedback.views import FeedbackCreate
from info.views import *
@ -62,6 +66,7 @@ urlpatterns = [
),
path('accounts/', include('allauth.urls')),
path('admin/', admin.site.urls),
path('api/', GraphQLView.as_view(graphiql=True)),
path(
'camps/',

95
src/program/schema.py Normal file
View file

@ -0,0 +1,95 @@
from graphene import relay
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import (
Event,
EventType,
EventLocation,
EventTrack,
EventInstance,
Speaker,
)
class EventTypeNode(DjangoObjectType):
class Meta:
model = EventType
interfaces = (relay.Node, )
filter_fields = {
'name': ['iexact'],
}
class EventLocationNode(DjangoObjectType):
class Meta:
model = EventLocation
interfaces = (relay.Node, )
filter_fields = {
'name': ['iexact'],
}
class EventTrackNode(DjangoObjectType):
class Meta:
model = EventTrack
interfaces = (relay.Node, )
filter_fields = {
'name': ['iexact'],
}
class EventInstanceNode(DjangoObjectType):
class Meta:
model = EventInstance
interfaces = (relay.Node, )
filter_fields = {
'event__title': ['iexact'],
}
def resolve_when(self, info):
# We need to resolve this ourselves, graphene-django isn't smart enough
return [self.when.lower, self.when.upper]
class SpeakerNode(DjangoObjectType):
class Meta:
model = Speaker
interfaces = (relay.Node, )
only_fields = ('id', 'name', 'biography', 'slug', 'camp', 'events')
filter_fields = {
'name': ['iexact'],
}
class EventNode(DjangoObjectType):
class Meta:
model = Event
interfaces = (relay.Node, )
filter_fields = {
'title': ['iexact'],
'track__camp__slug': ['iexact'],
}
class ProgramQuery(object):
event_type = relay.Node.Field(EventTypeNode)
all_event_types = DjangoFilterConnectionField(EventTypeNode)
event_location = relay.Node.Field(EventLocationNode)
all_event_locations = DjangoFilterConnectionField(EventLocationNode)
event_track = relay.Node.Field(EventTrackNode)
all_event_tracks = DjangoFilterConnectionField(EventTrackNode)
event_instance = relay.Node.Field(EventInstanceNode)
all_event_instances = DjangoFilterConnectionField(EventInstanceNode)
event = relay.Node.Field(EventNode)
all_events = DjangoFilterConnectionField(EventNode)
speaker = relay.Node.Field(SpeakerNode)
all_speakers = DjangoFilterConnectionField(SpeakerNode)

View file

@ -36,4 +36,6 @@ sqlparse==0.2.4
venusian==1.2.0
webencodings==0.5.1
wrapt==1.11.1
graphene-django==2.2.0
django-filter==2.1.0