apollo – How can I aggregate fields using a custom directive in Federated GraphQL at the gateway layer?

I have cross-cutting concern across my graph, where I need to aggregate fields at my gateway layer, versus at my subgraphs. I must tag fields of interest in all of my subgraphs with a custom directive @aggregateme to make it easy for my peers to participate in my behavior. @tag(name: "aggregateme") would be equally acceptable. When my Apollo Server/gateway either resolves each field with the @aggregateme directive on it, or, on willSendResponse, I need to collect the the path.to.the.field and the value for the field for all fields in the response.

However, problems.

  1. Although buildSubgraphSchema shows my custom directive at my subgraph, my composed federated schema using ApolloGateway({ supergraphSdl: ...subgraphDatas }) drops those directives. @tag is claimed to be included in the supergraph schema, yet, I still cannot see that directive in my supergraph’s schema when inspecting the in-mem GraphQL schema instance. I also see composeDirective, but suspect that’s only in graphos, not the javascript gateway.
  2. executionDidStart / willResolveField don’t seem to run in the gateway.

How would you recommend applying a centralized aggregation, based on custom directives, in the ApolloGateway?

I’m using all of the latest stuff:

"@apollo/gateway": "2.9.3",
"@apollo/server": "4.11.2",
"@apollo/subgraph": "^2.9.3",
"@graphql-tools/schema": "10.0.11",
"@graphql-tools/utils": "10.6.2",

Read more here: Source link