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

Feature Request: map columns with the same prefix to nested objects #1050

Closed
1 of 3 tasks
marshall007 opened this issue Apr 16, 2019 · 5 comments
Closed
1 of 3 tasks

Comments

@marshall007
Copy link
Contributor

marshall007 commented Apr 16, 2019

I'm submitting a ...

  • bug report
  • feature request
  • question

PostGraphile version: v4.3.3

Expected behavior:

Currently, using custom composite types and separate tables are the only way to expose nested objects in your generated GraphQL schema. It would be nice if there were an easy way to map columns named with the same prefix to nested properties on a single object. For example, assuming I have several columns with the prefix products.inventory_*, I'd like to be able to map those under a single object Product.inventory.* in the GQL schema.

This could be implemented on top of the existing @name smart comment, extending the current implementation to support nested paths i.e.

comment on column products.inventory_method is
  E'@name inventory.method';

However, in addition to explicit mappings, a more dynamic option (where the user can specify a path prefix) is probably desirable to avoid unnecessary verbosity:

-- @nested <prefix> <name?>

-- default `name` in this case would be `inventory`
comment on table products is
  E'@nested inventory_*';

comment on table products is
  E'@nested inventory_* inventory';
@benjie
Copy link
Member

benjie commented Apr 18, 2019

If we implement this, I think it should also allow arbitrary groupings. In addition to the glob pattern, we could also allow something like:

@nested street_address,city,country,postcode address

This would combine with the standard @name smart comment in case you want to, e.g., rename postcode to zipcode.

We could potentially implement this in a backwards compatible way by deprecating the existing fields and adding the nested object too. This could be an option.

@RedShift1
Copy link

RedShift1 commented Dec 29, 2019

For querying, you can already do this using a computed column:

CREATE TYPE nested_inventory AS
(
	warehouse text,
	location text
);
CREATE FUNCTION products_inventory(products products) RETURNS nested_inventory AS
$$
	SELECT products.warehouse::text, products.location::text
$$ LANGUAGE sql STABLE;

That doesn't solve it for mutations though, but perhaps this is already sufficient for your use case.

@benjie benjie added the 🔁 revisit-in-v5 Not happy with this behaviour, but changing it is a breaking change. label Jan 7, 2020
@benjie
Copy link
Member

benjie commented Sep 28, 2020

@benjie
Copy link
Member

benjie commented Jul 21, 2023

I just built a plugin for this in V5: https://gist.github.com/benjie/b9f3b6d46db0b6ae2524a6c9fd15fb9a

@benjie
Copy link
Member

benjie commented Jul 21, 2023

@benjie benjie added 🔮 fixed-in-v5 and removed 🔁 revisit-in-v5 Not happy with this behaviour, but changing it is a breaking change. labels Sep 29, 2023
@benjie benjie closed this as completed Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants