Union Types in Schema Via GraphQL API?

I am curious if the new GraphQL API supports union types? I could not seem to find it in the documentation here, but it seems like it has been mentioned before.

Here is an example of a way I’d like to specify the schema (if another strategy is better, please let me know!):

schema.graphql

type A {
    ...other fields
    bs_and_cs: [Thing]
}
 
type B {
   ...fields
}
 
type C {
    ...fields
} 

union Thing = B | C

If I have a schema like this (with more specific names of course), and try to post it to a locally running instance with: curl -X POST localhost:8080/admin/schema -d '@schema.graphql', the parser seems to have issues with the union type(s): couldn't rewrite mutation updateGQLSchema because input:1: Unexpected Name \"C\"\n"

Thank you!

It seems as though an interface type might work for a case where there are similar fields between B and C. Though, a union type might be more ideal.

interface Thing {
    similar_field: String
}

type A {
    ...other fields
    things: [Thing]
}
 
type B implements Thing {
   ...fields
}
 
type C implements Thing {
    ...fields
} 

Hey @ttlekich

Union types aren’t supported yet but you can expect support for it soon (sometime next month). Interface types are supported as you would have found out.

1 Like

@pawan Thank you so much! I figured they might be still being implemented/tested/etc., but thought I would ask just in case I missed something.

Are we able to use @hasInverse with unions?
@michaelcompton

type {
    favourites: [Things] @hasInverse(id)
}
union Things =  Food | Drink

type Food {
    id: String! @id 
}
type Drink {
    id: String! @id
}

thanks
Ming

1 Like

Hi,

As per discussion above, there’s no unions yet … and no decisions on exactly what will be supported when they arrive.

For interfaces, however, inverses and ids are supported both in the interface and in the implementing types.

Hi folks, just curious to know any updates on “union”. :thinking:
We are in the process of moving our infrastructure to dgraph and union is one of the last hurdles we have.
Thank you!

2 Likes

Hi @jana , Happy to hear that you are moving your infrastructure to Dgraph.
We will start working on UNION soon and it will be added in at max 6-8 weeks .

3 Likes

Anything on unions yet? Eagerly waiting for it.

Does this work? Is this tested? Can we have nodes of type Thing?

Please be patient, unions will arrive in a bit.

Yes, interfaces work. You can have the above schema. You can’t create nodes of interface Thing, but you can create nodes of type B and C. Once created, they can behave as nodes of interface Thing.

3 Likes

Hi @ttlekich @mingsterism @jana @pranaypratyush,
Please have a look at the RFC for Union Types. Hoping to get some feedback.

Thanks