这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
efe6bfc
add actions for query
codingkarthik Apr 3, 2020
e05c6c3
add action to query_root or mutation_root depending on `type`
codingkarthik Apr 6, 2020
dcb1e4a
[not compiling] add function to resolve query action
codingkarthik Apr 6, 2020
95fef81
Merge remote-tracking branch 'upstream/master' into query-actions-4032
codingkarthik Apr 6, 2020
9c4adf1
refactor QCACtionFetch to QCAsyncActionFetch
codingkarthik Apr 6, 2020
30fa99a
add HTTP manager and reqHeaders in type classes of query functions
codingkarthik Apr 7, 2020
9b85363
refactor mutationField to actionField in mkActionsSchema
codingkarthik Apr 7, 2020
c2cdf7a
add tests for query action
codingkarthik Apr 7, 2020
3d77add
throw500 when encountered an async query action
codingkarthik Apr 7, 2020
a518993
update the changelog
codingkarthik Apr 7, 2020
a9b1ed4
add the type argument in the ActionDefinition
codingkarthik Apr 7, 2020
1205aee
refactor ActionExecutionContext to ActionMutationExecutionContext
codingkarthik Apr 7, 2020
71a87c5
refactor schema actions to differentiate b/w query and mutation actions
codingkarthik Apr 7, 2020
97fbeda
refactor action schema module
codingkarthik Apr 7, 2020
6b56f1b
throw 400 when a query action is tried to explain
codingkarthik Apr 8, 2020
a77f26d
fix bug: the top level output of query actions was always list
codingkarthik Apr 8, 2020
fb1fbb6
modify the query actions tests to test for relationships and output t…
codingkarthik Apr 8, 2020
e825fc9
remove _adKind from action definition and modify _adType
codingkarthik Apr 8, 2020
40b4a7e
incorporate the change made to _adType in action schema
codingkarthik Apr 8, 2020
041a4dc
refactor ActionExecution prefix to ActionMutation prefix
codingkarthik Apr 8, 2020
3f45e87
update the actions docs to include query actions
codingkarthik Apr 9, 2020
af07dbe
make a new type QueryActionExecuter
codingkarthik Apr 9, 2020
9012cdf
add doc to QueryActionExecuter and redundant constraints pragma
codingkarthik Apr 9, 2020
1a4bd97
Merge remote-tracking branch 'upstream/master' into query-actions-4032
codingkarthik Apr 13, 2020
d892d98
update actions docs to include query type actions
Apr 14, 2020
15bb1fa
update the action docs
codingkarthik Apr 14, 2020
40c9bff
refactor the ToJSON and ToOrdJSON of ActionDefinition
codingkarthik Apr 15, 2020
184757b
Merge remote-tracking branch 'upstream/master' into query-actions-4032
codingkarthik Apr 15, 2020
b0ccf35
Merge branch 'master' into query-actions-4032
codingkarthik Apr 15, 2020
65a5642
Merge branch 'master' into query-actions-4032
codingkarthik Apr 15, 2020
14f0ebc
Merge branch 'master' into query-actions-4032
tirumaraiselvan Apr 16, 2020
4cdf12f
Merge branch 'master' into query-actions-4032
codingkarthik Apr 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Read more about check constraints on [Postgres Docs](https://www.postgresql.org/
- cli: fix flags in actions, migrate and metadata cmd (fix #3982) (#3991)
- cli: preserve action definition in metadata apply (fix… (#3993)
- cli: bug fixes related to actions (#3951)
- server: add support for query actions (#4032)

## `v1.2.0-beta.1`

Expand Down
2 changes: 1 addition & 1 deletion docs/graphql/manual/actions/action-connect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Connecting actions with the graph
Use case
--------

Actions are a way to extend your GraphQL schema by adding custom mutations. It
Actions are a way to extend your GraphQL schema with custom queries or mutations. It
is a typical use case that the custom actions' response is actually related to
existing objects in the schema. e.g. a custom ``insertAuthor`` action will be
related to the ``author`` object in the schema. Hence, we would want to be able
Expand Down
4 changes: 2 additions & 2 deletions docs/graphql/manual/actions/action-handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ defined in a handler which is an HTTP webhook.
HTTP handler
------------

When the action mutation is called, Hasura makes a ``POST`` request to the
handler with the mutation arguments and the session variables.
When the action is executed i.e. when the query or the mutation is called, Hasura makes a ``POST`` request to the
handler with the action arguments and the session variables.

The request payload is of the format:

Expand Down
4 changes: 4 additions & 0 deletions docs/graphql/manual/actions/async-actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ If you mark an action as **asynchronous**, Hasura also generates a
``query`` and a ``subscription`` field for the action so that you can
query/subscribe to its status.

.. admonition:: Note

Only actions of type ``mutation`` can be async. Actions of type query are always executed synchronously.

For example, let's say ``place_order`` is an asynchronous action

.. code-block:: graphql
Expand Down
211 changes: 191 additions & 20 deletions docs/graphql/manual/actions/create.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ Creating actions
Introduction
------------

An action is a GraphQL mutation. You have to define the GraphQL type of the
arguments that the mutation accepts and the GraphQL type of its response.
An action is a GraphQL query or mutation. You have to define the GraphQL type of the
arguments that the query or mutation accepts and the GraphQL type of its response.

To create an action, you have to:

1. Define the mutation
1. Define the query or mutation
2. Define the required types
3. Create a handler

**For example**, let's start with a basic mutation that accepts a list of numbers and returns
their sum. We'll call this mutation ``addNumbers``.
Let's look at **examples** for mutation and query type actions.

Step 0: Setup
-------------
Setup
-----

.. rst-class:: api_tabs
.. tabs::
Expand Down Expand Up @@ -73,8 +72,16 @@ Step 0: Setup
``metadata/`` directory.


Mutation type action
--------------------

Let's start with a mutation that accepts a username and password, and returns
an access token. We'll call this mutation ``login``.

Step 1: Define your mutation and associated types
-------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Start with defining the mutation and the required types. These types will reflect in the GraphQL schema.

.. rst-class:: api_tabs
.. tabs::
Expand All @@ -92,6 +99,169 @@ Step 1: Define your mutation and associated types
.. code-block:: graphql

type Mutation {
login (username: String!, password: String!): LoginResponse
}

In the above action, we called the returning object type to be ``LoginResponse``.
Define it in the ``New types definition`` as:

.. code-block:: graphql

type LoginResponse {
accessToken: String!
}

.. tab:: CLI

To create an action, run

.. code-block:: bash

hasura actions create login

This will open up an editor with ``metadata/actions.graphql``. You can enter
the action's mutation definition and the required types in this file. For your
``login`` mutation, replace the content of this file with the following
and save:

.. code-block:: graphql

type Mutation {
login (username: String!, password: String!): LoginResponse
}

type LoginResponse {
accessToken: String!
}

The above definition means:

* This action will be available in your GraphQL schema as a mutation called ``login``.
* It accepts two arguments called ``username`` and ``password`` of type ``String!``.
* It returns an output type called ``LoginResponse``.
* ``LoginResponse`` is a simple object type with a field called ``accessToken`` of type ``String!``.

Step 2: Create the action handler
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A handler is an HTTP webhook where you can perform the custom logic for the
action. In this case, we will just return an access token, but typically you would want to run all the business logic that the action demands. NodeJS/Express code
for this handler would look something like:

.. code-block:: js

const handler = (req, resp) => {
// You can access their arguments input at req.body.input
const { username, password } = req.body.input;

// perform your custom business logic
// check if the username and password are valid and login the user

// return the response
return resp.json({
accessToken: "Ew8jkGCNDGAo7p35RV72e0Lk3RGJoJKB"
})

};

You can deploy this code somewhere and get the URI. For getting started quickly, we
also have this handler ready at ``https://hasura-actions-starter-kit.glitch.me/login``.

**Set the handler**

Now, set the handler for the action:

.. rst-class:: api_tabs
.. tabs::

.. tab:: Console

Set the value of the ``handler`` field to the above endpoint.

.. tab:: CLI

Go to ``metadata/actions.yaml``. You must see a handler like ``http://localhost:3000``
or ``http://host.docker.internal:3000`` under the action named ``login``.
This is a default value taken from ``config.yaml``.

Update the ``handler`` to the above endpoint.

.. admonition:: URL templating

To manage handler endpoints across environments it is possible to template
the endpoints using ENV variables.

e.g. ``https://my-handler-endpoint/addNumbers`` can be templated to ``{{ACTION_BASE_ENDPOINT}}/addNumbers``
where ``ACTION_BASE_ENDPOINT`` is an ENV variable whose value is set to ``https://my-handler-endpoint``

Step 3: Finish action creation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Finally, to save the action:

.. rst-class:: api_tabs
.. tabs::

.. tab:: Console

Hit ``Create``.

.. tab:: CLI

Run ``hasura metadata apply``.


Step 4: Try it out
~~~~~~~~~~~~~~~~~~

In the Hasura console, head to the ``GraphiQL`` tab and try out the new action.

.. graphiql::
:view_only:
:query:
mutation {
login (username: "jondoe", password: "mysecretpassword") {
accessToken
}
}
:response:
{
"data": {
"login": {
"accessToken": "Ew8jkGCNDGAo7p35RV72e0Lk3RGJoJKB"
}
}
}

And that's it. You have extended your Hasura schema with a new mutation.

Query type action
-----------------

Let's start with a basic query that accepts a list of numbers and returns
their sum. We'll call this query ``addNumbers``.

Step 1: Define your query and associated types
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Start with defining the query and the required types. These types will reflect in the GraphQL schema.

.. rst-class:: api_tabs
.. tabs::

.. tab:: Console

Go to the ``Actions`` tab on the console and click on ``Create``. This will
take you to a page like this:

.. thumbnail:: ../../../img/graphql/manual/actions/action-create-page.png
:alt: Console action create

Define the action as follows in the ``Action Definition`` editor.

.. code-block:: graphql

type Query {
addNumbers (numbers: [Int]): AddResult
}

Expand All @@ -113,13 +283,13 @@ Step 1: Define your mutation and associated types
hasura actions create addNumbers

This will open up an editor with ``metadata/actions.graphql``. You can enter
the action's mutation definition and the required types in this file. For your
``addNumbers`` mutation, replace the content of this file with the following
the action's query definition and the required types in this file. For your
``addNumbers`` query, replace the content of this file with the following
and save:

.. code-block:: graphql

type Mutation {
type Query {
addNumbers (numbers: [Int]): AddResult
}

Expand All @@ -129,13 +299,13 @@ Step 1: Define your mutation and associated types

The above definition means:

* This action will be available in your GraphQL schema as a mutation called ``addNumbers``
* This action will be available in your GraphQL schema as a query called ``addNumbers``
* It accepts an argument called ``numbers`` which is a list of integers.
* It returns an output type called ``AddResult``.
* ``AddResult`` is a simple object type with a field called ``sum`` of type integer.

Step 2: Create the action handler
---------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A handler is an HTTP webhook where you can perform the custom logic for the
action. In this case, it is the addition of the numbers. NodeJS/Express code
Expand All @@ -161,11 +331,10 @@ for this handler would look something like:
}
};

You can deploy this code somewhere and get URI. For getting started quickly, we
You can deploy this code somewhere and get the URI. For getting started quickly, we
also have this handler ready at ``https://hasura-actions-starter-kit.glitch.me/addNumbers``.

Set the handler
***************
**Set the handler**

Now, set the handler for the action:

Expand Down Expand Up @@ -193,7 +362,9 @@ Now, set the handler for the action:
where ``ACTION_BASE_ENDPOINT`` is an ENV variable whose value is set to ``https://my-handler-endpoint``

Step 3: Finish action creation
------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Finally, to save the action:

.. rst-class:: api_tabs
.. tabs::
Expand All @@ -208,14 +379,14 @@ Step 3: Finish action creation


Step 4: Try it out
------------------
~~~~~~~~~~~~~~~~~~

In the Hasura console, head to the ``GraphiQL`` tab and try out the new action.

.. graphiql::
:view_only:
:query:
mutation MyMutation {
query {
addNumbers(numbers: [1, 2, 3, 4]) {
sum
}
Expand All @@ -229,4 +400,4 @@ In the Hasura console, head to the ``GraphiQL`` tab and try out the new action.
}
}

And that's it. You have created your first action!
And that's it. You have extended your Hasura schema with a new query.
21 changes: 9 additions & 12 deletions docs/graphql/manual/actions/derive.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ executing a mutation, for example, to perform validations or to enrich some data
from an external source. Actions can be used to achieve this.

To help with creation of such actions, Hasura lets you derive an action from an
existing mutation by:
existing query or a mutation by:

- Auto-generating the GraphQL types defining the action
- Generating the handler code to delegate the action back to the original mutation
- Generating the handler code to delegate the action back to the original query or mutation
after executing some business logic

Generate derived action GraphQL types
-------------------------------------

Hasura can generate the relevant GraphQL types required to define an action
given a mutation request.
given a GraphQL operation.

For example, let's say we would like to derive an action from the mutation:

Expand Down Expand Up @@ -127,7 +127,7 @@ derive our action:
.. note::

- The derived output type will be derived from the actual output type of the
original mutation and not the mutation string.
original query or mutation and not the selection-set of the given query or mutation string.
- As currently custom object types can only have scalar / enum fields any
object type fields in the original output type will be dropped in the derived
output type.
Expand All @@ -138,7 +138,7 @@ Generate handler code for a derived action
------------------------------------------

For a derived action, Hasura can generate the relevant handler code to delegate
the action back to the original mutation.
the action back to the original operation.

.. rst-class:: api_tabs
.. tabs::
Expand Down Expand Up @@ -169,12 +169,9 @@ the action back to the original mutation.
the corresponding codegen files. Hit `y` to generate the codegen files with
the delegation logic.

.. note::

The CLI does not persist information about derived actions. Hence it is
currently only possible to generate handler files with the delegation
logic during action creation.


The CLI does not persist information about derived actions. Hence if you wish to generate the delegation code,
you might want to pass the query or mutation string while running the codegen command:

.. code-block:: bash

hasura actions codegen <action-name> --derive-from '<query/mutation string>'
Loading