2

Recently, I started working with Postgraphile to implement an API for the use of the react app I'm currently working on.

As my app grows I find myself continuously writing new views with smart comments to create more complex queries to the database. Some of those include sub-queries, unions and coalesce statements. As for now there are around 20 to 30 views and it gets bigger harder to maintain.

Is there any alternative that still allows me to use the flexibility and power of Postgraphile without the need of continuously writing more and more views.

Thanks :)

maryum375
  • 727
  • 1
  • 10
  • 22

1 Answers1

3

PostGraphile maintainer here. I don't use views at all (with the exception of a few carefully selected materialized views) when working in PostGraphile; instead I use SQL functions. SQL functions have a number of advantages over views when used with PostGraphile: they tend to be more performant (particularly when they're inlined), they can be used to add new/better relations, they respect your RLS policies/grants, they can side-step security if you need them to (SECURITY DEFINER) and you can declare their "cost" which is useful if you're using something like query cost analysis. Take a look at custom queries and computed columns

Depending on what you're trying to achieve, you can also solve many things with makeExtendSchemaPlugin.

A good source of advice on this is the members of our Discord chat.

It's hard to advise further without knowing what problems you are solving with views :)

Benjie
  • 7,701
  • 5
  • 29
  • 44