Sharing Data Between Instances
This guide will help you share data between Instances using the remote_records and remote_users functionality. They are available on the Profile, User, and Record GraphQL result types.
You can use this functionality to keep data that is the same across the globe and you don't want to change it (or deploy it) in multiple places every time it changes. For example, a list of countries, country ISO codes, or timezones.
Find an example below.
Requirements
To follow the steps in this tutorial, you should be familiar with Records and Users.
- Records and Tables
- Users
- GraphQL API Reference: RemoteRecordsInterface
- GraphQL API Reference: RemoteUsersInterface
Steps
Step 1: Create model
First, create a model named child_model with the schema below:
---
name: child_model
custom_attributes:
- name: child_title
type: string
- name: child_id
type: integer
- name: parent_id_array
type: array
---
Step 2: Create child objects
To ensure you have some data in the child_model table, create two objects with the data below:
{
"child_id": 1,
"child_title": "Title 1",
"parent_id_array": [
"100",
"101"
]
}
{
"child_id": 2,
"child_title": "Title 2",
"parent_id_array": [
"101"
]
}
Step 3: Prepare Graph query
Create the get_main_records query that will retrieve records including the referenced remote data:
query get_main_records {
records(
page: 1
per_page: 10
sort: [{id: {order: ASC}}]
filter: {
table: { value: "child_model" }
}
) {
results {
id
properties
remote: remote_records(
endpoint: {
url: "https://some-remote-api/linked-records.json",
token: "secret-token"
}
join_on_property: "parent_id_array"
) {
id
properties
}
}
}
}
You will not execute the above query for now because you first need to ensure the Instance at https://some-remote-api/linked-records.json returns the correct data that you need.
Step 4: Create page
Create the page below inside the some-remote-api Instance:
---
slug: linked-records
format: json
authorization_policies:
- api_key_check
---
{% graphql g = 'get_linked_records', ids: params.ids %}
{{ g.records.results | json | html_safe }}
Step 5: Create authorization policy and Graph query
Create the referenced authorization policy and Graph query.
Authorization policy:
---
name: api_key_check
redirect_to: /
---
{% if context.headers.HTTP_AUTHENTICATION_TOKEN == "secret-token" -%}true{% endif -%}
Graph query:
query get_linked_records($ids: [String!]) {
records(
page: 1
per_page: 10
sort: [{id: {order: ASC}}]
filter: {
properties: [
{ name: "parent_id", value_in: $ids }
]
}
) {
results {
id
properties
}
}
}
Step 6: Create model
Create the model with the schema below in the some-remote-api instance.
---
name: parent_model
custom_attributes:
- name: parent_title
type: string
- name: parent_id
type: integer
---
Step 7: Create parent objects
{
"parent_id": 100,
"parent_title": "Parent Title 1"
}
{
"parent_id": 101,
"parent_title": "Parent Title 2"
}
Step 8: Execute Graph query
You are now ready to execute the get_main_records query, in the first Instance.
For each object returned, under remote, a list of records retrieved from the remote endpoint (https://some-remote-api/linked-records.json) will be listed if their ids (parent_id) are included in the current object's parent_id_array list of ids. Note that all remote objects will be retrieved in a single remote API call, so ensure the per_page parameter in get_linked_records is sufficiently large to accomodate all items to be retrieved.