Created
April 13, 2020 10:49
-
-
Save cuongld2/338e0805b348cfe0b00d4ed8ba3df261 to your computer and use it in GitHub Desktop.
CreateUser class with graphene
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateUser(graphene.Mutation): | |
class Arguments: | |
username = graphene.String(required=True) | |
password = graphene.String(required=True) | |
fullname = graphene.String() | |
ok = graphene.Boolean() | |
user = graphene.Field(lambda: UserInfoSchema) | |
@staticmethod | |
def mutate(root, info, username, password, fullname, ): | |
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) | |
user = UserInfoSchema(username=username, password=hashed_password, fullname=fullname) | |
ok = True | |
db_user = crud.get_user_by_username(db, username=username) | |
if db_user: | |
raise GraphQLError("Username already registered") | |
user_info = UserCreate(username=username, password=password, fullname=fullname) | |
crud.create_user(db, user_info) | |
return CreateUser(user=user, ok=ok) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment