Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could we have PRE event-trigger for customize data validation? #1291

Closed
hengsovandara opened this issue Jan 3, 2019 · 51 comments
Closed

Could we have PRE event-trigger for customize data validation? #1291

hengsovandara opened this issue Jan 3, 2019 · 51 comments
Assignees
Labels
c/actions Related to actions c/server Related to server

Comments

@hengsovandara
Copy link

I wonder whether I could have a pre-event-trigger to customize data validation before saving record. I known that Hasura has remote-schema which could do this, however, I think I don't need to create server to run the remote-schema. I just want to create Lambda function to check validation before saving records.

@hengsovandara
Copy link
Author

screenshot 35
Here is the example of the process that I need, where it is acting after client submit, but before writing stage into database.

@tirumaraiselvan
Copy link
Contributor

I concur it would be great to have a pre-commit webhook, but currently it is not easy to implement this in the present event triggers design. This is because the events are generated atomically with a mutation using native Postgres triggers. To accomplish pre-commit webhook, we will need to use something other than Postgres triggers so it will be a different sub-system.

@coco98 @0x777 thoughts on this feature?

For the time being, there are 2 solutions:

  1. Like you mentioned, you can implement your own graphql API on Lambda and stitch it with Hasura.
  2. You can perform a mutation to a temp table, which will trigger your validation lambda and in that validation lambda you can verify and perform an insert on the actual table. You can clean up the temp table afterwards.

@tirumaraiselvan tirumaraiselvan changed the title Could we have PRE or POST event-trigger for customize data validation? Could we have PRE event-trigger for customize data validation? Jan 3, 2019
@hengsovandara
Copy link
Author

@tirumaraiselvan , I am happy to get answer from you. And if I choose the second option, how could I get the error message to the client side, while it stores on event logs, not return into the client.

@hengsovandara
Copy link
Author

And How about the authentication system Hasura_Qraphql_Auth_Hook, is it Pre-Hook?

@tirumaraiselvan
Copy link
Contributor

@hengsovandara You will have to handle it asynchronously. You will have to emulate a sync like process for e.g. after performing the mutation to the temp table, you should wait/block the UI and poll (or use subscriptions) for the validation status.

Since validation is more naturally a sync process, hence approach 1) is recommended :)

Regarding auth system, yes it is pre-hook and that is exactly the kind of system which would be needed to achieve pre-hook event triggers. Let's see if there is enough interest in this.

@tirumaraiselvan
Copy link
Contributor

tirumaraiselvan commented Jan 3, 2019

@hengsovandara A 3rd way is to use CHECK constraints or postgres stored proc for validation although this is best suited for validations on data based on regexes and simple constraints.

@hengsovandara
Copy link
Author

hengsovandara commented Jan 3, 2019 via email

@tirumaraiselvan
Copy link
Contributor

@hengsovandara Understand :)

Why don't you give Remote Schemas a try? With the boilerplates available at https://github.com/hasura/graphql-serverless, I think it should be a very easy process. It would be same as your Lambda code and the wrapper code is already present. If there is any trouble, we will be happy to solve it for you.

@hengsovandara
Copy link
Author

hengsovandara commented Jan 3, 2019 via email

@trekze
Copy link

trekze commented Jan 31, 2019

@hengsovandara

When I was using firebase, I bumped into similar issues. Reading state from a database directly, with an ACL for access control, is amazing, and saves time. You also get "real-time" updates for free on both platforms. But modifying/creating state, almost always ends up needing some custom code.

Therefore the convention I came up with was simple. All state mutations go through cloud functions, and (almost all) read-only queries go straight to the DB.

I suspect a simliar convention would work well with Hasura so as to not get stuck down the line; i.e. use a remote schema for EVERYTHING that modifies state in the database. Use Hasura's awesome autogenerated schema for everything else.

@dsandip dsandip added c/server Related to server k/ideas Discuss new ideas / pre-proposals / roadmap and removed k/question labels May 13, 2019
@brodie-hodges
Copy link

If Hasura had this feature then you wouldn't need a separate remote schema server for 95% of cases. Most other business logic could be handled with events and subscriptions. Couldn't Hasura leverage the remote schema functionality internally to achieve this? Would also be great to have common field validations easily configurable: email, credit card / mod-10, US state / zip, sql injection prevention for text fields, URL format, etc.

@tirumaraiselvan
Copy link
Contributor

@brodie-hodges For common field types like email, credit card, zip, etc , you can perhaps use Postgres CHECK constraints : https://www.postgresql.org/docs/9.5/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS

For more complex use-cases, we have a solution in the works called "actions" which will be out pretty soon. This will eliminate remote schemas use for tasks like these.

@mnlbox
Copy link
Contributor

mnlbox commented Jun 16, 2019

@tirumaraiselvan good news brow 😉

@brodie-hodges
Copy link

@tirumaraiselvan Thanks for the update. Is this on a roadmap somewhere?

@icewind
Copy link

icewind commented Jul 1, 2019

Thanks for the update! Are there any details about upcoming "actions"? 😊

@iamjrock
Copy link

Thanks @tirumaraiselvan! Support for custom pre-insert/update/delete business logic validation via the upcoming "Actions" functionality will be incredibly useful.

Where can we follow the roadmap for this please?

I checked and the release candidate roadmap does not seem to mention Actions.

@lostb1t
Copy link

lostb1t commented Jul 25, 2019

Subscribe

@coco98 coco98 self-assigned this Jul 26, 2019
@cantaspinar
Copy link
Contributor

It would be great to have pre triggers for custom logic. Are there any updates on that?

@ec-raphael
Copy link

For more complex use-cases, we have a solution in the works called "actions" which will be out pretty soon. This will eliminate remote schemas use for tasks like these.

Any word on the "actions" solution @tirumaraiselvan ?

@0x777
Copy link
Member

0x777 commented Aug 12, 2019

Hi, we've been actively working on this. We'll have a preview build by the end of this week.

@MikeBosw
Copy link

A high level overview of actions has been made available here: https://deploy-preview-3042--hasura-docs.netlify.com/graphql/manual/actions/index.html (found on PR #3042)

Exciting :)

@jorgecolonconsulting
Copy link

jorgecolonconsulting commented Oct 11, 2019 via email

@lukaspili
Copy link

@2upmedia I just found the PR and read the doc, I don't have more info.
From my understanding, actions trigger a webhook so this logic happens outside of Hasura. But yeah, the most common architecture will be a serverless function running on node.

@jorgecolonconsulting
Copy link

@lukaspili thanks. I'll reach out to the person that created the PR.

@0x777 any ideas why there's a Python example in the documentation?

@MikeBosw
Copy link

@lukaspili thanks. I'll reach out to the person that created the PR.

@0x777 any ideas why there's a Python example in the documentation?

@2upmedia, if my understanding is correct, the HTTP endpoint is something you yourself create, which means that you can use any language, be it Node, Python, or Racket. In what sense is it supposed to be Node?

@valstu
Copy link

valstu commented Oct 11, 2019

Python syntax is almost like writing some sort of pseudo code so I guess it makes sense. But yeah, your backend could be written in any language so it really doesn’t matter like @MikeBosw said.

@coco98
Copy link
Contributor

coco98 commented Oct 11, 2019

Also, we're planning to add support for the action handler to be a postgres function (say, plv8) or an HTTP endpoint. So you can move from one to the other quite easily. For example, start off with a postgres function if its simple data validation type code and move to a HTTP endpoint later without affecting anything.

@marionschleifer marionschleifer added the c/actions Related to actions label Oct 14, 2019
@jorgecolonconsulting
Copy link

jorgecolonconsulting commented Oct 17, 2019

#1291 (comment)

@MikeBosw

This makes sense now. I wasn’t aware that the action could be an HTTP endpoint.

@mklueh
Copy link

mklueh commented Oct 17, 2019

Stumbled across this amazing project and handling pre-processing / validation logic was my only concern when adopting it. I'm very glad to hear you are working on a solution

@0x777 0x777 assigned 0x777 and unassigned coco98 Nov 29, 2019
@yasinarik
Copy link

@tirumaraiselvan , @coco98 , @0x777 , I've been waiting for a long time for actions feature to be released.

How long will it take? It will change our DB structure dramatically. Here is an example of a use case: #3536

Thanks for building the amazing Hasura!

@marionschleifer marionschleifer removed the k/ideas Discuss new ideas / pre-proposals / roadmap label Jan 7, 2020
@KirillSuhodolov
Copy link

Hi.
What is current status?
Lack of this feature is a blocker for me start using hasura.io for new projects

@rkawajan
Copy link

I'm so excited for this feature! Are there any updates on that?

@jmonday
Copy link

jmonday commented Feb 19, 2020

Maybe these are two different things, but I'd love to be able to specify when the event should be triggered. Right now, it looks like it's being triggered after the update/mutation is made. I'd like to be able to configure the event to run before an update/mutation. This way, my webhook can determine if the update is allowed and/or modify the response for additional processing (thinking ETL cases). If my response is, say 403, the mutation is not executed/blocked.

Thoughts?

@jmonday
Copy link

jmonday commented Feb 20, 2020

I was reading the documentation further and it seems that something similar is already in place but I'm not seeing it work as described (or I'm misunderstanding the documentation).

Webhook response structure

A 2xx response status code is deemed to be a successful invocation of the webhook. Any other response status will be deemed as an unsuccessful invocation which will cause retries as per the retry configuration.

I see that if I return a non 2xx status code, the retries are fired as expected but the database update has already been executed and not rolled back. When first reading about this feature, I thought of it almost like a database transaction where the transaction would not be committed if the status code was non 2xx.

@tirumaraiselvan
Copy link
Contributor

@jmonday The docs you are referring to are for Event-Triggers which are very different from Actions -- the topic of this thread.

Anyway, in event-triggers, you have transactionality between the database mutation and the creation of the event. But, you don't have transactionality with the invocation of the webhook (as it is an external system).

@tirumaraiselvan
Copy link
Contributor

tirumaraiselvan commented Apr 14, 2020

Hey everyone

Happy to announce that we have released Actions in v1.2 (currently in beta) to solve this problem. Please try out the beta from here https://github.com/hasura/graphql-engine/releases. Expecting to release stable by week-end.

Why did we build Actions to solve this problem?

  • Data validations, in general, are NOT simply static in nature (if they are static or even declarative, consider using CHECK constraints or permission rules). This means that most validations will involve checking things against the db: i) see if inventory stock > 0, ii) see if shopping cart size < 10 and so on.

  • In view of above, the validations and the operation cannot be separate. Otherwise, you will have a stricter programming environment, no transactionality, brittle business logic, etc

  • Complete control: You may need greater functionality in your operation. For e.g. suppose you want to validate in article to be "clean" in content during an insert_article, if the article is not "clean" you might want to perform red flagging the author by performing a different mutation instead (update_author).

How does Actions work?

All this is available via the CLI as well to fit nicely with your existing dev environment.

Closing this issue now, but feel free to ask any questions here or on Discord !

@dariocravero
Copy link
Contributor

Nice @tirumaraiselvan! Amazing work on actions.

For anyone else seeing this, we've been using them for a couple of weeks in production and they are super useful. Right off the bat, it saves you from having to write the graphql stuff surrounding your logic which means that a simple function like the one you were using in events pretty much works. 👏

One question I had was whether you'll be opening actions to use them as queries apart from mutations. I remember that when we discussed this @coco98 said it should be almost as easy as letting the keyword query be used instead of mutation. What do you think?

Another thing that comes to mind is being able to use the existing types within the app. enums would be particularly handy but also uuid, timestamptz etc. I know that I can write my own and we've been doing it so far but being able to access the system's ones would help keep things consistent. Is that something you thought about?

Thanks!

@tirumaraiselvan
Copy link
Contributor

@dariocravero Yes, query support is almost merged: #4032 and so is re-using postgres scalars: #4125 . We will keep improving other things as we go along.

@dariocravero
Copy link
Contributor

dariocravero commented Apr 14, 2020 via email

@RodolfoSilva
Copy link
Contributor

@tirumaraiselvan has any plan to add support for the Postgres functions?

@marionschleifer
Copy link
Contributor

Hey folks, in our next community call on Thursday, 27th of April, we will showcase a new permission type for insert mutations which allows you to use external webhooks to perform input validations. This will allow any kind of validation to be performed during inserts without touching the user database. Sign up for the community call here. Would love to see you there! ✨

hasura-bot pushed a commit that referenced this issue Nov 6, 2024
### What
This PR updates the engine to use the NDC Spec v0.2.0-rc.1 version. This
is very likely to be the final RC before release.

### How

The `ndc_models` crate got updated, which then resulted in the schema
migration code in `metadata_resolve` being updated. This affected a lot
of test results because connectors that used deprecated type
representations got migrated to other representations, and if a type
representation was missing then JSON was used instead.

The NDC request-sending code in `execute` was updated to send the
`X-Hasura-NDC-Version` header depending on the version of request
getting sent.

The custom connector was updated to be compatible with the new NDC
0.2.0-rc.1 types. This resulted in the schema changing, so a lot of
tests that contained the connector's schema were updated.

---------

Co-authored-by: Daniel Harvey <[email protected]>
V3_GIT_ORIGIN_REV_ID: b1c7081eb1ee6cffdead08328a857903102332c6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c/actions Related to actions c/server Related to server
Projects
None yet
Development

No branches or pull requests