|
| 1 | +# Copyright 2018 ACSONE SA/NV |
| 2 | +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). |
| 3 | + |
| 4 | +import re |
| 5 | +from functools import partial |
| 6 | + |
| 7 | +from graphql_server import ( |
| 8 | + default_format_error, |
| 9 | + encode_execution_results, |
| 10 | + json_encode, |
| 11 | + load_json_body, |
| 12 | + run_http_query, |
| 13 | +) |
| 14 | + |
| 15 | +from odoo import http |
| 16 | + |
| 17 | + |
| 18 | +class GraphQLControllerMixin(object): |
| 19 | + @staticmethod |
| 20 | + def patch_for_json(path_re): |
| 21 | + # this is to avoid Odoo, which assumes json always means json+rpc, |
| 22 | + # complaining about "function declared as capable of handling request |
| 23 | + # of type 'http' but called with a request of type 'json'" |
| 24 | + path_re = re.compile(path_re) |
| 25 | + orig_get_request = http.Root.get_request |
| 26 | + |
| 27 | + def get_request(self, httprequest): |
| 28 | + if path_re.match(httprequest.path): |
| 29 | + return http.HttpRequest(httprequest) |
| 30 | + return orig_get_request(self, httprequest) |
| 31 | + |
| 32 | + http.Root.get_request = get_request |
| 33 | + |
| 34 | + def _parse_body(self): |
| 35 | + req = http.request.httprequest |
| 36 | + # We use mimetype here since we don't need the other |
| 37 | + # information provided by content_type |
| 38 | + content_type = req.mimetype |
| 39 | + if content_type == "application/graphql": |
| 40 | + return {"query": req.data.decode("utf8")} |
| 41 | + elif content_type == "application/json": |
| 42 | + return load_json_body(req.data.decode("utf8")) |
| 43 | + elif content_type in ( |
| 44 | + "application/x-www-form-urlencoded", |
| 45 | + "multipart/form-data", |
| 46 | + ): |
| 47 | + return http.request.params |
| 48 | + return {} |
| 49 | + |
| 50 | + def _process_request(self, schema, data, catch): |
| 51 | + request = http.request.httprequest |
| 52 | + execution_results, all_params = run_http_query( |
| 53 | + schema, |
| 54 | + request.method.lower(), |
| 55 | + data, |
| 56 | + query_data=request.args, |
| 57 | + batch_enabled=False, |
| 58 | + catch=catch, |
| 59 | + context={"env": http.request.env}, |
| 60 | + ) |
| 61 | + result, status_code = encode_execution_results( |
| 62 | + execution_results, |
| 63 | + is_batch=isinstance(data, list), |
| 64 | + format_error=default_format_error, |
| 65 | + encode=partial(json_encode, pretty=False), |
| 66 | + ) |
| 67 | + # TODO what to do with status_code? |
| 68 | + return http.request.make_response( |
| 69 | + result, headers={"Content-Type": "application/json"} |
| 70 | + ) |
| 71 | + |
| 72 | + def _handle_graphql_request(self, schema): |
| 73 | + data = self._parse_body() |
| 74 | + return self._process_request(schema, data, catch=False) |
| 75 | + |
| 76 | + def _handle_graphiql_request(self, schema): |
| 77 | + req = http.request.httprequest |
| 78 | + if req.method == "GET" and req.accept_mimetypes.accept_html: |
| 79 | + return http.request.render("graphql_base.graphiql", {}) |
| 80 | + # this way of passing a GraphQL query over http is not spec compliant |
| 81 | + # (https://graphql.org/learn/serving-over-http/), but we use |
| 82 | + # this only for our GraphiQL UI, and it works with Odoo's way |
| 83 | + # of passing the csrf token |
| 84 | + return self._process_request(schema, http.request.params, catch=True) |
0 commit comments