Logo-amall

Configuration suggestion

Last active 11 days ago

6 replies

2 views

  • ME

    Hi everyone, I’m using functions to do some custom business logic, and I constantly finding myself creating a new NhostClient, I would love to hear some suggestions of what is the best way to do this. (because I’m clearly doing it the wrong way).

    Also, I wanted to know  How can I detect which user is performing the call to the function? headers?

  • PK

    I'm not the full expert and a little behind in the version of the SDK I'm using so this might be a little out of date:

    • you can always set the headers in each use (e.g. call) of the nhost client the way you want them. E.g. pass headers into nhost.graphql.request() as part of the options.
    • in some functions (e.g. actions) nhost SDK I believe that client will know the calling user and creating a client will use that info when making the call so you don't need to do anything. But this is not true for a public webhook for example. In fact, I've generally had to figure out the user from inbound information in the webhook call in this case, then set the client's headers.
  • PK
    • actions: these are invoked via GraphQL calls so the headers are set to the calling user, role etc. You can copy them from the payload into the client's headers or call's options.
  • PK
    • events: these are invoked when some a DB row changes. The payload contains old/new/updated data and would contain whatever is in the DB schema. If that's a new user, then you get the user ID alongside other auth.user table row info. If it's a something you built in public schema and you have a foreign Key column to auth.user.id, you can then query using GraphQL to get that user and act on their behalf to do other stuff.
  • PK

    Hope that helps.

  • ME

    Thank you very much @PK ,
    But I’m searching for more technical way (if there is) to reduce the amount of new clients that I’m creating, I’m just don’t know if this is the right way to use NhostClient..

Last active 11 days ago

6 replies

2 views