Confusion Around Extending Entities in Federation 2

I’m relatively new to federation, and after reading through the docs, looking at examples, and creating a POC, I’m a bit confused on what the current state of extending a type defined in one subgraph in another subgraph is. From the docs, I only see extending types referenced in the removing unnecessary syntax Moving to Apollo Federation 2 - Apollo GraphQL Docs section for moving from version 1 to version 2 of federation. In the federation 2 docs on entities, it is stated

Every subgraph that contributes at least one unique field to an entity must define a reference resolver for that entity.

From this info, it led me to believe that extending a type in another subgraph was something utilized in Federation 1 but not necessary in Federation 2. However, the restriction that you have to define a reference resolver for the type now seems odd to me. If each subgraph is an independent service with its own data store, one subgraph can’t load the base entity out of the other service’s data store. However, that service does have related info that makes sense to expose on that type as a field.

As an example, the Product and Review graph that is used in a lot of federation examples, if you have product and reviews services/subgraphs with their own data stores, it makes sense for the Product subgraph to defined a Product type and a Review subgraph to define a reviews field on the Product type. But it’s not obvious to me how the reviews subgraph would define a reference resolver for Product, which seems to point towards extending the type rather than re-declearing it and needing to define a reference resolver`1.

As another note, I’ve been using pothos as my schema builder. In their federation plugin examples, they mention extending external entities, which ends up using the @extends directive. This is actually how I originally tracked down that extending types between subgraphs even worked.

Anyways, I’m basically just trying to determine if extending entities is still the norm for handling this sort of situation and if it’s not, how do you deal with the reference resolver requirement in a subgraph that doesn’t have access to where the entities backing data is stored?

Hello :wave:

Extending entities is a core feature of Federation (both v1 and v2). Fed v2 simplified some of the logic around it so you no longer need to use extend type/@extends syntax to mark your “extended” entities.

Reference resolvers are the entities entry point to a given subgraph, i.e. in order to extend given entity with new functionality, we need to be able to resolve entity to some object. In the reviews example, the simplest Product entity resolver would just instantiate Product object with provided keys (in general entity should also verify whether provided key is a valid one, i.e. can be resolved to an object).

Take a look at the entities (basics) and entities (advanced) section of the docs for more details.


1 Like