Marks a field of a connection’s filter input as a facet. The generated Connection type gains a facets: <ConnName>Facets field with one entry per marked filter field, each returning [<Scalar>FacetValue!] (a nullable list of non-null entries) where every entry carries a value and a count.
Each facet’s counts apply the connection’s full filter minus that facet’s own predicate. When a user has filtered rating: [PG], the rating facet still shows counts for all ratings (so the user can pivot their selection), while every other facet shows counts for the rows matching rating = PG. The paginated edges / nodes query is unaffected and applies the full filter unchanged.
value mirrors the filter field’s element type exactly, same scalar and same nullability, so a client feeds facetValue.value straight back into the filter (filter: { rating: [facetValue.value] }) with no coercion. A nullable filter element preserves the NULL bucket as its own group; a non-null element scrubs it with IS NOT NULL.
SDL signature
directive @asFacet on INPUT_FIELD_DEFINITION
Canonical example
type Query {
films(filter: FilmFilter): [Film!]! @asConnection @defaultOrder(primaryKey: true)
}
input FilmFilter {
rating: [MpaaRating!] @field(name: "RATING") @asFacet
length: [Int!] @field(name: "LENGTH") @asFacet
title: String @field(name: "TITLE")
}
Graphitron expands the Connection with:
type QueryFilmsConnectionFacets {
rating: [MpaaRatingFacetValue!]
length: [IntFacetValue!]
}
type MpaaRatingFacetValue { value: MpaaRating! count: Int! }
type IntFacetValue { value: Int! count: Int! }
A query selecting any facet field issues exactly one extra SQL statement per request: a UNION ALL of per-facet GROUP BY arms, one arm per selected facet, each under its filter-minus-self predicate. When no facet field is selected, the aggregate is skipped entirely. Each facet’s entries are ordered count descending, then within equal counts by the decoded value’s natural order (integers numerically, enums in declaration order), a preserved NULL bucket sorting after non-null values.
Facets over a related table
A facet field may reach its column through @reference, the same way a filter field does. The counts group on the column at the end of the path and count rows of the connection, not rows of the joined table:
input CityFacetFilter {
country: [String!] @reference(path: [{table: "country"}]) @field(name: "country") @asFacet
district: [String!] @reference(path: [{table: "address"}]) @field(name: "district") @asFacet
}
district counts cities, not addresses: a city with two addresses in the same district counts once in that district’s bucket. A city with addresses in two districts counts once in each, so the counts of a facet whose path fans out can add up to more than the connection’s totalCount; each count is still exactly the number of cities filter: { district: [value] } returns.
A row with no related row at the end of the path appears in no bucket, just as no filter value selects it. A related row whose column is NULL follows the nullable-element rule above.
Every path shape a @reference filter field accepts is served: one hop or several, a junction table, a path that fans out, and hops that join or narrow with condition:.
Failure semantics
Facets are a best-effort aggregate, never a structural guarantee. The facets object and each per-facet field are nullable, so a facet query failure or timeout degrades to null (plus an entry in the GraphQL errors array) without propagating through non-null bubbling to the connection or the request; the page of results still returns.
Constraints
-
Valid only on fields of an input type used as the filter input of at least one field returning a connection; an input type no such field consumes is
FACET_SURFACED_NOWHERE(the facet’s counts would surface nowhere). Facets are generated only for@asConnection-bearing fields so far: an input consumed only by fields returning connection types the schema declares is valid, and is the deferralAUTHORED_CONNECTION_FACET_UNSUPPORTED. An input shared by connection and non-connection consumers is fine: the directive surfaces facets at the connection use sites and is inert at the others. -
The field must be bound to a column with
@field(name:), either on the connection’s own table or, with@reference(path:), on the table the path ends at.@condition/@nodeIdbindings,ID-typed fields, and input-object fields are rejected (node-ID and condition facets are a follow-up). -
A
@referencefacet counts distinct rows by the connection’s primary key, so it needs a connection over a table that has one. A@routine-sourced connection, or one over a table with no primary key, rejects a@referencefacet at build time. -
A
@referencefacet cannot sit under@condition(override: true)on the connection field or its filter argument: the override replaces the generated filter the facet’s join is taken from. -
The field must be nullable (optional): an always-active filter value could never show unfiltered pivot counts, and the generated filter-minus-self fragments suppress a facet’s own predicate by leaving it unset.
-
Facets are generated only on root
Queryconnections over a@table-backed object element. A faceted child connection or interface/union connection isFACETED_CONNECTION_UNSUPPORTED, a deferral the build stops on. -
Every field returning one connection type fills that type’s one facets record, so each must surface the same facets from its own filter arguments: the same names, value types and nullability, in the same order. The argument a facet rides in and the column it groups by are each field’s own; its count and facet queries run over its own arguments. Fields that disagree, or one surfacing no facets for a connection type that has them, are
CONNECTION_FACETS_DISAGREEat each of them. -
A facet’s name must be unique across the carrier’s whole filter surface (every filter input’s fields and every top-level argument): the facet fragments and the generated condition method key parameters by name, so any same-named sibling filter is rejected at build time.
-
Composes with the deprecated
@asConnection(connectionName:)override: the generator resolves facets per producing occurrence rather than by connection name, so an overridden connection name synthesises its facet surface (<ConnName>Facetsand the facet value types) under the overridden name. The override itself remains deprecated; see@asConnection. -
Facet synthesis applies to directive-driven
@asConnectioncarriers only for now; a hand-written structural Connection type does not gain afacetsfield yet, and one declaring its own isCONNECTION_FACETS_FIELD_DECLARED. Sharing a filter input between a served root carrier and a structural carrier is legal (the directive is inert at the structural consumer), and an input consumed only by structural connections is the deferralAUTHORED_CONNECTION_FACET_UNSUPPORTED: valid, its facets surfacing nowhere until graphitron generates them onto a declared connection type.
See also
-
@asConnectionowns the Connection synthesis the facets ride on. -
@fieldbinds the facet to its column. -
@referencereaches that column on a related table. -
@defaultOrderanchors the connection’s pagination; facets do not affect it.