Union Type - A type value of multiple types

I am building a schema in which one node can have one edge to one other node which can be of different types.

I wonder how should I specify this in the type system.

Does GraphQL± support Union Types?

Would this work?

type Person {
    name: string
    works_for: [Person | Company]
}

type Company {
    name: string
}

Or maybe I should leave it as a generic uid value?

type Person {
    name: string
    works_for: [uid]
}

But then, I loose the expand(_all_) functionality when querying, isn’t?

Nope, We don’t have a convention about the Type Schema. And this is in the very early stages, so still have things to come. Open an issue if you find it necessary.

leave like this. If it don’t work, open a bug issue.

As far I know you only loose the expand all functionality if the mutation itself doesn’t have the <dgraph.type> specified and also doesn’t have a related type in the Type Schema.

1 Like

@Pepo we are currently building out a native support for GraphQL in Dgraph. That will have union types.

2 Likes

Hello there, @michaelcompton,
do you know if a plan exist for supporting the Union type with Dgraph Graphql ?

Here is the response I got from the dgraph server when trying to give a schema with an union type :

"message": "couldn't rewrite mutation updateGQLSchema because input:1: You can't add union definitions. Only type, interface and enums are allowed in initial schema.\n",
      "extensions": {
        "code": "Error"
      }

dgraph version: v20.03.0

ps: I had to add space after the union type definition in the schema otherwise it trow a parsing error.

Hey @dtrckd, please always create a new topic if you have questions. Even if it is slightly similar to that topic.

If you ask questions on other users’ topics you can confuse the topics and also everyone involved receives emails. And it can be annoying.

Cheers.

Ok @MichelDiz, thanks for the tips, I’ll take care of that next time.

1 Like

We can’t use Apollo Federation with Dgraph GraphQL if Dgraph doesn’t support Union types alongside standard GraphQL :frowning: hopefully native support for GraphQL makes its way into release soon.

Edit: Apparently support for Unions is slated for next month.

Hi @geoyws @dtrckd @Pepo,
Please have a look at the RFC for Union Types. Hoping to get some feedback.

Thanks

4 Likes