-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
extend support for custom SQL functions (mutations, other functions types) #1514
Comments
@dsandip Is it possible to support
|
Would this include exposing Apologies if I misunderstood this issue (in which case I'll open a new one). |
@shahidhk any updates on this (since I saw that you were planning to implement it)?
Originally posted by @shahidhk in #1073 (comment) Thanks in advance. |
@MhdSyrwan There is no exact ETA at the moment. |
Is there anyway to track the function that are manually created right now? Selecting the track option won't work as it says the function does not return a "COMPOSITE" type and if I refresh the browser I lose all the declarations |
@tafelito you can't return a composite type from a function. A function has to return a SETOF table. |
@marionschleifer so if I have something like this
I don't have a SETOF table to return. Is there any other way I can keep track of this functions? |
@tafelito This is tracked in the Postgres schema as a trigger and the associated function. You don't want Hasura to "track" this. Hasura's tracking is for functions which you want to expose as GraphQL queries. |
So if I need to see the functionsI created or if I want to edit one I have to connect directly to Postgress outside Hasura? Is that what you're saying? Btw, I don;t want to expose this functions as GQL queries, but it'd be nice if I can edit them if I need them inside the hasura editor |
Yes, since it has nothing to do with Hasura or GraphQL. You can use the Run SQL window in Hasura also. Just don't tick the "Track this" box. |
Hi Friends, I am not sure if this is offtopic, but I managed to call PLPgsql Functions as mutations with Hasura 1.0 and I want to share my approach with you: https://mnesarco.github.io/blog/2020/02/25/hasura-postgresql-function-as-mutation |
@mnesarco your approach is great and self-integrated. it allows developers even to inspect and trace previous calls and their responses! 👍 Question: I didn't get this part BTW, for people who want a simple quick solution, they may just create a view with a trigger to replace the create action of it in order to make a single callable function. |
Hi @MhdSyrwan thanks for your review. I have fixed the missing "values" part. |
Example: You can create a dummy view like CREATE VIEW my_action AS SELECT 'dummy'; Then write a trigger to implement its INSERT action like this: CREATE OR REPLACE FUNCTION my_action_replace_insert ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $function$
DECLARE
BEGIN
-- function body here
END;
$function$;
CREATE TRIGGER my_action_replace_insert_trigger
INSTEAD OF INSERT ON "my_action"
FOR EACH ROW
EXECUTE PROCEDURE my_action_replace_insert (); Then, by tracking the |
I think that the view approach does not allow to return any result from the function but I can be wrong. I will try your idea and if it works i will add a section at the end of the article. |
You're right. it does not allow. Actually I didn't care about this feature since the whole point of this issue was to make a custom change in the database rather than to return something. Functions that return value without altering DB are already supported check #1073. Still, your approach would be the best for somebody trying to make a change and get some info at the same time. |
Yes my solution is for calling functions without write restrictions, without input restrictions and without returning restrictions. |
I have published a reference source code here: https://github.com/mnesarco/hasura-pg-actions |
@mnesarco. I think when actions is arrived we will have this feature built in. Right now we can do this with external functions. But you approach is very useful, but we need some permission checks before expose this for the users. I believe this is the way. |
I have a case where using a VOLATILE SQL function in a computed field would simplify things alot. I also noticed I can create the function as STABLE, add it as computed field, and then change the function to VOLATILE and it works just fine in hasura. But feels hacky and hasura complains on metadata reload. Also seem's there's not really much needed to support this except allowing VOLATILE function to be tracked, at least in my case. Will the work for this issue support this use case as well ? |
@karibertils look at linked draft pull request. |
@Bessonov I can see in the pull request that VOLATILE functions will be supported as mutations. But I can't really tell if adding those mutations as computed fields will also be supported like with the STABLE functions ? It might be a bit odd behaviour doing query { table { computed } } that's actually mutating data before returning the results. |
@karibertils There is no plan at the moment to allow Can you give me an idea of your use-case? Maybe you have a function that is "morally" side-effect -free, but needs to be marked VOLATILE for some reason? Also fwiw we also have some early plans to simplify/unify "function tracking" and "computed fields" APIs which should at least make this easier to talk about |
@jberryman Ability to query VOLATILE functions would open possiblity to implement server-side memoization. It was several times when I started to think "aha ... and we can cache result so it .. Stop - hasura will no allow it.. Sad" |
@ayuryshev thanks! Yep the caching use-case you describe makes sense to me. I don't think that will happen in the VOLATILE function-as-mutations work, since it requires some discussion. Would you mind opening a feature request issue and mention the use-case you just outlined? It would also be helpful if you wanted to link to resources that describe the technique you're using. |
PR is in review ^ |
This also supports tracking VOLATILE functions as queries. Review and discussion of the initial version of this work can be found in this closed PR: hasura#5858
This also supports tracking VOLATILE functions as queries. Review and discussion of the initial version of this work can be found in this closed PR: hasura#5858
Is this released in 1.4 ? |
Any update on when this will be released? |
@jberryman |
My use case for VOLATILE SQL function in query is as follows. For every user there should always be exactly one in-progress order. If I query If the user has no orders, or has only completed orders, it would create new empty order and return that as the current_order. If the user has existing in-progress order it would return the existing one. The intention is to simplify the API usage. Skipping the need to query, mutate, query again in cases with no current_order. The orders can get finished in a few different ways, and feels less error-prone handling it at a single place rather than tracking it in many. I know this can be accomplished using SQL triggers or in the backend etc. But having a VOLATILE SQL function + computed field handle this felt like clean solid way. |
|
Well done team Hasura. Volatile functions are more valuable to the users of your product than the dogma of graphql abstaining from them. I see this as an inflection point in customer value over technocracy that will serve Hasura well into being a global enterprise suite. |
The text was updated successfully, but these errors were encountered: