Postgraphile: Is @foreignKey available for 2 or more columns in 1 view?

Created on 29 Apr 2021  ·  3Comments  ·  Source: graphile/postgraphile

Summary

In my understanding, @foreingKey smart tags can be used for only 1 column in 1 view.

CREATE TABLE product (
  product_id BIGSERIAL,
  name TEXT
);

CREATE TABLE customer (
  customer_id BIGSERIAL,
  name TEXT,
  email TEXT
);

CREATE TABLE order (
  order_id BIGSERIAL,
  product_id BIGINT NOT NULL REFERENCES product (product_id) ON UPDATE CASCADE ON DELETE RESTRICT,
  customer_id BIGINT NOT NULL REFERENCES customer (customer_id) ON UPDATE CASCADE ON DELETE RESTRICT,
  amount INT
);

CREATE VIEW some_view AS (
  SELECT product.product_id
       , customer.customer_id
       , order.order_id
  FROM order
    INNER JOIN product ON order.product_id = product.product_id
    INNER JOIN customer ON order.customer_id = customer.customer_id
);

In this definition above in Postgres, I can define a @foreginKey smart tags like this by library usage.

import { makeJSONPgSmartTagsPlugin } from 'graphile-utils';
import { postgraphile } from 'postgraphile';

const graphqlServer = postgraphile('DB_URL', 'public', {
  // ... some other options
  appendPlugins: [
    makeJSONPgSmartTagsPlugin({
      version: 1,
      config: {
        class: {
          some_view: {
            tags: {
              foreignKey: '(order_id) references order (order_id)'
            }
          }
        }
      }
    }),
  ]
});
query {
  someViews {
    nodes {
      productId
      customerId
      orderId
      order {  # <- thanks to foreignKey
        amount
      }
    }
  }
}

I want to add more @foreignKey for other tables like product and customer.

query {
  someViews {
    nodes {
      product {  # <- foreginKey #1
        productId
        name
      }
      customer {  # <- foreginKey #2
        customerId
        name
        email
      }
      orderId
      order {  # <- foreginKey #3
        amount
      }
    }
  }
}

I tried these, and faced warning.

foreignKey: '(product_id) references product (product_id), (customer_id) references customer (customer_id), (order_id) references order (order_id)'
foreignKey: [
  '(product_id) references product (product_id)',
  '(customer_id) references customer (customer_id)',
  '(order_id) references order (order_id)'
]

How can I deal with it?

Additional context

  • NodeJS: 14
  • Postgrahile: v4.12.0-alpha.0
❔ question

All 3 comments

The latter is correct. Maybe share the warning and I can give guidance.

@benjie I checked again, and latter works fine.

https://www.graphile.org/postgraphile/smart-tags/#foreignkey

The documentation should be updated, because both string and string[] can be accepted.

class: {
  my_materialized_view: {
    tags: {
      foreignKey: "(key_1, key_2) references other_table (key_1, key_2)"
    }
  }
}

// or

class: {
  my_materialized_view: {
    tags: {
      foreignKey: [
        "(key_1, key_2) references other_table (key_1, key_2)",
        "(key_3, key_4) references more_table (key_3, key_4)"
      ]
    }
  }
}

And more, is it possible to specify multiple @foreignKey in database comment like this?

comment on materialized view my_materialized_view is E'@foreignKey (key_1, key_2) references other_table (key_1, key_2)|@foreignKey (key_3, key_4) references more_table (key_3, key_4)';

Fixes to the docs are very welcome! Rather than | it’s \n as detailed in the spec: https://www.graphile.org/postgraphile/smart-comments/#smart-comment-spec

Was this page helpful?
0 / 5 - 0 ratings

Related issues

james-ff picture james-ff  ·  4Comments

ssomnoremac picture ssomnoremac  ·  5Comments

tazsingh picture tazsingh  ·  3Comments

outsidenote picture outsidenote  ·  4Comments

Venryx picture Venryx  ·  4Comments