Union types

Considering such query:

{
  search(keyword: "car") {
    ... on Product {
      name
      price
    }
    ... on Category {
      name
      itemsCount
      avgProductPrice
    }
  }
}

Search field return union of Product and Category. To define such union we need to use @Union decorator

import { ObjectType, Field, Union } from 'typegql';

@ObjectType()
class Product {
  @Field() name: string;
  @Field() price: number;
}

@ObjectType()
class Category {
  @Field() name: string;
  @Field() itemsCount: number;
  @Field() avgProductPrice: number;
}

@Union({ types: [Product, Category] })
class SearchResult {}

results matching ""

    No results matching ""