-
Notifications
You must be signed in to change notification settings - Fork 36
/
schema.py
67 lines (55 loc) · 1.93 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
import graphene
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyConnectionField, SQLAlchemyObjectType
from database import db_session,User as UserModel
from sqlalchemy import and_
class Users(SQLAlchemyObjectType):
class Meta:
model = UserModel
interfaces = (relay.Node, )
# Used to Create New User
class createUser(graphene.Mutation):
class Input:
name = graphene.String()
email = graphene.String()
username = graphene.String()
ok = graphene.Boolean()
user = graphene.Field(Users)
@classmethod
def mutate(cls, _, args, context, info):
user = UserModel(name=args.get('name'), email=args.get('email'), username=args.get('username'))
db_session.add(user)
db_session.commit()
ok = True
return createUser(user=user, ok=ok)
# Used to Change Username with Email
class changeUsername(graphene.Mutation):
class Input:
username = graphene.String()
email = graphene.String()
ok = graphene.Boolean()
user = graphene.Field(Users)
@classmethod
def mutate(cls, _, args, context, info):
query = Users.get_query(context)
email = args.get('email')
username = args.get('username')
user = query.filter(UserModel.email == email).first()
user.username = username
db_session.commit()
ok = True
return changeUsername(user=user, ok = ok)
class Query(graphene.ObjectType):
node = relay.Node.Field()
user = SQLAlchemyConnectionField(Users)
find_user = graphene.Field(lambda: Users, username = graphene.String())
all_users = SQLAlchemyConnectionField(Users)
def resolve_find_user(self,args,context,info):
query = Users.get_query(context)
username = args.get('username')
# you can also use and_ with filter() eg: filter(and_(param1, param2)).first()
return query.filter(UserModel.username == username).first()
class MyMutations(graphene.ObjectType):
create_user = createUser.Field()
change_username = changeUsername.Field()
schema = graphene.Schema(query=Query, mutation=MyMutations, types=[Users])