Unions and Interfaces

Unions and interfaces are abstract GraphQL types that enable a schema field to return one of multiple object types.

1. Union Types

Neo4j GraphQL supports the use of Unions on a @relationship field.

1.1. Example

The following schema defines a User type, that has a relationship(HAS_CONTENT), of type [Content]. Content is of type union representing either Blog or Post.

union Content = Blog | Post

type Blog {
  title: String
  posts: [Post] @relationship(type: "HAS_POST", direction: OUT)
}

type Post {
  content: String
}

type User {
  name: String
  content: [Content] @relationship(type: "HAS_CONTENT", direction: OUT)
}

1.2. Querying a union

The below query gets the user and their content;

query GetUsersWithContent {
  users {
    name
    content {
        ... on Blog {
            title
            posts {
                content
            }
        }
        ... on Post {
            content
        }
    }
  }
}

1.3. Creating a union

The below mutation creates the user and their content;

mutation CreateUserAndContent {
  createUsers(
    input: [
      {
        name: "Dan"
        content_Blog: {
          create: [
            {
              title: "My Cool Blog"
              posts: { create: [{ content: "My Cool Post" }] }
            }
          ]
        }
      }
    ]
  ) {
    users {
      name
    }
  }
}

2. Interface Types

Using interface types will give you no real database support therefor no; query, update, delete, filter support. But instead used as a language feature to safeguard your schema. Great for when dealing with repetitive or large schemas you can essentially put "The side railings up".