-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.py
86 lines (66 loc) · 2.37 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# flask_sqlalchemy/schema.py
from email import message
import graphene
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from models.user import User as UserModel
from dao.user_dao import UserDAO
from db import db_session
from graphql import GraphQLError
class CustomNode(graphene.Node):
class Meta:
name = 'MyNode'
@staticmethod
def to_global_id(type, id):
return id
class UserSchema(SQLAlchemyObjectType):
class Meta:
model = UserModel
interfaces = (CustomNode, )
class UserInput(graphene.InputObjectType):
id = graphene.ID()
name = graphene.String()
address = graphene.String()
phone = graphene.String()
class Query(graphene.ObjectType):
node = relay.Node.Field()
all_users = SQLAlchemyConnectionField(UserSchema.connection)
single_user = graphene.Field(UserSchema, id=graphene.ID(required=True))
def resolve_single_user(root, info, id):
return UserDAO.get_user_by_id(id)
class AddUser(graphene.Mutation):
class Arguments:
name = graphene.String(required=True)
address = graphene.String(required=True)
phone = graphene.String(required=True)
ok = graphene.Boolean()
user = graphene.Field(lambda: UserSchema)
message = graphene.String()
def mutate(self, info, name, address, phone):
user, ok, message = UserDAO.add_user(name, address, phone)
return AddUser(user=user, ok=ok, message=message)
class DeleteUser(graphene.Mutation):
ok = graphene.Boolean()
message = graphene.String()
class Arguments:
id = graphene.ID()
@classmethod
def mutate(cls, root, info, id):
ok,message = UserDAO.delete_user(id)
return cls(ok=ok, message=message)
class UpdateUser(graphene.Mutation):
ok = graphene.Boolean()
class Arguments:
id = graphene.ID()
input = UserInput(required=True)
user = graphene.Field(lambda: UserSchema)
message = graphene.String()
@classmethod
def mutate(cls, root, info, id, input=None):
user, ok, message = UserDAO.update_user(id, input)
return cls(user=user, ok=ok, message=message)
class Mutation(graphene.ObjectType):
add_user = AddUser.Field()
delete_user = DeleteUser.Field()
update_user = UpdateUser.Field()
schema = graphene.Schema(query=Query, mutation= Mutation)