-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
Interfaces/Union #198
Comments
so sorry for the delay getting back to you! that isn't usual, I had notifications misconfigured There will likely be a as far as I can tell that is consistent with similar "reflect graphql from db" projects but if you're aware of any prior art e.g. from Graphile, Hasura etc, for how they do it I'd love to take a look! |
@olirice Just wanted to bring this up again, but it would be really useful to generate type unions based on PostgreSQL's table inheritance. Assuming I have a type Employee implements Person {
salary: Int!
} which would allow a query returning |
I would love to contribute to the project @olirice, but could use some help understanding where I should look with regards to the PSQL -> GraphQL type generation, and the GraphQL -> PSQL query transformation. |
using table inheritance for type unions is a very interesting idea @bryanmylee table inheritance is pretty out-of-fashion and I recall being told that its no longer recommended. I'll ask the postgres team and see if they have any strong opinions There could be some edge cases around computed columns and views not being able to conform to the interface any thoughts @alaister?
Contributions to the project would be great. This feature is pretty significant though so possibly not the best place to start: These would be the steps:
I don't think any of the resolver logic would need to change off the top of my head since all the relevant fields are already present on the inherited types |
Interfaces + Unions (Polymorphism) would certainly be a useful feature addition. However, I think inherited tables are not the best way of implementing them. Personally, I don't find them to be very ergonomic. Perhaps we could hint to pg_graphql (via the |
I've only started using PostgreSQL extensively recently so I'm curious: what seems to be the issue with inherited tables? |
IIRC its a bunch of little things
but if you know what you're doing I don't know of anything that's really wrong with inheritance in pg |
Just as an update for others who have the same problem to solve, our team has gotten rid of table inheritance in favor of composed tables due to some glaring issues with inheritance. The most egregious behavior was that entries in child tables will show up in queries on the parent table, but those entries will not be referencable in foreign keys, which broke our use case. create table user (
id uuid not null default gen_random_uuid (),
constraint user_pkey primary key (id)
);
create table employee (
name text not null
) inherits (user);
create table member (
user_id uuid not null,
constraint member_user_id_fk foreign key (user_id)
references user (id) on delete cascade
);
insert into employee(id, name)
values ('b28e7d43-d46f-4d20-8a06-db6342569553', 'Bryan');
select * from user; /**
+--------------------------------------+
| id |
+--------------------------------------+
| b28e7d43-d46f-4d20-8a06-db6342569553 |
+--------------------------------------+
*/
insert into member(user_id)
values ('b28e7d43-d46f-4d20-8a06-db6342569553'); /**
'b28e7d43-d46f-4d20-8a06-db6342569553' does not exist in foreign table user.
*/ Composition resolves all our use cases, and is compatible with create table user (
id uuid not null default gen_random_uuid (),
constraint user_pkey primary key (id)
);
create table employee (
id uuid not null default gen_random_uuid (),
constraint employee_pkey primary key (id),
-- the unique constraint allows pg_graphql to create a 1-to-1
user_id uuid not null unique,
constraint employee_user_id_fk foreign key (user_id)
references user (id) on delete cascade,
name text not null
) inherits (user);
create table member (
user_id uuid not null,
constraint member_user_id_fk foreign key (user_id)
references user (id) on delete cascade
);
insert into user(id)
values ('b28e7d43-d46f-4d20-8a06-db6342569553');
insert into employee(id, user_id, name)
values (
'b28e7d43-d46f-4d20-8a06-db6342569553',
'b28e7d43-d46f-4d20-8a06-db6342569553',
'Bryan'
);
-- works fine now.
insert into member(user_id)
values ('b28e7d43-d46f-4d20-8a06-db6342569553'); query {
userCollection {
edges {
node {
employee { # any extra child fields from employees
name
}
}
}
}
} One small rough edge is that we have to insert records into two tables whenever we want to create new entries, but that's something that can be worked around, and #294 will solve the pain points eventually. |
Hi, how do I need to create the tables to generate my interfaces and unions, or how do I edit the scheme file so I can implement this myself?
The text was updated successfully, but these errors were encountered: