federation
federation copied to clipboard
@provides with unions
Hi, trying to make @provides with unions work, but it doesn't. This is the schema:
In Users
service:
type User @key(fields: "id") {
id: ID!
name: String! @shareable
age: Int!
}
In Companies
service:
type Query {
companies: [CompanyResponse!]! @provides(fields: "items { <WHAT TO TYPE HERE> } ")
}
type User @key(fields: "id", resolvable: false) {
id: ID!
name: String! @external
}
type Company {
id: ID!
name: String!
}
union CompanyOrUser = Company | User
type CompanyResponse {
items: CompanyOrUser!
}
Question is what should we type in the @provides
? We tried several things, e.g items { ...on User { name } }
neither of them worked and resulted a composition error that because name
field marked as external but not being in use (cuz federation doesn't recognize it) in @provides.
HELP?
Answer from discord:
Using the @provides directive in this way would not be possible, as even though the union type technically have the same fields, there is no guarantee that they do. Because they may not have the same fields, there is a possibility of having an error case, so composition does not allow this.
I'm not 100% sure if it would pass composition, but you could try to make an interface object with the common field between the two types. I'm not sure this would work with @provides either, as you still can only specify one of the type's in provides. https://www.apollographql.com/docs/apollo-server/schema/unions-interfaces#interface-type
neither of them worked and resulted a composition error that because name field marked as external but not being in use (cuz federation doesn't recognize it) in @provides.
What version of federation are you using?
I just tested current main
with the very example of the description, using:
companies: [CompanyResponse!]! @provides(fields: "items { ... on User { name } } ")
and this work as expected (it composes, and the plan does understand and use the @provides
).