fix(@nestjs/apollo): expose schema transform hook on gateway driver#3969
Merged
kamilmysliwiec merged 1 commit intoMay 1, 2026
Conversation
ApolloGatewayDriverConfig had no entry point for transforming the composed supergraph schema. Options placed on `gateway` were forwarded verbatim to `new ApolloGateway()`, which silently ignores fields it does not recognize, while `server` explicitly omits `transformSchema`. Apollo Server consumes the gateway schema via the synchronous `onSchemaLoadOrUpdate` event fired during `gateway.load()`, so the transform must be applied before that event reaches the server. Add an optional `transformSchema` callback at the gateway driver config level. When provided, the driver wraps the gateway with a small adapter that queues Apollo Server's listener, awaits the user transform during `load()`, and emits the transformed schema before returning. When omitted, the gateway is passed through unchanged. Closes nestjs#3296
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #3296
User-supplied schema transforms are silently dropped when using
ApolloGatewayDriver.transformSchemaon thegatewayoption does nothing — those options are forwarded verbatim tonew ApolloGateway(), which silently ignores fields it does not recognize.transformSchemaon theserveroption is explicitly disallowed — the type definitionOmitstransformSchemafrom the gateway server config.The result is that there is no supported way to apply custom directives or otherwise transform the composed supergraph schema in a federated setup. Apollo Server consumes the gateway schema via the synchronous
gateway.onSchemaLoadOrUpdateevent fired duringgateway.load(), so any transform has to be in place before that event reaches the server — which the current driver never does.What is the new behavior?
Adds an optional
transformSchemacallback at the top level ofApolloGatewayDriverConfig(sibling ofgatewayandserver):When provided, the driver wraps the underlying
ApolloGatewaywith a small adapter that:onSchemaLoadOrUpdatelistener instead of registering it directly on the real gateway.transformSchemaduringgateway.load().load()returns, so Apollo Server only ever sees the transformed version.When
transformSchemais omitted, the gateway is passed through unchanged — zero behavioral cost for existing users.Documented limitation: post-startup gateway updates (e.g. supergraph polling) are forwarded untransformed; the JSDoc on the new option calls this out. The primary use case (registering custom directives at startup) is fully covered.
Tests: new e2e spec
packages/apollo/tests/e2e/graphql-gateway-transform-schema.spec.tscovers two cases:transformSchemais invoked exactly once during gateway startup.transformSchemastill serves federated queries correctly.Does this PR introduce a breaking change?
Additive, optional field with a backward-compatible default. Existing configurations behave identically.
Other information
Files touched:
packages/apollo/lib/interfaces/apollo-gateway-driver-config.interface.ts— new optionaltransformSchemafield on the driver config.packages/apollo/lib/drivers/apollo-gateway.driver.ts— wraps the gateway with the transform adapter when the option is provided; passes through otherwise.packages/apollo/tests/e2e/graphql-gateway-transform-schema.spec.ts— regression coverage.Closes #3296