graphql-engine/docs/graphql/core/actions/codegen/index.rst

131 lines
3.4 KiB
ReStructuredText
Raw Normal View History

.. meta::
:description: Codegen for Hasura actions
:keywords: hasura, docs, actions, codegen
.. _actions_codegen:
2020-02-24 19:19:14 +03:00
Actions codegen
===============
.. contents:: Table of contents
:backlinks: none
:depth: 2
2020-02-24 19:19:14 +03:00
:local:
Introduction
------------
Actions need HTTP handlers to run the business logic. It might be inconvenient
to write the complete handler code for every action. Luckily, GraphQL's type
system allows us to auto-generate the boilerplate code for actions.
.. note::
Hasura currently has codegen set up for a few frameworks. The list of
supported frameworks should grow with contributions from the
community.
2020-02-25 18:30:00 +03:00
.. _actions-codegen-execute:
2020-02-24 19:19:14 +03:00
Generating handler code for your action
---------------------------------------
.. rst-class:: api_tabs
.. tabs::
.. tab:: Console
Head to the ``Actions -> [action-name] -> Codegen`` tab in the console
You can select the framework of your choice to get the corresponding
handler boilerplate code.
.. thumbnail:: /img/graphql/core/actions/console-codegen-tab.png
2020-02-24 19:19:14 +03:00
:alt: Console codegen tab
.. tab:: CLI
**Configuration**
Before being able to codegen for actions, you have to configure your CLI.
Run:
.. code-block:: bash
hasura actions use-codegen
1. Choose which framework you want to codegen for:
.. thumbnail:: /img/graphql/core/actions/cli-framework-prompt.png
2020-02-24 19:19:14 +03:00
:alt: CLI Framework Prompt
2. Choose if you also wish to clone a starter kit for the chosen framework:
.. thumbnail:: /img/graphql/core/actions/cli-starter-kit-prompt.png
2020-02-24 19:19:14 +03:00
:alt: CLI Starter Kit Prompt
3. Choose a path where you want to output the auto-generated code files
.. thumbnail:: /img/graphql/core/actions/cli-output-dir-prompt.png
2020-02-24 19:19:14 +03:00
:alt: CLI Starter Kit Prompt
This command will update your ``config.yaml`` with the codegen config as per
your preferences. You can also set these values manually in ``config.yaml``.
For example:
.. code-block:: yaml
:emphasize-lines: 8-10
version: "2"
endpoint: http://localhost:8080
metadata_directory: metadata
migrations_directory: migrations
actions:
handler_webhook_baseurl: http://localhost:3000
kind: synchronous
codegen:
framework: nodejs-express
output_dir: ./nodejs-express/src/handlers/
**Codegen**
To finally get auto-generated code for an action, run:
.. code-block:: bash
hasura actions codegen <action-name>
The codegen files will be generated at the ``output_dir`` path from ``config.yaml``.
Codegen for your framework
--------------------------
2020-02-24 19:19:14 +03:00
As of now, Hasura provides codegen for a few frameworks (``nodejs-express``,
``typescript-zeit``, ``python-flask`` and many more). You can see the full list in the ``Codegen`` tab on the console after you've :ref:`created an action <create_actions>`.
We will continue adding more examples to the documentation.
.. toctree::
:maxdepth: 1
Python & Flask <python-flask>
Building your own codegen
^^^^^^^^^^^^^^^^^^^^^^^^^
2020-02-24 19:19:14 +03:00
If you wish to build a code generator for your framework
2020-07-09 11:42:33 +03:00
`read the contrib guide <https://github.com/hasura/codegen-assets/blob/master/builder-kit/README.md>`__.
2020-02-24 19:19:14 +03:00
.. admonition:: Additional Resources
2020-02-24 19:19:14 +03:00
Introduction to Hasura Actions - `View Recording <https://hasura.io/events/webinar/hasura-actions/?pg=docs&plcmt=body&cta=view-recording&tech=>`__.
2020-02-24 19:19:14 +03:00