-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathschema.py
122 lines (95 loc) · 4.42 KB
/
schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import graphene
from graphql import GraphQLError
from graphene_django.types import DjangoObjectType, ObjectType
from .models import Question, UserResult
# its like serializers in django rest framework
class QuestionType(DjangoObjectType):
class Meta:
model = Question
exclude = ('status',)
class UserResultType(DjangoObjectType):
class Meta:
model = UserResult
exclude = ('id',)
# a subclass of ObjectType representing the GraphQL queries related to questions
class QuestionQuery(ObjectType):
questions = graphene.List(QuestionType)
question = graphene.Field(QuestionType, id=graphene.ID(required=True))
@staticmethod
def resolve_questions(parent, info, **kwargs):
return Question.objects.filter(status=True).defer('status')
@staticmethod
def resolve_question(parent, info, **kwargs):
pk = kwargs.get('id')
return Question.objects.get(id=pk)
class UserResultQuery(ObjectType):
user_result = graphene.Field(UserResultType, name=graphene.String(required=True))
@staticmethod
def resolve_user_result(parent, info, **kwargs):
name = kwargs.get('name')
return UserResult.objects.get(fullname=name)
class QuestionInputs(graphene.InputObjectType):
question = graphene.String()
option1 = graphene.String()
option2 = graphene.String()
option3 = graphene.String()
option4 = graphene.String()
answer = graphene.String()
class CreateQuestion(graphene.Mutation):
class Arguments:
inputs = QuestionInputs(required=True)
is_created = graphene.Boolean(default_value=False)
@classmethod
def mutate(cls, parent, info, inputs):
if not inputs.answer in ['option1', 'option2', 'option3', 'option4']:
raise GraphQLError('choose one of the options as answer!')
elif not info.context.user.is_authenticated:
raise GraphQLError('you are not authenticated')
Question.objects.create(
question=inputs.question, option1=inputs.option1,
option2=inputs.option2, option3=inputs.option3,
option4=inputs.option4, answer=inputs.answer,
)
is_created = True
return CreateQuestion(is_created=is_created)
class UpdateQuestion(graphene.Mutation):
class Arguments:
id = graphene.ID(required=True)
inputs = QuestionInputs()
is_updated = graphene.Boolean(default_value=False)
@classmethod
def mutate(cls, parent, info, id, inputs=None):
if not info.context.user.is_authenticated:
raise GraphQLError('you are not authenticated!')
elif not inputs:
raise GraphQLError('there is no parameter to update!')
elif inputs.answer and inputs.answer not in ['option1', 'option2', 'option3', 'option4']:
raise GraphQLError('choose one of the options as answer!')
else:
question_instance = Question.objects.get(id=id)
question_instance.question = inputs.question if inputs.question is not None else question_instance.question
question_instance.option1 = inputs.option1 if inputs.option1 is not None else question_instance.option1
question_instance.option2 = inputs.option2 if inputs.option2 is not None else question_instance.option2
question_instance.option3 = inputs.option3 if inputs.option3 is not None else question_instance.option3
question_instance.option4 = inputs.option4 if inputs.option4 is not None else question_instance.option4
question_instance.answer = inputs.answer if inputs.answer is not None else question_instance.answer
question_instance.save()
is_updated = True
return UpdateQuestion(is_updated=is_updated)
class DeleteQuestion(graphene.Mutation):
class Arguments:
id = graphene.ID(required=True)
is_deleted = graphene.Boolean(default_value=False)
@classmethod
def mutate(cls, parent, info, id):
if not info.context.user.is_authenticated:
raise GraphQLError('you are not authenticated!')
question = Question.objects.get(id=id)
question.delete()
is_deleted = True
return DeleteQuestion(is_deleted=is_deleted)
# a subclass of graphene.ObjectType representing the GraphQL mutation operations
class QuestionMutate(graphene.ObjectType):
create_question = CreateQuestion.Field()
update_question = UpdateQuestion.Field()
delete_question = DeleteQuestion.Field()