Skip to content

Commit 082f846

Browse files
committed
Create User
1 parent 1de772e commit 082f846

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

hackernews/hackernews/schema.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import graphene
22

33
import links.schema
4+
import users.schema
45

56

67
class Query(links.schema.Query, graphene.ObjectType):
78
pass
89

9-
class Mutation(links.schema.Mutation, graphene.ObjectType):
10+
class Mutation(users.schema.Mutation, links.schema.Mutation, graphene.ObjectType):
1011
pass
1112

1213
schema = graphene.Schema(query=Query, mutation=Mutation)

hackernews/users/schema.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from django.contrib.auth import get_user_model
2+
3+
import graphene
4+
from graphene_django import DjangoObjectType
5+
6+
7+
class UserType(DjangoObjectType):
8+
class Meta:
9+
model = get_user_model()
10+
11+
12+
class CreateUser(graphene.Mutation):
13+
user = graphene.Field(UserType)
14+
15+
class Arguments:
16+
username = graphene.String(required=True)
17+
password = graphene.String(required=True)
18+
email = graphene.String(required=True)
19+
20+
def mutate(self, info, username, password, email):
21+
user = get_user_model()(
22+
username=username,
23+
email=email,
24+
)
25+
user.set_password(password)
26+
user.save()
27+
28+
return CreateUser(user=user)
29+
30+
31+
class Mutation(graphene.ObjectType):
32+
create_user = CreateUser.Field()

0 commit comments

Comments
 (0)