Union Types in GraphQL Schema

Union type in a GraphQL Schema is declared using the union keyword and the pipe symbol (|), and it suggests that a field can have more than one type. Let’s take a look at the example below –

type Post {
text: String!
title: String!
}
type Author {
name: String!
}
union Feed = Post | Author

Here, Feed is a union type of Post and Author, implying that Feed can be either of type Post or of type Author.

Now, let’s create a basic Schema with a union type, and create our own resolver to see it in action.

Union Types in GraphQL Schema

GraphQL is an open-source Query Language for APIs that allows us to query only the data we require, unlike the REST framework which returns the entire data as is configured in its API. With GraphQL, we can maintain a Schema that defines what data we can query from the server, and we can define a proper contractual schema like this between the client and the server. In this article, we will learn about the Union Types in GraphQL Schema, and implement our resolver for it in Node.JS.

Similar Reads

Union Types in GraphQL Schema

Union type in a GraphQL Schema is declared using the union keyword and the pipe symbol (|), and it suggests that a field can have more than one type. Let’s take a look at the example below –...

Using Union Types in Queries

Step 1: Define the Schema...

Conclusion

...

Contact Us