Skip to main content

Base GraphQL/GraphiQL controller

Project description

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

This modules enables the creation of GraphQL endpoints. In itself, it does nothing and must be used by a developer to create the GraphQL schema and resolvers using graphene, and expose them through a controller. An example is available in the graphql_demo module.

Table of contents

Usage

To use this module, you need to

  • create your graphene schema

  • create your controller to expose your GraphQL endpoint, and optionally a GraphiQL IDE.

This module does not attempt to expose the whole Odoo object model. This could be the purpose of another module based on this one. We believe however that it is preferable to expose a specific well tested endpoint for each customer, so as to reduce coupling by knowing precisely what is exposed and needs to be tested when upgrading Odoo.

To start working with this module, we recommend the following approach:

  • Learn GraphQL basics

  • Learn graphene, the python library used to create GraphQL schemas and resolvers.

  • Examine the graphql_demo module in this repo, copy it, adapt the controller to suit your needs (routes, authentication methods).

  • Start building your own schema and resolver.

Building your schema

The schema can be built using native graphene types. An odoo.addons.graphql_base.types.OdooObjectType is provided as a convenience. It is a graphene ObjectType with a default attribute resolver which:

  • converts False to None (except for Boolean types), to avoid Odoo’s weird False strings being rendered as json "false";

  • adds the user timezone to Datetime fields;

  • raises an error if an attribute is absent to avoid field name typing errors.

Creating GraphQL controllers

The module provides an odoo.addons.graphql_base.GraphQLControllerMixin class to help you build GraphQL controllers providing GraphiQL and/or GraphQL endpoints.

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)

    # Optional monkey patch, needed to accept application/json GraphQL
    # requests. If you only need to accept GET requests or POST
    # with application/x-www-form-urlencoded content,
    # this is not necessary.
    GraphQLControllerMixin.patch_for_json("^/graphql/demo/?$")

    # 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)

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 smashing 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

odoo12_addon_graphql_base-12.0.2.0.0-py3-none-any.whl (29.3 kB view details)

Uploaded Python 3

File details

Details for the file odoo12_addon_graphql_base-12.0.2.0.0-py3-none-any.whl.

File metadata

  • Download URL: odoo12_addon_graphql_base-12.0.2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 29.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.3

File hashes

Hashes for odoo12_addon_graphql_base-12.0.2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8de25147f4269ea7c04eabc1c7b10b6d696850f23a4a9bb8d06adb818e481c5
MD5 6fd3335877513746b71eaf3a969409b0
BLAKE2b-256 e20edcd388ca73b302944b8592c8c0f5e2f185e6b54c57813e84ad29f19a694a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page