question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Support graphql union types

See original GitHub issue

Currently the only method to do query multiple objects in the same time is to define an custom sql function:

CREATE OR REPLACE FUNCTION search(text TEXT)
RETURNS SETOF search_trigger LANGUAGE sql STABLE AS $$
SELECT id, name,height, NULL AS primary_function, NULL AS length, 'human' AS type FROM humans
WHERE (...)
UNION ALL
SELECT id, name, NULL AS height, primary_function, NULL AS length, 'droid' AS type FROM droids
WHERE (...)
UNION ALL
SELECT id, name, NULL AS height, NULL AS primary_function, length,  'starship' AS type FROM starships
WHERE (...)
$$

while in the front end we query the search_trigger

query {
  search(text: "an") {
    __typename
    id
    name
    height
    primary_function
    length
    type
  }
}

While this should work, we have another type search_trigger instead of article and user in the front end, which can cause many inconveniences, e.g. write new interface and types, inconsistency in local store, etc. It will be good to have support for union type so that we can query with:

query {
  search(text: "an") {
    __typename
    ... on Human {
      name
      height
    }
    ... on Droid {
      name
      primaryFunction
    }
    ... on Starship {
      name
      length
    }
  }
}

Database schema in the examples is borrowed from https://graphql.org/learn/schema/#union-types.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:105
  • Comments:13

github_iconTop GitHub Comments

10reactions
Svartocommented, Dec 21, 2020

+1, Ran into this limitation as well, would love union type support to be added to Hasura.

3reactions
studiouscommented, Aug 20, 2021

I was just beginning to lean into Union types heavily in our Rails portion of the graph only to find out Hasura is lacking this support given how coupled it is to the database as source of truth. Union types and Interfaces have started becoming rather helpful. I’d love to see something announced as our plan is to move even more to Hasura.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unions and interfaces - Apollo GraphQL Docs
Unions and interfaces are abstract GraphQL types that enable a schema field to return one of multiple object types. Union type.
Read more >
GraphQL union/interface ETA? - Fauna Forums
I'm planning a project that will be leveraging FaunaDB + GraphQL API (technology that has me VERY excited!), but find the lack of...
Read more >
Doing More With GraphQL: Interfaces and Unions - Dgraph Blog
Learn how to use GraphQL's interface and union types. ... like a new feature or requirements to support new types of requests from...
Read more >
Getting the Best of TypeScript and GraphQL: Union Types
GraphQL is a query language that is used to define the API of a server. Like TypeScript or even Go language, GraphQL supports...
Read more >
GraphQL Unions – Customize your results!
Unions are abstract GraphQL types that enable a schema field to return one of multiple object types. Not really sure what that means?...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found