The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
blog post for pg_graphql v1.2
Oliver Ricece5d36552a46
diff --git a/apps/www/_blog/2023-04-19-whats-new-in-pg-graphql-v1-2.mdx b/apps/www/_blog/2023-04-19-whats-new-in-pg-graphql-v1-2.mdxnew file mode 100644index 00000000000..e631e263318--- /dev/null+++ b/apps/www/_blog/2023-04-19-whats-new-in-pg-graphql-v1-2.mdx@@ -0,0 +1,193 @@+---+title: 'What''s New in pg_graphql v1.2'+description: 'New Features in the v1.2 release of pg_graphql'+launchweek: 7+tags:+ - postgres+ - graphql+ - planetpg+date: '2023-04-94'+published_at: '2023-04-14T08:00:00.000-07:00'+toc_depth: 3+author: oli_rice+image: launch-week-6/pggraphql/og-pg-graphql.png+thumb: launch-week-6/pggraphql/og-pg-graphql.png+---++It's been 4 months since the [1.0.0 release of pg_graphql](https://supabase.com/blog/pg-graphql-v1).+Since then, we’ve pushed several features to improve the APIs `pg_graphql` produces.++In this article, we’ll walk through those features and show examples of each.++### View Support++Prior to `v1.1` `pg_graphql` was restricted to reflecting standard tables. Following that release, views, materialized views, and foreign tables are similarly reflected in the GraphQL schema.++```sql+create view "ProjectOwner" as+ select+ acc.id,+ acc.name+ from+ account acc+ join role r+ on r.id = acc.role_id+ where+ acc.role = 'project_owner'+```++Since all entities exposed by pg_graphql require primary keys, we must define that constraint for the view using a comment directive++```sql+comment on view "ProjectOwner"+ is '@graphql({"primary_key_columns": ["id"]})';+```++which yields the GraphQL type++```graphql+type ProjectOwner implements Node {+ nodeId: ID!+ id: UUID!+ name: String+}+```++With associated `Edge` and `Connection` types. That enables querying via++```graphql+{+ projectOwnerCollection (first: 2) {+ edges {+ node {+ nodeId+ name+ }+ }+ }+}+```++Additionally, simple views automatically support mutation events like inserts and updates. That capability allows users to migrate underlying tables while maintaining backwards compatibility with previous API versions.++## Filtering++Filtering in SQL is endlessly flexible. We’ve taken two incremental steps to bring more of that flexibility to the GraphQL interface.++### `is null` and `is not null`++`null` handling is notorious fiddly in both SQL and GraphQL. Despite that, there is common ground we can exploit. In `pg_graphql` each scalar data type has a dedicated filter type e.g. `IntFilter` , `StringFilter` . Each of those filter types now supports an `is` argument allowing filtering on if a value `is null` or `is not null` using `{is: NULL}` and `{is: NOT_NULL }` respectively.++```graphql+enum FilterIs {+ NULL+ NOT_NULL+}++type IntFilter {+ ...+ is: FilterIs+}+```++For example:++```graphql+{+ blogCollection(filter: { name: {is: NULL}}) {+ ...+ }+}+```++to return all `blog`s where the `name` is null.++### **`like`**, **`ilike`**, and **`startsWith`**++Text filtering options in `pg_graphql` have historically been restricted to equality checks. The hesitation was over concerns about exposing a default filter that is difficult to index. The combination of [citext](https://www.postgresql.org/docs/current/citext.html) and [PGroonga available on the platform](https://supabase.com/docs/guides/database/extensions/pgroonga) solves those scalability risks and enabled us to expand the `StringFilter` with options for `like` `ilike` and `startsWith`.++```graphql+input StringFilter {+ eq: String+ ...+ startsWith: String+ like: String+ ilike: String+}+```++Note that `startsWith` filters should be preferred where appropriate because they can leverage simple B-Tree indexes to improve performance.++```graphql+{+ generalLedgerCollection(filter: {identifierCode: {startsWith: "BX1:"}}) {+ edges {+ node {+ nodeId+ identifierCode+ amount+ }+ }+ }+}+```++### GraphQL directives `@skip` and `@include`++The GraphQL spec has evolved over time. Although the spec is clear, it is common for GraphQL servers to selectively omit some chunks of functionality. For example, some frameworks intentionally do not expose an introspection schema as a form of security through obscurity.++`pg_graphql` aims to be unopinionated and adhere exactly to the spec. The `@skip` and `@include` directives are part of the [GraphQL core specification](https://spec.graphql.org/October2021/#sec--skip) and are now functional.++The **`@skip`** directive in GraphQL is used to conditionally skip a field or fragment during query execution based on a Boolean variable. It can be used to make the query more efficient by reducing the amount of data retrieved from the server.++The **`@include`** directive is the mirror of **`@skip`** where a field or fragment is conditionally included depending on the value of a Boolean variable.++Here's an example of how the **`@skip`** directive can be used in a GraphQL query:++```graphql+query getBooks($includeDetails: Boolean!) {+ booksCollection {+ edges {+ node {+ id+ title+ description @skip(if: $includeDetails)+ }+ }+ }+}+```++### User Defined Descriptions++Users can now use the [comment directive system to assign descriptions](https://supabase.github.io/pg_graphql/configuration/#description) to tables, views and columns.++```sql+create table public.book(+ id int primary key,+ title text not null+);++comment on table public.book+is e'@graphql({"description": "a library book"})';++comment on column public.book.title+is e'@graphql({"description": "the title of the book"})';+```++GraphQL IDEs, such as GraphiQL render those descriptions, allowing developers to provide clearer API documentation.++<Img+ src="/images/blog/2023-04-19-pg_graphql-v1_2/description_directive.png"+ wide={false}+ alt="description comment directive"+/>++## Roadmap++The headline features we aim to launch in coming releases of `pg_graphql` include:++1. Support for user-defined functions: [GitHub issue](https://github.com/supabase/pg_graphql/issues/222)+2. Support for nested inserts: [GitHub issue](https://github.com/supabase/pg_graphql/issues/294)+3. An alternative approach to computed relationships based on SQL functions returning **`SET OF`** rather than comment directives (compatible with PostgREST)+diff --git a/apps/www/public/images/blog/2023-04-19-pg_graphql-v1_2/description_directive.png b/apps/www/public/images/blog/2023-04-19-pg_graphql-v1_2/description_directive.pngnew file mode 100644index 00000000000..efd1e45dd02Binary files /dev/null and b/apps/www/public/images/blog/2023-04-19-pg_graphql-v1_2/description_directive.png differ