Postgres: Action to Database Relationships
Action relationships allow you to join data across tables and actions. Once you create relationships between types from your database and types created from actions, you can then "join" them by running GraphQL queries.
Actions are a way to extend Hasura’s schema with custom business logic using custom queries and mutations. The resolvers for these custom fields are written in REST endpoints. They are especially useful for setting up serverless functions as resolvers.
Create an action relationship
Step 1: Create an action
Step 2: Define and create the relationship
The following values can be defined for an action relationship:
- Relationship type: Select a type of relationship.
- Object relationship: For one-to-one relationships.
- Array relationship: For one-to-many relationships.
- Relationship name: Create a name for the relationship.
- Reference schema: Select a reference schema from your database.
- Reference table: Select a table from your database.
- From: Select a field returned in the action response.
- To: Select a column from the reference table to join the field to.
In this example, we're creating a relationship for the
createUser action. We're creating a relationship called
id field returned in the action response, to the
id column of the
Head to the
Actions -> [action-name] -> Relationshipstab.
Add a relationship.
In the section opened by the above step, fill out the following fields and hit
You can add an action relationship by adding it to the respective custom type in the
actions.yaml file inside the
- name: UserOutput
Apply the Metadata by running:
hasura metadata apply
You can create an action relationship when defining custom types via the set_custom_types Metadata API:
POST /v1/metadata HTTP/1.1
Step 3: Explore with GraphiQL
API tab, test out your action relationship.
If your table has an existing remote relationship, you can also query the fields from the Remote Schema.