Introduction
The@requires federation directive declares that a field’s resolver depends on values from fields resolved by another subgraph. Cosmo Connect supports @requires for gRPC-backed entities, enabling you to implement computed fields that span multiple subgraphs with full type safety and automatic batching.
If you’re not familiar with how @requires works in federation generally, read the @requires directive documentation first.
How @requires Works in Cosmo Connect
When a field on a gRPC entity is annotated with@requires, Cosmo Connect generates a dedicated RPC method for that field, separate from the entity’s regular lookup. This mirrors the approach used for field resolvers, where each resolution concern gets its own type-safe RPC.
Take the following schema as an example:
tags is resolved by another subgraph and marked @external. The tagSummary field is resolved by this subgraph, but only after tags has been fetched from the other subgraph.
Generated RPC
Cosmo Connect generates a dedicated method for the requiring field alongside the entity’s regular lookup:Require{TypeName}{FieldName}By{KeyFieldName}.
Request and Response Structure
The generated protobuf messages carry everything the resolver needs: the entity key (so you can identify which entity you’re computing for) and the resolved values of the required external fields.Request
key is passed through automatically from the parent entity’s @key directive, giving you a stable identifier to use in your business logic. The fields message contains the already-resolved values of the @external fields you declared in @requires. In this example, fields.tags will already be populated with the tag list fetched from the subgraph that resolves them before your RPC is called.
Response
Batching
@requires RPCs are batched by default. When multiple entities need their requiring field resolved in a single GraphQL operation, Cosmo Connect aggregates all of them, fetches the @external field values from the subgraph that resolves them for all entities at once, and issues a single RPC call to your service with all context elements in one request.
This eliminates the N+1 query problem: no matter how many Storage entities are returned in a list, there is always exactly one RequireStorageTagSummaryById call per operation.
Response Ordering
Always iterate overreq.context in order and append one result per element, even for entities where the field resolves to a zero value or nil:
Runtime Execution Flow
Entity lookup
The router resolves the entity via its regular lookup RPC (e.g.,
LookupStorageById), fetching its locally-resolved fields.External field fetch
The router fetches the
@external fields (tags) from the subgraph that resolves them.Require RPC call
With both the entity keys and external field values in hand, the router issues the batched
RequireStorageTagSummaryById RPC to your service.Limitations
See Also
- @requires directive — Federation concept documentation
- @external directive — Marking fields as resolved by another subgraph
- GraphQL Support — Supported features and limitations overview