diff --git a/graphql_demo/README.rst b/graphql_demo/README.rst new file mode 100644 index 000000000..154204814 --- /dev/null +++ b/graphql_demo/README.rst @@ -0,0 +1,94 @@ +============ +GraphQL Demo +============ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:c346b472bba4541d33184c35cf66ba333c2a01b1e0fe15aeaf76c9283ff22389 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github + :target: https://github.com/OCA/rest-framework/tree/18.0/graphql_demo + :alt: OCA/rest-framework +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-graphql_demo + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This is a demonstration module providing a sample GraphQL endpoint, as +well as tests for ``graphql_base``. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +This module can be used in different ways: + +- as an example: copy the code and hack your way; +- to test ``graphql_base`` (install it with ``--test-enable``); +- on runbot, login and change the url to ``/graphiql/demo``. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* ACSONE SA/NV + +Contributors +~~~~~~~~~~~~ + +* Ajay Javiya + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-sbidoul| image:: https://github.com/sbidoul.png?size=40px + :target: https://github.com/sbidoul + :alt: sbidoul + +Current `maintainer `__: + +|maintainer-sbidoul| + +This module is part of the `OCA/rest-framework `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/graphql_demo/__init__.py b/graphql_demo/__init__.py new file mode 100644 index 000000000..e046e49fb --- /dev/null +++ b/graphql_demo/__init__.py @@ -0,0 +1 @@ +from . import controllers diff --git a/graphql_demo/__manifest__.py b/graphql_demo/__manifest__.py new file mode 100644 index 000000000..b2ae2aecb --- /dev/null +++ b/graphql_demo/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2018 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +{ + "name": "GraphQL Demo", + "version": "18.0.1.0.0", + "license": "LGPL-3", + "author": "ACSONE SA/NV, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/rest-framework", + "depends": ["graphql_base"], + "development_status": "Beta", + "maintainers": ["sbidoul"], + "installable": True, +} diff --git a/graphql_demo/controllers/__init__.py b/graphql_demo/controllers/__init__.py new file mode 100644 index 000000000..12a7e529b --- /dev/null +++ b/graphql_demo/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/graphql_demo/controllers/main.py b/graphql_demo/controllers/main.py new file mode 100644 index 000000000..6ec163e94 --- /dev/null +++ b/graphql_demo/controllers/main.py @@ -0,0 +1,22 @@ +# Copyright 2018 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import http + +from odoo.addons.graphql_base import GraphQLControllerMixin + +from ..schema import schema + + +class GraphQLController(http.Controller, GraphQLControllerMixin): + # The GraphiQL route, providing an IDE for developers + @http.route("/graphiql/demo", auth="user") + def graphiql(self, **kwargs): + return self._handle_graphiql_request(schema.graphql_schema) + + # The graphql route, for applications. + # Note csrf=False: you may want to apply extra security + # (such as origin restrictions) to this route. + @http.route("/graphql/demo", auth="user", csrf=False) + def graphql(self, **kwargs): + return self._handle_graphql_request(schema.graphql_schema) diff --git a/graphql_demo/i18n/graphql_demo.pot b/graphql_demo/i18n/graphql_demo.pot new file mode 100644 index 000000000..a34493d50 --- /dev/null +++ b/graphql_demo/i18n/graphql_demo.pot @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * graphql_demo +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: graphql_demo +#. odoo-python +#: code:addons/graphql_demo/schema.py:0 +#, python-format +msgid "UserError example" +msgstr "" + +#. module: graphql_demo +#. odoo-python +#: code:addons/graphql_demo/schema.py:0 +#, python-format +msgid "as requested" +msgstr "" diff --git a/graphql_demo/i18n/it.po b/graphql_demo/i18n/it.po new file mode 100644 index 000000000..502874903 --- /dev/null +++ b/graphql_demo/i18n/it.po @@ -0,0 +1,31 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * graphql_demo +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-07-01 11:47+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: graphql_demo +#. odoo-python +#: code:addons/graphql_demo/schema.py:0 +#, python-format +msgid "UserError example" +msgstr "Esempio errore utente" + +#. module: graphql_demo +#. odoo-python +#: code:addons/graphql_demo/schema.py:0 +#, python-format +msgid "as requested" +msgstr "come richiesto" diff --git a/graphql_demo/pyproject.toml b/graphql_demo/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/graphql_demo/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/graphql_demo/readme/DESCRIPTION.md b/graphql_demo/readme/DESCRIPTION.md new file mode 100644 index 000000000..bc29cbcba --- /dev/null +++ b/graphql_demo/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This is a demonstration module providing a sample GraphQL endpoint, as +well as tests for `graphql_base`. diff --git a/graphql_demo/readme/USAGE.md b/graphql_demo/readme/USAGE.md new file mode 100644 index 000000000..713606b77 --- /dev/null +++ b/graphql_demo/readme/USAGE.md @@ -0,0 +1,5 @@ +This module can be used in different ways: + + - as an example: copy the code and hack your way; + - to test `graphql_base` (install it with `--test-enable`); + - on runbot, login and change the url to `/graphiql/demo`. diff --git a/graphql_demo/schema.py b/graphql_demo/schema.py new file mode 100644 index 000000000..76d8007fc --- /dev/null +++ b/graphql_demo/schema.py @@ -0,0 +1,101 @@ +# Copyright 2018 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +# disable undefined variable error, which erroneously triggers +# on forward declarations of classes in lambdas +# pylint: disable=E0602 + +import graphene + +from odoo import _ +from odoo.exceptions import UserError + +from odoo.addons.graphql_base import OdooObjectType + + +class Country(OdooObjectType): + code = graphene.String(required=True) + name = graphene.String(required=True) + + +class Partner(OdooObjectType): + name = graphene.String(required=True) + street = graphene.String() + street2 = graphene.String() + city = graphene.String() + zip = graphene.String() + country = graphene.Field(Country) + email = graphene.String() + phone = graphene.String() + is_company = graphene.Boolean(required=True) + contacts = graphene.List(graphene.NonNull(lambda: Partner), required=True) + + @staticmethod + def resolve_country(root, info): + return root.country_id or None + + @staticmethod + def resolve_contacts(root, info): + return root.child_ids + + +class Query(graphene.ObjectType): + all_partners = graphene.List( + graphene.NonNull(Partner), + required=True, + companies_only=graphene.Boolean(), + limit=graphene.Int(), + offset=graphene.Int(), + ) + + reverse = graphene.String( + required=True, + description="Reverse a string", + word=graphene.String(required=True), + ) + + error_example = graphene.String() + + @staticmethod + def resolve_all_partners(root, info, companies_only=False, limit=None, offset=None): + domain = [] + if companies_only: + domain.append(("is_company", "=", True)) + return info.context["env"]["res.partner"].search( + domain, limit=limit, offset=offset + ) + + @staticmethod + def resolve_reverse(root, info, word): + return word[::-1] + + @staticmethod + def resolve_error_example(root, info): + raise UserError(_("UserError example")) + + +class CreatePartner(graphene.Mutation): + class Arguments: + name = graphene.String(required=True) + email = graphene.String(required=True) + is_company = graphene.Boolean() + raise_after_create = graphene.Boolean() + + Output = Partner + + @staticmethod + def mutate(self, info, name, email, is_company=False, raise_after_create=False): + env = info.context["env"] + 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): + create_partner = CreatePartner.Field(description="Documentation of CreatePartner") + + +schema = graphene.Schema(query=Query, mutation=Mutation) diff --git a/graphql_demo/static/description/icon.png b/graphql_demo/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/graphql_demo/static/description/icon.png differ diff --git a/graphql_demo/static/description/index.html b/graphql_demo/static/description/index.html new file mode 100644 index 000000000..dae5cc5e9 --- /dev/null +++ b/graphql_demo/static/description/index.html @@ -0,0 +1,429 @@ + + + + + +GraphQL Demo + + + +
+

GraphQL Demo

+ + +

Beta License: LGPL-3 OCA/rest-framework Translate me on Weblate Try me on Runboat

+

This is a demonstration module providing a sample GraphQL endpoint, as +well as tests for graphql_base.

+

Table of contents

+ +
+

Usage

+

This module can be used in different ways:

+
    +
  • as an example: copy the code and hack your way;
  • +
  • to test graphql_base (install it with --test-enable);
  • +
  • on runbot, login and change the url to /graphiql/demo.
  • +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ACSONE SA/NV
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

sbidoul

+

This module is part of the OCA/rest-framework project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/graphql_demo/tests/__init__.py b/graphql_demo/tests/__init__.py new file mode 100644 index 000000000..09741ac29 --- /dev/null +++ b/graphql_demo/tests/__init__.py @@ -0,0 +1,2 @@ +from . import test_controller +from . import test_graphene diff --git a/graphql_demo/tests/test_controller.py b/graphql_demo/tests/test_controller.py new file mode 100644 index 000000000..1e3b9fb16 --- /dev/null +++ b/graphql_demo/tests/test_controller.py @@ -0,0 +1,158 @@ +# Copyright 2018 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +import json + +from werkzeug.urls import url_encode + +from odoo.tests import HttpCase +from odoo.tests.common import HOST +from odoo.tools import config, mute_logger + + +class TestController(HttpCase): + def url_open_json(self, url, json): + return self.opener.post( + "http://{}:{}{}".format(HOST, config["http_port"], url), json=json + ) + + def _check_all_partners(self, all_partners, companies_only=False): + domain = [] + if companies_only: + domain.append(("is_company", "=", True)) + expected_names = set(self.env["res.partner"].search(domain).mapped("name")) + actual_names = {r["name"] for r in all_partners} + self.assertEqual(actual_names, expected_names) + + def test_get(self): + self.authenticate("admin", "admin") + query = "{allPartners{name}}" + data = {"query": query} + r = self.url_open("/graphql/demo?" + url_encode(data)) + self.assertEqual(r.status_code, 200) + self.assertEqual(r.headers["Content-Type"], "application/json") + self._check_all_partners(r.json()["data"]["allPartners"]) + + def test_get_with_variables(self): + self.authenticate("admin", "admin") + query = """ + query myQuery($companiesOnly: Boolean) { + allPartners(companiesOnly: $companiesOnly) { + name + } + } + """ + variables = {"companiesOnly": True} + data = {"query": query, "variables": json.dumps(variables)} + r = self.url_open("/graphql/demo?" + url_encode(data)) + self.assertEqual(r.status_code, 200) + self.assertEqual(r.headers["Content-Type"], "application/json") + self._check_all_partners(r.json()["data"]["allPartners"], companies_only=True) + + def test_post_form(self): + self.authenticate("admin", "admin") + query = "{allPartners{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._check_all_partners(r.json()["data"]["allPartners"]) + + def test_post_form_with_variables(self): + self.authenticate("admin", "admin") + query = """ + query myQuery($companiesOnly: Boolean) { + allPartners(companiesOnly: $companiesOnly) { + name + } + } + """ + variables = {"companiesOnly": True} + data = {"query": query, "variables": json.dumps(variables)} + r = self.url_open("/graphql/demo", data=data) + self.assertEqual(r.status_code, 200) + self.assertEqual(r.headers["Content-Type"], "application/json") + self._check_all_partners(r.json()["data"]["allPartners"], companies_only=True) + + def test_post_json_with_variables(self): + self.authenticate("admin", "admin") + query = """ + query myQuery($companiesOnly: Boolean) { + allPartners(companiesOnly: $companiesOnly) { + name + } + } + """ + variables = {"companiesOnly": True} + data = {"query": query, "variables": variables} + r = self.url_open_json("/graphql/demo", data) + self.assertEqual(r.status_code, 200) + self.assertEqual(r.headers["Content-Type"], "application/json") + self._check_all_partners(r.json()["data"]["allPartners"], companies_only=True) + + def test_post_form_mutation(self): + self.authenticate("admin", "admin") + query = """ + mutation { + createPartner( + name: "Le Héro, Toto", email: "toto@example.com" + ) { + 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.assertEqual("Le Héro, Toto", r.json()["data"]["createPartner"]["name"]) + self.assertEqual( + len(self.env["res.partner"].search([("email", "=", "toto@example.com")])), 1 + ) + + def test_get_mutation_not_allowed(self): + """ + Cannot perform a mutation with a GET, must use POST. + """ + self.authenticate("admin", "admin") + query = """ + mutation { + createPartner( + name: "Le Héro, Toto", email: "toto@example.com" + ) { + name + } + } + """ + data = {"query": query} + r = self.url_open("/graphql/demo?" + url_encode(data)) + self.assertEqual(r.status_code, 405) + self.assertEqual(r.headers["Content-Type"], "application/json") + self.assertIn( + "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: "toto@example.com", + 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", "=", "toto@example.com")])), 0 + ) diff --git a/graphql_demo/tests/test_graphene.py b/graphql_demo/tests/test_graphene.py new file mode 100644 index 000000000..ab44d6db7 --- /dev/null +++ b/graphql_demo/tests/test_graphene.py @@ -0,0 +1,70 @@ +# Copyright 2018 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +import logging + +from graphene.test import Client + +from odoo.tests import TransactionCase + +from ..schema import schema + + +class TestGraphene(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.client = Client(schema) + # disable logging for the graphql executor because we are testing + # errors and OCA's test runner considers the two errors being logged + # as fatal + logging.getLogger("graphql.execution").setLevel(logging.CRITICAL) + + def execute(self, query): + res = self.client.execute(query, context={"env": self.env}) + if not res: + raise RuntimeError("GraphQL query returned no data") + if res.get("errors"): + raise RuntimeError( + "GraphQL query returned error: {}".format(repr(res["errors"])) + ) + return res.get("data") + + def test_query_all_partners(self): + expected_names = set(self.env["res.partner"].search([]).mapped("name")) + actual_names = { + r["name"] for r in self.execute(" {allPartners{ name } }")["allPartners"] + } + self.assertEqual(actual_names, expected_names) + + def test_query_all_partners_companies_only(self): + expected_names = set( + self.env["res.partner"].search([("is_company", "=", True)]).mapped("name") + ) + actual_names = { + r["name"] + for r in self.execute(" {allPartners(companiesOnly: true){ name } }")[ + "allPartners" + ] + } + self.assertEqual(actual_names, expected_names) + + def test_error(self): + r = self.client.execute("{errorExample}", context={"env": self.env}) + self.assertIn("UserError example", r["errors"][0]["message"]) + + def test_mutation(self): + mutation = """\ + mutation{ + createPartner( + name: "toto", + email: "toto@acsone.eu", + ) { + name + } + } + """ + self.client.execute(mutation, context={"env": self.env}) + self.assertEqual( + len(self.env["res.partner"].search([("name", "=", "toto")])), 1 + )