Task updating relationships works locally but not when deployed

I have a task that gets called when a json file is uploaded.

When that task runs, it does a createMany mutation

after the createMany is done. It loops over all the “items” that was created and does an update to add missing relationships.

When running this locally with an exact mock of the event.data it receives when it’s called. It works perfectly fine. It creates the new records and then updates them 1 by 1 to add the missing relationships.

However when it’s deployed and it runs with the exact same event.data it does the createMany correctly, and it does run the individual updates afterwards. But the items never gets updated with the relationships. :weary:

I’ve just spent the whole day adding this feature to almost all of my tables and then it doesn’t work serverside… Any idea what’s going on here?

Here’s the relevant code:

const ADD_SIGNAL_RELATIONSHIPS = `
  mutation ADD_SIGNAL_RELATIONSHIPS($id: ID, $ticker: String, $planName: String) {
  signalUpdate(data: {
    id: $id,
    plan: { connect: { planID: $planName } },
    report: { connect: { ticker: $ticker } },
    stock: { connect: { ticker: $ticker } }
  }) {
    id
  }
}
`;

 if (newSignals.length) {
    const { signalCreateMany } = await graphQLClient.request(
      CREATE_MANY_SIGNALS_MUTATION,
      {
        ...signalAdvancedDataVariables
      }
    );

    signalCreateMany.items.forEach(async signal => {
      // update to add relationships.
      await graphQLClient.request(ADD_SIGNAL_RELATIONSHIPS, {
        id: signal.id,
        ticker: signal.ticker,
        planName
      });
    });

I do have a signal.create before trigger which makes sure the stock it’s connecting to exists. But that all seems to work as expected.

Why is this task working locally but not working as expected on the server?

I tried adding a 1 second wait before it does the updates in case it was a weird race-condition or something. But it still didn’t work when deployed.

Please help… I can’t launch without working relationships, which has been quite problematic from the start.

Hey Mark,

It looks like the promise is lost here. Promise.all should accept an array of the promises, so the code should look like:

await Promise.all(signalCreateMany.items.map(signal => 
    // update to add relationships.
    graphQLClient.request(ADD_SIGNAL_RELATIONSHIPS, {
        id: signal.id,
        ticker: signal.ticker,
        planName
    );
});)

Note that you don’t even need an async modifier inside the lambda if you return a promise.

Lambda execution won’t wait for any pending callbacks and will simply exit before the completion. That definitely could be a reason for that issue.

4 Likes

@vepanimas Ahh yeah you’re totally right. When it’s running serverside I guess it gets cut off as soon as the function returns, whereas when you run it locally the function can return first and still finish running the remaining code.

Thanks for the help! That solved it with a minor fix :grin:

Great that it helped! You’re welcome.

1 Like