evg-app-back/users/schema.py

69 lines
1.8 KiB
Python
Raw Normal View History

2021-01-04 23:25:59 +03:00
import graphene
2021-01-10 15:51:59 +03:00
import graphql_jwt
import graphene_django
from django.contrib.auth.models import User
from .models import Profile
2021-01-20 22:10:57 +03:00
from graphene import relay
from graphene_django.filter import DjangoFilterConnectionField
2021-01-10 15:51:59 +03:00
class UserType(graphene_django.DjangoObjectType):
class Meta:
model = User
2021-01-20 22:10:57 +03:00
pk = graphene.Int()
def resolve_pk(self, info):
return self.pk
2021-01-10 15:51:59 +03:00
class ProfileType(graphene_django.DjangoObjectType):
class Meta:
model = Profile
2021-01-20 22:10:57 +03:00
interfaces = (relay.Node,)
filter_fields = {
"user__username" : ("exact","contains")
}
pk = graphene.Int()
def resolve_pk(self, info):
return self.pk
2021-01-10 15:51:59 +03:00
class RegisterUserInput(graphene.InputObjectType):
username = graphene.String()
password = graphene.String()
class RegisterUser(graphene.Mutation):
class Arguments:
input = RegisterUserInput(required=True)
ok = graphene.Boolean()
2021-01-10 15:51:59 +03:00
def mutate(self, info, input=None):
try:
User.objects.create_user(username=input.username, password=input.password)
except:
return RegisterUser(ok=False)
return RegisterUser(ok=True)
2021-01-10 15:51:59 +03:00
class Mutation(graphene.ObjectType):
token_auth = graphql_jwt.ObtainJSONWebToken.Field()
verify_token = graphql_jwt.Verify.Field()
refresh_token = graphql_jwt.Refresh.Field()
register_user = RegisterUser.Field()
2021-01-04 23:25:59 +03:00
class Query(graphene.ObjectType):
2021-01-10 15:51:59 +03:00
hello = graphene.Field(graphene.String, token=graphene.String(required=True))
user_info = graphene.Field(UserType, token=graphene.String(required=True))
2021-01-20 22:10:57 +03:00
profile = relay.Node.Field(ProfileType)
all_profiles = DjangoFilterConnectionField(ProfileType)
2021-01-04 23:25:59 +03:00
2021-01-10 15:51:59 +03:00
def resolve_hello(self, info, **kwargs):
print(info.context.user.id)
2021-01-04 23:25:59 +03:00
return "hello"
2021-01-10 15:51:59 +03:00
def resolve_user_info(self, info, **kwargs):
return info.context.user
schema = graphene.Schema(query=Query, mutation=Mutation)