Any way to show new table connections via triggers?

Hi,

I’m trying to run code based on when new connections are created on a table (essentially when one record links to another). But neither the event.originalObject nor the event.data objects contain any reference to linked tables.

You can see the two objects listed below. The trigger was an update to an Account field that isn’t listed, so both objects are identical. Not only is the linked field that I updated missing, but all linked fields are missing.

event.originalObject:
{ "id": "cktg50ubl00b509lae2ph8zpo", "createdAt": "2021-09-11T18:44:25.858Z", "updatedAt": "2021-09-11T21:18:28.108Z", "deletedAt": 0, "createdById": "ckosqhgvt05jl07kwg8vo8fsa", "fullName": "Presenter 888"}

event.data:
{ "id": "cktg50ubl00b509lae2ph8zpo", "createdAt": "2021-09-11T18:44:25.858Z", "updatedAt": "2021-09-11T21:19:03.552Z", "deletedAt": 0, "createdById": "ckosqhgvt05jl07kwg8vo8fsa", "fullName": "Presenter 888"}

Does anyone know how I can create a trigger of some kind when a new connection is created?

Thanks!

Hello!

I’ve got your problem. I’ll let give you a response as soon as possible.

Lada Kokotova | Technical Support Engineer

1 Like

Hello!

At this time you cant use triggers on relations, in future we have a plan to change that, but now you have only one option:
Instead of trigger create task or resolver (depend on your logic) and run them manually when you connecting relations, here is a simple example for server-side code

export const POST_CREATE_MANY_MUTATION = gql`
  mutation MetricValueCreateMany($data: [PostCreateManyInput]!) {
    postCreateMany(data: $data) {
      id
    }
  }
`;

// Here is list of connected relations
const auhtorIds = [
  'ckoe61c0c02er09la4wd4556q',
  'ckoe64jfb011y07mhaz7m0o49',
];

await ctx.api.gqlRequest(
  POST_CREATE_MANY_MUTATION,
  { 
    data: [{
      title: "Example",
      author: {
        connect: {
          id: { in: auhtorIds }
        }
      }
    }, {
      title: "Example 2",
      author: {
        connect: {
          id: { in: auhtorIds }
        }
      }
    }]
  }
);

// Only allowed in server context (deployed custom functions)
await ctx.invokeFunction(
  'onConnectAuthorTask', // it can be task or even trigger
  { data: { auhtorIds } },
  { waitForResponse: false }, // if you don't need to wait for the trigger
);

if you make a request from the frontend (like react app) use your gql client instead of ctx.api.gqlRequest, and invokeFunction will be replaced with gql mutation.

Let me know if you have any questions about this example.

Lada Kokotova | Technical Support Engineer