Skip to content

Commit

Permalink
[FIX] graphql: controller must rollback in case of error
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Feb 6, 2019
1 parent 6846e20 commit 4cc7835
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
7 changes: 7 additions & 0 deletions graphql_base/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,20 @@ def _process_request(self, schema, data):
headers["Content-Type"] = "application/json"
response = http.request.make_response(result, headers=headers)
response.status_code = status_code
if any(er.errors for er in execution_results):
env = http.request.env
env.cr.rollback()
env.clear()
return response
except HttpQueryError as e:
result = json_encode({"errors": [default_format_error(e)]})
headers = dict(e.headers)
headers["Content-Type"] = "application/json"
response = http.request.make_response(result, headers=headers)
response.status_code = e.status_code
env = http.request.env
env.cr.rollback()
env.clear()
return response

def _handle_graphql_request(self, schema):
Expand Down
10 changes: 8 additions & 2 deletions graphql_demo/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ class Arguments:
name = graphene.String(required=True)
email = graphene.String(required=True)
is_company = graphene.Boolean()
raise_after_create = graphene.Boolean()

Output = Partner

def mutate(self, info, name, email, is_company=False):
def mutate(
self, info, name, email, is_company=False, raise_after_create=False
):
env = info.context["env"]
return env["res.partner"].create(
partner = env["res.partner"].create(
{"name": name, "email": email, "is_company": is_company}
)
if raise_after_create:
raise UserError("as requested")
return partner


class Mutation(graphene.ObjectType):
Expand Down
38 changes: 38 additions & 0 deletions graphql_demo/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from odoo.tests import HttpCase
from odoo.tests.common import HOST, PORT
from odoo.tools import mute_logger


class TestController(HttpCase):
Expand Down Expand Up @@ -114,6 +115,14 @@ def test_post_form_mutation(self):
self.assertEqual(
"Le Héro, Toto", r.json()["data"]["createPartner"]["name"]
)
self.assertEqual(
len(
self.env["res.partner"].search(
[("email", "=", "[email protected]")]
)
),
1,
)

def test_get_mutation_not_allowed(self):
"""
Expand All @@ -137,3 +146,32 @@ def test_get_mutation_not_allowed(self):
"Can only perform a mutation operation from a POST request.",
r.json()["errors"][0]["message"],
)

@mute_logger("graphql.execution.executor", "graphql.execution.utils")
def test_post_form_mutation_rollback(self):
self.authenticate("admin", "admin")
query = """
mutation {
createPartner(
name: "Le Héro, Toto",
email: "[email protected]",
raiseAfterCreate: true
) {
name
}
}
"""
data = {"query": query}
r = self.url_open("/graphql/demo", data=data)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.headers["Content-Type"], "application/json")
self.assertIn("as requested", r.json()["errors"][0]["message"])
# a rollback must have occured
self.assertEqual(
len(
self.env["res.partner"].search(
[("email", "=", "[email protected]")]
)
),
0,
)

0 comments on commit 4cc7835

Please sign in to comment.