|
| 1 | +import json |
| 2 | +import falcon |
| 3 | +from .schema import schema |
| 4 | + |
| 5 | + |
| 6 | +def set_graphql_allow_header( |
| 7 | + req: falcon.Request, |
| 8 | + resp: falcon.Response, |
| 9 | + resource: object, |
| 10 | +): |
| 11 | + resp.set_header('Allow', 'GET, POST, OPTIONS') |
| 12 | + |
| 13 | + |
| 14 | +class HelloWorldResource: |
| 15 | + |
| 16 | + def on_get(self, req, resp): |
| 17 | + name = "Hello World!" |
| 18 | + resp.status = falcon.HTTP_200 |
| 19 | + resp.body = json.dumps({"respone": name, "status": resp.status}) |
| 20 | + |
| 21 | + def on_post(self, req, resp): |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +@falcon.after(set_graphql_allow_header) |
| 26 | +class GraphQLResource: |
| 27 | + |
| 28 | + def on_get(self, req, resp): |
| 29 | + query = req.params['query'] |
| 30 | + result = schema.execute(query) |
| 31 | + |
| 32 | + if result.data: |
| 33 | + data_ret = {'data': result.data} |
| 34 | + resp.status = falcon.HTTP_200 |
| 35 | + resp.body = json.dumps(data_ret, separators=(',', ':')) |
| 36 | + |
| 37 | + def on_post(self, req, resp): |
| 38 | + query = req.params['query'] |
| 39 | + result = schema.execute(query) |
| 40 | + if result.data: |
| 41 | + data_ret = {'data': result.data} |
| 42 | + resp.status = falcon.HTTP_200 |
| 43 | + resp.body = json.dumps(data_ret, separators=(',', ':')) |
0 commit comments