Suppose that there are two subgraphs, sub1
and sub2
, defined for an Apollo Federation supergraph of version 2.0 or higher.
sub1
’s schema contains:
#...
type MyType @shareable {
idField: ID
myField: String
}
#...
sub2
’s schema contains:
#...
type MyType @key(fields:"idField") {
idField: ID
myField: String
}
#...
Is that a valid combination, or will it generate a schema composition error?
This is specific to Fed 2, but this is not a valid combination. @shareable
and @key
should not exist on the same type. They can be used for fields and other types, but not for Entities.
To understand more, see our What’s new in Federation 2 guide, but the short answer here is you need to first identity the type as an Entity using the @key
directive. If the type can not be identified by some id or combination of fields with @key
, then you should use @shareable
instead.
But in your case you do have a key. In Fed 2 the fields marked as keys are automatically considered sharedable, all other fields that are shared across subgraph definitions need to be marked as shareable
# Subgraph 1
type MyType @key(fields: "idField") {
idField: ID
myField: String @shareable
foo: String
}
# Subgraph 2
type MyType @key(fields: "idField") {
idField: ID
myField: String @shareable
bar: String
}