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

Multiple Tables (Unions) #47

Closed
matthiasg opened this issue Dec 13, 2016 · 4 comments
Closed

Multiple Tables (Unions) #47

matthiasg opened this issue Dec 13, 2016 · 4 comments

Comments

@matthiasg
Copy link

We have a data schema where a table X can reference either table Y1 or Y2. In GQL the resolve function thus resolves either Y1 or Y2 which we put into a union Y. In that case the query function would have to execute a query on Y1 and Y2 the fields in Y1 and Y2 are a little different (1 and 2 are respective major versions of the tables. eg. one might have a field the other might not or it might be of an incompatible type).

Is something like this expressible to join monster ?

@acarl005
Copy link
Collaborator

Unfortunately there is no support for union types right now. We avoided this feature since there really is no obvious way to automatically union two types with different tables. However, the need has arisen.

What we envision is creating a SQL view which unions the two tables, and telling the GraphQL union type to query that view.

const X = new GraphQLUnionType({
  types: [ Y1, Y2 ],
  resolveType: fn() {...},
  sqlView: 'Y'
})
CREATE VIEW Y AS
SELECT column_name(s) FROM Y1
UNION ALL
SELECT column_name(s) FROM Y2;

Would this work for your use case? Could you elaborate on the use case a bit more? We're curious why you're using GraphQL union types.

@matthiasg
Copy link
Author

A view should only be necessary when there is a need to sort those. In all other cases simply running two queries and merging the results into the same list is quite sufficient. A view might be more idiomatic with respect to SQL but that would also require a potentially high number of columns in the view since the types might not have overlapping fields. One of our cases for example, is a 'children field which is resolved using a shared field parent which is an id linking an item to a parent. The id is shared, the rest is not.

If this is not required by other users I think any work done here should be as minimal as possible though just to get the idea out and validated.

@acarl005
Copy link
Collaborator

Yeah, creating views that union very disjoint tables is ugly.

For now the best way to handle it is a separate, hand-written query in your resolver.

@acarl005
Copy link
Collaborator

Union and Interface types are now possible. It's still not easy to do with incongruous tables, but it's possible. Certainly open to feedback. Feel free to open new issue with suggestions. docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants