How can I do polymorphic relationships? Say I have...
# prisma-whats-new
p
How can I do polymorphic relationships? Say I have a
Product
type. And then I have
Book
,
Toy
,
Food
, etc, and I need to associate those with
Product
.
Apparently GraphQL supports polymorphic types
Copy code
searchMedia(name: "abc") {
  ... on Photo {
   height
   width
  }
  ... on Video {
    height
    width
    duration 
  }
  ... on Audio {
    duration 
    key
  }
}
but how can I define those in Graphcool?
@nilan?
n
Hey @pier 👋 Right now you would create multiple relations between those types, resulting in "sparse" data. Once we added support for union types and interfaces, this can be handled much more elegantly: https://github.com/graphcool/feature-requests/issues/165
p
ok, but how would I query this?
for example if I have a list of products with different relations to the different product types
and I need to filter for one of the attributes of a product type
n
Can you give an example? Do you mean you want to query all products that have
height > 10
?
p
correct, but only one type of product has height
or not all types of products
n
yea and you don't know "beforehand" which types do have this property, right?
p
hm
yes let's say I don't know
n
This situation is indeed quite tricky to model right now without interfaces and unions
p
ok
and what if I do know which types have height?
n
However, I'd also say that this is a typical use case for a search provider such as Algolia
so you could use the Algolia integration to sync all the product data to Algolia and use their API to filter the products
if you know which types have the
height
, you can manually build the query to look like this
p
hm
that must be a pretty long query? 😄
n
Copy code
query {
  allProducts(filter: {
    OR: [{video: { height_gt: 10 } }, { photo: { height_gt: 10 } }] 
  }) {
    video {
      id
    }
    photo {
      id
    }
  }
}
sorry, was quickly heading out 😛
p
ok thanks!