Postgraphile: Run makeWrapResolversPlugin on succesful insert and access JWT inside

Created on 22 Oct 2020  ·  2Comments  ·  Source: graphile/postgraphile

Summary

First of all, thanks for the awesome work on this project!
For a set of web applications I'm working on atm, some users need to be able to register new users. To do so, we need to insert them into the database (which postgraphile handles wonderfully), but also register new users with an external vendor via their REST API.
I thought I'd use makeWrapResolversPlugin to write a little function to do so, but encounter some questions which I can't really seem to figure out:

  1. From my understanding, any functions passed via makeWrapResolversPlugin is run before hitting the database. Are there options available to have it run after it does so? Only if the transaction commited successfully?
  2. Could I access a JWT from inside such a function for verification? (Users are added by existing users, who require the right permissions for this).

Thanks in advance!

Additional context

Postgres 13
Node 14.14.0
Postgraphile 14.9.0

❔ question

All 2 comments

The approach you outline (performing the action after the transaction completes) is very vulnerable to temporary failures. It can mean that the user is successfully created but due to a network error never gets created on the external vendor.

If it's not reasonable to do this as part of the transaction (so that you can roll back on failure), a much better approach would be to use a job queue for this. Create a trigger on the users table that queues a job when a user is inserted, and then have the worker upload to the external vendor API, automatically retrying with exponential backoff in the case of errors.

Check out: https://github.com/graphile/worker


[semi-automated message] Thanks for your question; hopefully we're well on the way to helping you solve your issue. This doesn't currently seem to be a bug in the library so I'm going to close the issue, but please feel free to keep requesting help below and if it does turn out to be a bug we can definitely re-open it 👍

You can also ask for help in the #help-and-support channel in our Discord chat.

Thanks for your quick response @benjie ! I'll be looking into this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

WestleyArgentum picture WestleyArgentum  ·  3Comments

5argon picture 5argon  ·  4Comments

mrbarletta picture mrbarletta  ·  5Comments

tazsingh picture tazsingh  ·  3Comments

giacomorebonato picture giacomorebonato  ·  3Comments