Tags an implementing object with the value its parent interface’s @discriminate(on:) column must hold for a row to belong to this subtype. Pairs with @discriminate for single-table polymorphism. Without @discriminate on the interface there is nothing for the value to match against; without @discriminator on each implementer the row mapper has no way to assign rows.
SDL signature
directive @discriminator(value: String!) on OBJECT
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
|
|
(required) |
The literal database value of the discriminator column that places a row in this subtype. Compared verbatim against the column at row-mapping time; case-sensitive. |
Canonical example
From the example schema’s Content family:
interface Content @table(name: "content") @discriminate(on: "CONTENT_TYPE") {
contentId: Int! @field(name: "CONTENT_ID")
title: String! @field(name: "TITLE")
}
type FilmContent implements Content @table(name: "content") @discriminator(value: "FILM") {
contentId: Int! @field(name: "CONTENT_ID")
title: String! @field(name: "TITLE")
length: Int @field(name: "LENGTH")
rating: MpaaRating @reference(path: [{key: "content_film_id_fkey"}]) @field(name: "RATING")
}
type ShortContent implements Content @table(name: "content") @discriminator(value: "SHORT") {
contentId: Int! @field(name: "CONTENT_ID")
title: String! @field(name: "TITLE")
description: String @field(name: "SHORT_DESCRIPTION")
}
Each implementer also carries @table(name: "content") (the same table as the interface) and @discriminator(value: …). At row-mapping time the fetcher reads content_type; rows where content_type = 'FILM' map to FilmContent, rows where content_type = 'SHORT' map to ShortContent. Rows with any other discriminator value fail to map and surface as a runtime error.
Constraints
-
@discriminatoris only meaningful on objects that implement an interface decorated with@discriminate. The build rejects it on objects whose implemented interfaces have no discriminator column. -
The implementer’s
@tablemust match the interface’s@table. Mixing tables breaks single-table polymorphism. -
valueis matched verbatim against the database column; quoting and case must match the stored value exactly. -
Each implementer of a single
@discriminateinterface should declare a distinctvalue. Two implementers sharing the same value collapse to one branch and the second is unreachable. -
The directive applies only to
OBJECT; interfaces and unions cannot carry@discriminator.
See also
-
@discriminateis the interface-side counterpart that names the column. -
@tableestablishes the shared table. -
How-to: Polymorphic types covers single-table vs multi-table layouts and discriminator-value pitfalls.