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

Computed columns on a Polymorphic Table #1951

Open
albertkoch opened this issue Feb 8, 2024 · 1 comment
Open

Computed columns on a Polymorphic Table #1951

albertkoch opened this issue Feb 8, 2024 · 1 comment
Labels

Comments

@albertkoch
Copy link

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:

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:

npx postgraphile --preset postgraphile/presets/amber --connection <redacted>

Expected results

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

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.

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!

@benjie
Copy link
Member

benjie commented Feb 8, 2024

This will need investigation; thanks for finding and filing the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 🐭 Shrew
Development

No branches or pull requests

2 participants