Skip to content

Instantly share code, notes, and snippets.

@cuongld2
Created April 13, 2020 10:49
Show Gist options
  • Save cuongld2/338e0805b348cfe0b00d4ed8ba3df261 to your computer and use it in GitHub Desktop.
Save cuongld2/338e0805b348cfe0b00d4ed8ba3df261 to your computer and use it in GitHub Desktop.
CreateUser class with graphene
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