-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphql_app.py
More file actions
79 lines (67 loc) · 1.82 KB
/
Graphql_app.py
File metadata and controls
79 lines (67 loc) · 1.82 KB
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
from ariadne import QueryType
from ariadne import MutationType
from ariadne import gql
from ariadne import make_executable_schema
from ariadne.asgi import GraphQL
from mongoengine import connect
from bson.objectid import ObjectId
type_defs = gql("""
type Event {
name: String
time: String
state: String
attendee: String
links: String
category: String
date: String
groupname: String
}
input EventInput {
name: String!
time: String!
state: String!
attendee: String!
links: String!
category: String!
date: String!
groupname: String!
}
type Query {
events(name: String, state: String, date: String): [Event!]
}
type Mutation {
add(event: EventInput!): Boolean!
delete(id: String!): Boolean!
}
""")
mongo_client = connect('events', host='127.0.0.1', port=27017)
query = QueryType()
mutation = MutationType()
db = mongo_client.get_database('events')
collection = db.get_collection('event')
@query.field("events")
def resolve_events(*args, **kwargs):
print(kwargs)
return collection.find(kwargs if len(kwargs) > 0 else {})
@mutation.field('add')
def resolve_add(*args, **kwargs):
event = kwargs['event']
try:
collection.insert(event)
return True
except Exception as ex:
print(ex)
return False
@mutation.field('delete')
def resolve_delete(*args, **kwargs):
id = kwargs['id']
try:
collection.remove({
'_id': ObjectId(id),
})
return True
except Exception as ex:
print(ex)
return False
schema = make_executable_schema(type_defs, [query, mutation])
app = GraphQL(schema, debug=True)