graphile / graphile/crystal

Computed columns on a Polymorphic Table

Open
#1,951 3 comments 2 reactions 0 assignees View on GitHub
💅 enhancement
Dominant language
TypeScript
Stars
12.9k
Forks
625
Avg merge
5h 23m
Merged PRs (30d)
24

Description

### Summary

I'm trying to attach a computed column to a polymorphic table with postgraphile 5.0.0-beta.20. The column only shows up on the specific types, but not the interface, so querying it requires a call-out for each type.

### Steps to reproduce

Create a minimal schema as follows:

```sql
BEGIN;

CREATE TYPE base_type AS ENUM ( 'A', 'B' );

CREATE TABLE base (
id BIGSERIAL PRIMARY KEY,
type base_type NOT NULL
);

COMMENT ON TABLE base IS $$
@interface mode:single type:type
@type A name:VarA
@type B name:VarB
$$;

CREATE FUNCTION base_common(b base)
RETURNS INTEGER
AS $$
SELECT 42;
$$ LANGUAGE sql STABLE STRICT;

INSERT INTO base ( type ) VALUES ( 'A' );
INSERT INTO base ( type ) VALUES ( 'A' );
INSERT INTO base ( type ) VALUES ( 'B' );

COMMIT;
```

I launched postgraphile as follows:

```console
npx postgraphile --preset postgraphile/presets/amber --connection
```

### Expected results

I expected this query to work, to allow me to access the `common` field.

```graphql
query {
allBases {
nodes {
id
type
common
}
}
}
```

### Actual results

The above query caused the error message: `Cannot query field \"common\" on type \"Base\". Did you mean to use an inline fragment on \"VarA\" or \"VarB\"?`

I was able to access the `common` field with the following query. This is much more verbose, and makes adding additional polymorphic types a lot more difficult for my API consumers.

```graphql
query {
allBases {
nodes {
id
type
... on VarA {
common
}
... on VarB {
common
}
}
}
}
```

### Additional context

* Operating System: Linux (NixOS 23.11, but I doubt this is relevant).
* `node --version`: v18.18.2
* `postgres --version`: postgres (PostgreSQL) 15.5
* `postgraphile`: `5.0.0-beta.20`

Note: I've removed all the irrelevant parts of my schema to create a minimal working example. I understand that, as shown, there's no need for polymorphism, but it is required in my actual schema.

Thanks!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.