Add public graphql endpoint
This commit is contained in:
parent
9837248881
commit
e982579711
|
|
@ -0,0 +1,25 @@
|
||||||
|
import graphene
|
||||||
|
from django.conf import settings
|
||||||
|
from graphene import relay
|
||||||
|
from graphene_django.debug import DjangoDebug
|
||||||
|
|
||||||
|
|
||||||
|
from users.schema_public import UsersQuery
|
||||||
|
from users.mutations import ProfileMutations
|
||||||
|
|
||||||
|
|
||||||
|
class Query(UsersQuery, graphene.ObjectType):
|
||||||
|
node = relay.Node.Field()
|
||||||
|
|
||||||
|
if settings.DEBUG:
|
||||||
|
debug = graphene.Field(DjangoDebug, name='__debug')
|
||||||
|
|
||||||
|
|
||||||
|
# class Mutation(BookMutations, RoomMutations, AssignmentMutations, ObjectiveMutations, CoreMutations, PortfolioMutations,
|
||||||
|
# ProfileMutations, SurveysMutations, graphene.ObjectType):
|
||||||
|
|
||||||
|
if settings.DEBUG:
|
||||||
|
debug = graphene.Field(DjangoDebug, name='__debug')
|
||||||
|
|
||||||
|
|
||||||
|
schema = graphene.Schema(query=Query)
|
||||||
|
|
@ -19,12 +19,14 @@ from surveys.schema import SurveysQuery
|
||||||
from surveys.mutations import SurveysMutations
|
from surveys.mutations import SurveysMutations
|
||||||
from rooms.mutations import RoomMutations
|
from rooms.mutations import RoomMutations
|
||||||
from rooms.schema import RoomsQuery, ModuleRoomsQuery
|
from rooms.schema import RoomsQuery, ModuleRoomsQuery
|
||||||
from users.schema import UsersQuery
|
from users.schema_public import UsersQuery
|
||||||
|
from users.schema import AllUsersQuery
|
||||||
from users.mutations import ProfileMutations
|
from users.mutations import ProfileMutations
|
||||||
|
|
||||||
|
|
||||||
class Query(UsersQuery, ModuleRoomsQuery, RoomsQuery, ObjectivesQuery, BookQuery, AssignmentsQuery, StudentSubmissionQuery,
|
class Query(UsersQuery, AllUsersQuery, ModuleRoomsQuery, RoomsQuery, ObjectivesQuery, BookQuery, AssignmentsQuery,
|
||||||
BasicKnowledgeQuery, PortfolioQuery, MyActivityQuery, SurveysQuery, graphene.ObjectType):
|
StudentSubmissionQuery, BasicKnowledgeQuery, PortfolioQuery, MyActivityQuery, SurveysQuery,
|
||||||
|
graphene.ObjectType):
|
||||||
node = relay.Node.Field()
|
node = relay.Node.Field()
|
||||||
|
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,21 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from graphene_django.views import GraphQLView
|
||||||
|
|
||||||
|
from api.public_schema import schema
|
||||||
|
|
||||||
from core.views import PrivateGraphQLView
|
from core.views import PrivateGraphQLView
|
||||||
|
|
||||||
app_name = 'api'
|
app_name = 'api'
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
url(r'^graphql-public', csrf_exempt(GraphQLView.as_view(schema=schema))),
|
||||||
url(r'^graphql', csrf_exempt(PrivateGraphQLView.as_view())),
|
url(r'^graphql', csrf_exempt(PrivateGraphQLView.as_view())),
|
||||||
]
|
]
|
||||||
|
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
|
urlpatterns += [url(r'^graphiql-public', csrf_exempt(GraphQLView.as_view(schema=schema, graphiql=True,
|
||||||
|
pretty=True)))]
|
||||||
urlpatterns += [url(r'^graphiql', csrf_exempt(PrivateGraphQLView.as_view(graphiql=True, pretty=True)))]
|
urlpatterns += [url(r'^graphiql', csrf_exempt(PrivateGraphQLView.as_view(graphiql=True, pretty=True)))]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,54 +1,18 @@
|
||||||
import graphene
|
import graphene
|
||||||
from graphene import relay
|
|
||||||
from graphene_django import DjangoObjectType
|
|
||||||
from graphene_django.filter import DjangoFilterConnectionField
|
from graphene_django.filter import DjangoFilterConnectionField
|
||||||
|
|
||||||
from users.models import SchoolClass, User
|
from users.models import User
|
||||||
|
from users.schema_public import UserNode
|
||||||
|
|
||||||
|
|
||||||
class SchoolClassNode(DjangoObjectType):
|
class AllUsersQuery(object):
|
||||||
pk = graphene.Int()
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = SchoolClass
|
|
||||||
filter_fields = ['name']
|
|
||||||
interfaces = (relay.Node,)
|
|
||||||
|
|
||||||
def resolve_pk(self, *args, **kwargs):
|
|
||||||
return self.id
|
|
||||||
|
|
||||||
|
|
||||||
class UserNode(DjangoObjectType):
|
|
||||||
pk = graphene.Int()
|
|
||||||
permissions = graphene.List(graphene.String)
|
|
||||||
selected_class = graphene.Field(SchoolClassNode)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = User
|
|
||||||
filter_fields = ['username', 'email']
|
|
||||||
only_fields = ['username', 'email', 'first_name', 'last_name', 'school_classes', 'last_module', 'avatar_url',
|
|
||||||
'selected_class']
|
|
||||||
interfaces = (relay.Node,)
|
|
||||||
|
|
||||||
def resolve_pk(self, info, **kwargs):
|
|
||||||
return self.id
|
|
||||||
|
|
||||||
def resolve_permissions(self, info):
|
|
||||||
return self.get_all_permissions()
|
|
||||||
|
|
||||||
def resolve_selected_class(self, info):
|
|
||||||
return self.selected_class()
|
|
||||||
|
|
||||||
|
|
||||||
class UsersQuery(object):
|
|
||||||
me = graphene.Field(UserNode)
|
me = graphene.Field(UserNode)
|
||||||
all_users = DjangoFilterConnectionField(UserNode)
|
all_users = DjangoFilterConnectionField(UserNode)
|
||||||
|
|
||||||
def resolve_me(self, info, **kwargs):
|
|
||||||
return info.context.user
|
|
||||||
|
|
||||||
def resolve_all_users(self, info, **kwargs):
|
def resolve_all_users(self, info, **kwargs):
|
||||||
if not info.context.user.is_superuser:
|
if not info.context.user.is_superuser:
|
||||||
return User.objects.none()
|
return User.objects.none()
|
||||||
else:
|
else:
|
||||||
return User.objects.all()
|
return User.objects.all()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# ITerativ GmbH
|
||||||
|
# http://www.iterativ.ch/
|
||||||
|
#
|
||||||
|
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
|
||||||
|
#
|
||||||
|
# Created on 2019-10-01
|
||||||
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
||||||
|
import graphene
|
||||||
|
from graphene import relay
|
||||||
|
from graphene_django import DjangoObjectType
|
||||||
|
from graphene_django.filter import DjangoFilterConnectionField
|
||||||
|
|
||||||
|
from users.models import User, SchoolClass
|
||||||
|
|
||||||
|
|
||||||
|
class SchoolClassNode(DjangoObjectType):
|
||||||
|
pk = graphene.Int()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = SchoolClass
|
||||||
|
filter_fields = ['name']
|
||||||
|
interfaces = (relay.Node,)
|
||||||
|
|
||||||
|
def resolve_pk(self, *args, **kwargs):
|
||||||
|
return self.id
|
||||||
|
|
||||||
|
|
||||||
|
class UserNode(DjangoObjectType):
|
||||||
|
pk = graphene.Int()
|
||||||
|
permissions = graphene.List(graphene.String)
|
||||||
|
selected_class = graphene.Field(SchoolClassNode)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
filter_fields = ['username', 'email']
|
||||||
|
only_fields = ['username', 'email', 'first_name', 'last_name', 'school_classes', 'last_module', 'avatar_url',
|
||||||
|
'selected_class']
|
||||||
|
interfaces = (relay.Node,)
|
||||||
|
|
||||||
|
def resolve_pk(self, info, **kwargs):
|
||||||
|
return self.id
|
||||||
|
|
||||||
|
def resolve_permissions(self, info):
|
||||||
|
return self.get_all_permissions()
|
||||||
|
|
||||||
|
def resolve_selected_class(self, info):
|
||||||
|
return self.selected_class()
|
||||||
|
|
||||||
|
|
||||||
|
class UsersQuery(object):
|
||||||
|
me = graphene.Field(UserNode)
|
||||||
|
all_users = DjangoFilterConnectionField(UserNode)
|
||||||
|
|
||||||
|
def resolve_me(self, info, **kwargs):
|
||||||
|
return info.context.user
|
||||||
|
|
||||||
|
def resolve_all_users(self, info, **kwargs):
|
||||||
|
if not info.context.user.is_superuser:
|
||||||
|
return User.objects.none()
|
||||||
|
else:
|
||||||
|
return User.objects.all()
|
||||||
Loading…
Reference in New Issue