2021-11-02 17:35:35 +03:00
|
|
|
.. meta::
|
2022-01-17 16:27:01 +03:00
|
|
|
:description: REST connectors for actions
|
2021-12-14 11:41:08 +03:00
|
|
|
:keywords: hasura, docs, action, transforms, rest connectors
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. _actions_rest_connectors:
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
REST Connectors for actions
|
|
|
|
===========================
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
.. contents:: Table of contents
|
|
|
|
:backlinks: none
|
|
|
|
:depth: 2
|
|
|
|
:local:
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
------------
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
REST Connectors for actions are used to integrate existing REST APIs as GraphQL APIs
|
|
|
|
without changing any upstream code.
|
|
|
|
|
|
|
|
REST Connectors work by changing the default HTTP request made by an action to a
|
|
|
|
different HTTP request by adding suitable transforms.
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
.. admonition:: Supported from
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
REST Connectors are supported in Hasura GraphQL Engine versions ``v2.1.0`` and above
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
Configuring REST Connectors
|
|
|
|
---------------------------
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
.. rst-class:: api_tabs
|
|
|
|
.. tabs::
|
|
|
|
|
|
|
|
.. tab:: Console
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
Go to the ``Actions`` tab on the console and create or modify an action. Scroll down to ``Configure REST Connectors`` section:
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. thumbnail:: /img/graphql/core/actions/configure-rest-connectors.png
|
|
|
|
:alt: Configure REST connectors for actions
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. tab:: CLI
|
|
|
|
|
|
|
|
Will be added soon
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
.. tab:: API
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
REST Connectors can be added to actions using the :ref:`create_action metadata API <metadata_create_action>` or
|
2021-11-02 17:35:35 +03:00
|
|
|
:ref:`update_action metadata API <metadata_update_action>` by adding a
|
|
|
|
:ref:`request_transform <RequestTransformation>` field to the args:
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: http
|
|
|
|
:emphasize-lines: 24-26
|
|
|
|
|
|
|
|
POST /v1/metadata HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
X-Hasura-Role: admin
|
|
|
|
|
|
|
|
{
|
|
|
|
"type":"create_action",
|
|
|
|
"args":{
|
|
|
|
"name":"create_user",
|
|
|
|
"definition":{
|
|
|
|
"kind":"synchronous",
|
|
|
|
"arguments":[
|
|
|
|
{
|
|
|
|
"name":"username",
|
|
|
|
"type":"String!"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name":"email",
|
|
|
|
"type":"String!"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"output_type":"User",
|
|
|
|
"handler":"https://action.my_app.com/create-user",
|
|
|
|
"timeout":60,
|
|
|
|
"request_transform": {
|
|
|
|
"body": "{{$body.input.name}}"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"comment": "Custom action to create user"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
Types of transforms
|
|
|
|
-------------------
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
REST Connectors allows you to add different types of "transforms" to the default HTTP request.
|
|
|
|
You can also use "context variables" in the transforms to achieve dynamic behaviour for each request.
|
2022-01-12 15:00:18 +03:00
|
|
|
|
|
|
|
Context Variables
|
|
|
|
*****************
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
The context variables available in transforms are:
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
.. list-table::
|
|
|
|
:header-rows: 1
|
|
|
|
|
|
|
|
* - Context variable
|
|
|
|
- Value
|
2022-01-17 16:27:01 +03:00
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
* - $body
|
|
|
|
- Original body of action request
|
2022-01-17 16:27:01 +03:00
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
* - $base_url
|
|
|
|
- Original configured URL
|
2022-01-17 16:27:01 +03:00
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
* - $session_variables
|
2022-01-17 16:27:01 +03:00
|
|
|
- Session variables
|
|
|
|
|
2022-01-12 15:00:18 +03:00
|
|
|
Sample Context
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
The console allows you to provide mock ``session variables`` and ``env variables`` to test your transforms.
|
2022-01-12 15:00:18 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
Configure an ``env var`` in the action webhook handler.
|
2022-01-12 15:00:18 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. thumbnail:: /img/graphql/core/actions/transformation-context-vars-0.png
|
|
|
|
:alt: Console action webhook handler
|
|
|
|
:width: 800px
|
2022-01-12 15:00:18 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
Add the ``env var`` value to the ``Sample Context`` under ``Sample Env Variables``.
|
2022-01-12 15:00:18 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. thumbnail:: /img/graphql/core/actions/transformation-context-vars-1.png
|
|
|
|
:alt: Console action context env
|
|
|
|
:width: 800px
|
2022-01-12 15:00:18 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
The value should be reflected in the ``{{$base_url}}`` in ``Change Request Options`` section:
|
2022-01-12 15:00:18 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. thumbnail:: /img/graphql/core/actions/transformation-context-vars-2.png
|
|
|
|
:alt: Console action req options transformation
|
|
|
|
:width: 800px
|
2022-01-12 15:00:18 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
``Session vars`` can also be added to the ``Sample context`` as shown above,
|
|
|
|
and used like so: ``{{$session_variables['x-hasura-user-id']}}``.
|
|
|
|
The above screen also shows an example of using the session vars from context.
|
2022-01-12 15:00:18 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. admonition:: Context variables validation error
|
2022-01-12 15:00:18 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
Actual environment variables are not resolved during testing transforms as it could expose sensitive information to the UI.
|
|
|
|
|
|
|
|
Note that if you don't provide mock ``env/session variables`` and test your transform, you would get a UI validation error.
|
|
|
|
Considering this section is only used for testing, ``Create Action`` button will still be usable.
|
|
|
|
When you click on ``Create Action``, any referenced envs are validated at the server without leaking any sensitive information to the UI.
|
2022-01-12 15:00:18 +03:00
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
Request body
|
|
|
|
************
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
Generate a request body by configuring a template to transform the default payload to a custom payload.
|
|
|
|
The ``body`` field takes a template in the `Kriti templating language <https://github.com/hasura/kriti-lang>`__ to evaluate the transform.
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
.. rst-class:: api_tabs
|
|
|
|
.. tabs::
|
|
|
|
|
|
|
|
.. tab:: Console
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
In the ``Configure REST Connectors`` section, click on ``Add Payload Transform``:
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. thumbnail:: /img/graphql/core/actions/payload-transform.png
|
2021-11-02 17:35:35 +03:00
|
|
|
:alt: Add payload transformation
|
2022-01-10 21:39:15 +03:00
|
|
|
:width: 1100px
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. tab:: CLI
|
|
|
|
|
|
|
|
Will be added soon
|
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
.. tab:: API
|
|
|
|
|
|
|
|
.. code-block:: json
|
2022-01-12 15:00:18 +03:00
|
|
|
:emphasize-lines: 3
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
"request_transform": {
|
2022-01-12 15:00:18 +03:00
|
|
|
"body": "{\n \"users\": {\n \"name\": {{$body.input.arg1.username}},\n \"password\": {{$body.input.arg1.password}}\n }\n}",
|
2021-11-02 17:35:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Content Type
|
|
|
|
************
|
|
|
|
|
|
|
|
You can change the ``Content-Type`` of the request to either ``application/json`` or ``x-www-form-urlencoded``. The default is ``application/json``.
|
|
|
|
|
|
|
|
.. rst-class:: api_tabs
|
|
|
|
.. tabs::
|
|
|
|
|
|
|
|
.. tab:: Console
|
|
|
|
|
|
|
|
Console support coming soon.
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. tab:: CLI
|
|
|
|
|
|
|
|
Will be added soon
|
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
.. tab:: API
|
|
|
|
|
|
|
|
.. code-block:: json
|
|
|
|
:emphasize-lines: 7
|
|
|
|
|
|
|
|
{
|
|
|
|
"request_transform": {
|
|
|
|
"body": {
|
|
|
|
"name": "{{$body.input.name}}",
|
|
|
|
"email": "{{$body.input.email}}",
|
|
|
|
},
|
|
|
|
"content_type": "x-www-form-urlencoded"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
With ``x-www-form-urlencoded``, the key-value pairs in ``body`` are transformed to ``name={{$body.input.name}}&key2={{$body.input.email}}``.
|
|
|
|
|
|
|
|
URL
|
|
|
|
***
|
|
|
|
|
|
|
|
Transform the request URL. This can be used to embed, say user-id, in the url path.
|
|
|
|
You can also provide ``query_params`` to add to the URL.
|
|
|
|
You can use the `Kriti templating language <https://github.com/hasura/kriti-lang>`__ to construct any string value here.
|
|
|
|
|
|
|
|
.. rst-class:: api_tabs
|
|
|
|
.. tabs::
|
|
|
|
.. tab:: Console
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
In the ``Configure REST Connectors`` section, click on ``Add Request Options Transform``:
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. thumbnail:: /img/graphql/core/actions/request-options-transform.png
|
|
|
|
:alt: Change request URL
|
2022-01-10 21:39:15 +03:00
|
|
|
:width: 800px
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. tab:: CLI
|
|
|
|
|
|
|
|
Will be added soon
|
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
.. tab:: API
|
|
|
|
|
|
|
|
.. code-block:: json
|
|
|
|
:emphasize-lines: 3
|
|
|
|
|
|
|
|
{
|
|
|
|
"request_transform": {
|
|
|
|
"url": "{{$base_url}}/{{$session_variables['x-hasura-user-id']}}",
|
|
|
|
"query_params": {
|
|
|
|
"param1": "{{$body.input.value1}}",
|
|
|
|
"param2": "{{$body.input.value2}}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.. admonition:: escapeUri
|
|
|
|
|
|
|
|
Note that you must use the ``escapeUri`` function to urlencode templated values.
|
|
|
|
For example, if you have to use session variables in the URL and those may contain non-ASCII values,
|
|
|
|
then you should provide the template URL as ``{{$base_url}}/{{escapeUri $session_variables['x-hasura-user-id']}}``
|
|
|
|
|
|
|
|
Method
|
|
|
|
******
|
|
|
|
|
|
|
|
Transform the method. This can be used to change the request method, say from ``POST`` to ``GET``, as shown below.
|
|
|
|
|
|
|
|
.. rst-class:: api_tabs
|
|
|
|
.. tabs::
|
|
|
|
|
|
|
|
.. tab:: Console
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
In the ``Configure REST Connectors`` section, click on ``Add Request Options Transform``:
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. thumbnail:: /img/graphql/core/actions/request-method-transform.png
|
|
|
|
:alt: Change request method
|
2022-01-10 21:39:15 +03:00
|
|
|
:width: 800px
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. tab:: CLI
|
|
|
|
|
|
|
|
Will be added soon
|
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
.. tab:: API
|
|
|
|
|
|
|
|
.. code-block:: json
|
|
|
|
:emphasize-lines: 3
|
|
|
|
|
|
|
|
{
|
|
|
|
"request_transform": {
|
2022-01-17 16:27:01 +03:00
|
|
|
"method": "GET"
|
2021-11-02 17:35:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
Let's integrate Auth0's management API to update the profile of a user:
|
|
|
|
|
|
|
|
.. rst-class:: api_tabs
|
|
|
|
.. tabs::
|
|
|
|
|
|
|
|
|
|
|
|
.. tab:: Console
|
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
Go to the ``Actions`` tab on the console and create or modify an action. Scroll down to ``Configure REST Connectors`` section:
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
Action definition:
|
|
|
|
|
|
|
|
.. thumbnail:: /img/graphql/core/actions/example-transformation-0.png
|
2022-01-17 16:27:01 +03:00
|
|
|
:alt: Example rest connector for actions
|
2022-01-10 21:39:15 +03:00
|
|
|
:width: 1100px
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
The transformation is given by:
|
|
|
|
|
|
|
|
.. thumbnail:: /img/graphql/core/actions/example-transformation-1.png
|
2022-01-17 16:27:01 +03:00
|
|
|
:alt: Example rest connector for actions
|
2022-01-10 21:39:15 +03:00
|
|
|
:width: 800px
|
2021-11-02 17:35:35 +03:00
|
|
|
|
|
|
|
.. thumbnail:: /img/graphql/core/actions/example-transformation-2.png
|
2022-01-17 16:27:01 +03:00
|
|
|
:alt: Example rest connector for actions
|
2022-01-10 21:39:15 +03:00
|
|
|
:width: 1000px
|
2021-11-02 17:35:35 +03:00
|
|
|
|
2022-01-17 16:27:01 +03:00
|
|
|
.. tab:: CLI
|
|
|
|
|
|
|
|
Will be added soon
|
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
.. tab:: API
|
|
|
|
|
|
|
|
Action definition:
|
2022-01-17 16:27:01 +03:00
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
.. code-block:: graphql
|
2022-01-17 16:27:01 +03:00
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
type Mutation {
|
|
|
|
updateProfile(picture_url : String!) : ProfileOutput
|
|
|
|
}
|
2022-01-17 16:27:01 +03:00
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
type ProfileOutput {
|
|
|
|
id: String!
|
|
|
|
user_metadata: String!
|
|
|
|
}
|
2022-01-17 16:27:01 +03:00
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
The transform is given by:
|
2022-01-17 16:27:01 +03:00
|
|
|
|
2021-11-02 17:35:35 +03:00
|
|
|
.. code-block:: json
|
|
|
|
|
|
|
|
{
|
|
|
|
"request_transform": {
|
|
|
|
"body": "{\"user_metadata\": {\"picture\": {{$body.input.picture_url}} } }",
|
|
|
|
"url": "{{$base_url}}/{{$session_variables['x-hasura-user-id']}}",
|
|
|
|
"method": "PATCH"
|
|
|
|
}
|
|
|
|
}
|