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

Allow using database functions as GraphQL selectors instead of just tables and views. #394

Closed
dosco opened this issue Oct 9, 2022 · 9 comments

Comments

@dosco
Copy link
Owner

dosco commented Oct 9, 2022

Every function can return a table, ref https://www.postgresql.org/docs/current/sql-createfunction.html

EDIT:

Some of our functions also return a boolean, or a set or other return types, as mentioned in https://www.postgresql.org/docs/current/xfunc-sql.html

Originally posted by @krish7919 in #312 (comment)

@krish7919
Copy link

Hi @dosco, just checking in to see if there is a roadmap for this?

@dosco
Copy link
Owner Author

dosco commented Oct 22, 2022

Yes plan to work on it next week. working on another bug fix at the moment.

dosco added a commit that referenced this issue Nov 12, 2022
@dosco
Copy link
Owner Author

dosco commented Nov 12, 2022

Two new tests added to postgres_test.go to serve as examples to how this new functionality can be used. It will only work with functions that return records (aka tables). You can pass arguments to the functions with the args parameter. I can implement the argument passing as an object with name-value pairs but this was quicker for now.

CREATE OR REPLACE FUNCTION get_latest_users (n INTEGER, tag TEXT) 
RETURNS TABLE ( tag_name TEXT, id BIGINT, full_name TEXT ) 
LANGUAGE plpgsql 
as $$
BEGIN
	RETURN QUERY select tag as tag_name, u.id, u.full_name from users u order by u.id desc limit n;
END; $$; 
query {
  get_latest_users(limit: 2, args: [4, $tag]) {
	  tag_name
	  id
	  full_name
  }
}

@krish7919
Copy link

krish7919 commented Nov 16, 2022

A SQL function in Postgres can return this:

rettype

    The return data type (optionally schema-qualified). The return type can be a base, composite, or domain type, or can reference the type of a table column. Depending on the implementation language it might also be allowed to specify “pseudo-types” such as cstring. If the function is not supposed to return a value, specify void as the return type.

    When there are OUT or INOUT parameters, the RETURNS clause can be omitted. If present, it must agree with the result type implied by the output parameters: RECORD if there are multiple output parameters, or the same type as the single output parameter.

    The SETOF modifier indicates that the function will return a set of items, rather than a single item.

    The type of a column is referenced by writing table_name.column_name%TYPE.

Reference https://www.postgresql.org/docs/current/sql-createfunction.html

postgraphile supports all of this instead of just a record.

EDIT:

For our specific use case, we return a custom type in Postgres created using CREATE TYPE.

@dosco
Copy link
Owner Author

dosco commented Nov 16, 2022

Can you provide an example of a query using a function that returns non record values for example one that returns a string or boolean. How do you use a function as a selector in your query? An example will be helpful.

In GraphJin selectors are equal to tables they can have limit, where and other arguments. Open to evolving this I just need some examples beyond the link to the postgres doc on functions.

@dosco
Copy link
Owner Author

dosco commented Nov 16, 2022

For our specific use case, we return a custom type in Postgres created using CREATE TYPE.

Examples really help can you give me an example of this type of function (does it not return a type inherited from record) also an example GraphQL query using this will help.

@dosco
Copy link
Owner Author

dosco commented Nov 20, 2022

FYI Working on a fix

@dosco
Copy link
Owner Author

dosco commented Nov 28, 2022

Added support for functions with user-defined types CREATE TYPE. Next up working on adding support for functions that return singular values.

dosco added a commit that referenced this issue Dec 2, 2022
…394

1. db functions can now be used as fields or tables with suppot for named parameters
2. added top level __typename to work with fluter_graphql, etc  #404
@dosco
Copy link
Owner Author

dosco commented Dec 7, 2022

does this work for you? please close this issue if it does.

@dosco dosco closed this as completed Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants