graphql-engine icon indicating copy to clipboard operation
graphql-engine copied to clipboard

Unable to restrict GraphQL Query based on Table Computed Field

Open jepthy opened this issue 3 years ago • 5 comments
trafficstars

I have a Table Computed Field that is associated with one of my tables, and which is returning several fields back in the resulting query. I would like to be able to add one of these fields to the Where condition of the GraphQL call, but Hasura does not show the computed row(s) in where condition builder.

The name of the Table Computed Field is "billingAggregates". It returns a row which has fields like "last_unbilled_date", "total_billed_amount", etc.

My GraphQL works fine when it looks like this: query GetUnbilledJobs($unbilled_as_of_date: timestamp) { billing_job( limit: 1000 ) { id name startDt endDt billingAggregates(args: {_wipenddate: $unbilled_as_of_date}) { last_unbilled_date total_billed_amount } } }

But if I change it to this, then I get an error that "field billingAggregates" not found in type billing_job_bool_exp" query GetUnbilledJobs($unbilled_as_of_date: timestamp) { billing_job( limit: 1000 where: {_and: [{_and: [{billingAggregates:{last_unbilled_date: {_gte: "2022-09-22 00:00:00"}}}, {billingAggregates:{last_unbilled_date: {_lt: "2022-9-23"}}}]}]} ) { id name startDt endDt billingAggregates(args: {_wipenddate: $unbilled_as_of_date}) { last_unbilled_date total_billed_amount } } }

I noticed that there is a ticket that says computed fields are now supported as of version 2.0.3, but I wonder if that is just single fields, and not table fields?

jepthy avatar Oct 03 '22 23:10 jepthy

Do you have column-level permissions set for the mentioned computed field(s) ?

Here is the link to the doc for your reference:

https://hasura.io/docs/latest/schema/postgres/computed-fields/#computed-fields-permissions

adas98012 avatar Oct 04 '22 16:10 adas98012

Thank you for your response.

As I read the documentation, the permissions should be set at the table level: "For table computed fields, the permissions set on the return table are respected."

My sql function returns a type of vw_billing_aggregate_record, which is an empty view that I have created (it is empty because the view is never used, it is needed as a return type for the sql function).

Permissions are set in Hasura to be able to select from this view. image

If I go to look at the row level permissions for the table that has these Table Computed Fields, I don't show the computed fields to be able to grant permissions. This is likely because it isn't a single computed field, it is a result set that is returned (i.e. it is a Table Computed Field vs a Scalar Computed Field) image

jepthy avatar Oct 04 '22 18:10 jepthy

Out of curiosity, do I need to track the function (like I do with other functions)? Right now I am only tracking the function in the context of the table it is being used on.

jepthy avatar Oct 04 '22 20:10 jepthy

Out of curiosity, do I need to track the function (like I do with other functions)? Right now I am only tracking the function in the context of the table it is being used on.

That didn't work.

jepthy avatar Oct 07 '22 02:10 jepthy

Unfortunately, ordering results by computed fields is not currently supported. There is an open Github issue on this. You may be interested in the suggested workaround in the issue as well. To summarize: Hasura can track functions as computed fields, but computed fields cannot be used in ordering and filtering Hasura can track functions as root fields, which can be used in ordering and filtering

adas98012 avatar Oct 12 '22 00:10 adas98012

Strangely enough it seems to work as admin but I cannot seem to make it work using roles

wieseljonas avatar Mar 06 '23 22:03 wieseljonas

Support for filtering using computed fields (also tables-valued computed fields) was introduced in v2.03 in July 2021.

I was unable to reproduce the error: When I have a table-valued computed fields with select permissions on both involved tables the fields do appear in the schema for me and I am able to filter accordingly.

Because of this, and because the issue has not seen any activity for several months I'm going to close it.

Feel free to reopen if the issue is still relevant for you.

plcplc avatar Jan 04 '24 09:01 plcplc