federation
federation copied to clipboard
Allow putting `@key` on a field for the common single-key-of-single-field case
Currently, defining a key for an entity type is done with:
type User @key(fields: "id") {
id: ID!
x: Int
y: Int
}
and this syntax certainly afford plenty of flexibility, in particular:
- it allows multiple separate keys.
- it allows compound keys, composed of more than on field, including nested ones.
However, the most common case is by far that of a single key on a single field. Some experimental sampling of real world data I did suggests that it is over 90% of cases.
As such, the proposal of this ticket is to introduce a convenience to simplify this most common case and to allow writing the example above more simply as:
type User {
id: ID! @key
x: Int
y: Int
}
That is, to allow a @key
without arguments directly on the one field that should be the key.
Technically, this would mean:
- changing the
@key
definition so that 1) thefields
argument becomes optional and 2) it is allowed onFIELD_DEFINITION
locations. - add additional validation around
@key
usages so that:- the argument remains mandatory anywhere except when used on fields.
- the argument is not accepted when used on fields.
-
@key
on field is only accepted for fields of "leaf" types (and fields without arguments). - we cannot mix and match
@key
on fields and type for the same type. - we can use
@key
on field only on a single field of a type (or not; we could allow it on multiple fields as a way to declare multiple single-field keys, but happy to disallow it initially to avoid confusion). - whatever else I'm forgetting right now.
I reckon that this suggestion does not introduce anything you couldn't do before, and as such gives 2 ways to do the same thing, and it does require a bit of new validation to ensure we don't introduce confusing cases, but overall, it feels like an intuitive shortcut and save typing for the most common use of @key
, so it feels way possibly worth.
Open to feedback, but as I would personally definitively use and enjoy such convenience, figured it was worth a ticket.