-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Skip some supergraph validations in production by default #2657
Conversation
|
Name | Link |
---|---|
🔨 Latest commit | 2d543e90693c6066bebb2ea3b085d58f4742db2d |
🦋 Changeset detectedLatest commit: 387d2c5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 7 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
Note that this PR is currently on top of #2655 because it uses the refactor done in that PR to be able to pass the argument to disable validation cleanly. |
15ca882
to
2d543e9
Compare
When receiving a supergraph, we were always running all the graphQL validations on both the supergraph itslef, and on each of the subgraphs we extract from it. This is useful for us when doing dev, as it allows to get decent error message when we break the supergraph generation process or the subgraph extraction process, but this is of very limited value otherwise: even if we don't run those full validations, the supergraph still has to be valid graphQL syntax to parse in the first place, and the subgraph extraction will still throw if the supergraph is not essentially a valid supergraph (and note that the code checking that the `join` spec version is valid, or that the supergraph does not use non-supported feature for `SECURITY` or `EXECUTION` is *not* disabled by this). Besides, it's not like uplink will send basically-valid-but-slightly-corrupted supergraphs, and when using `IntrospectAndCompose` there isn't really room for the supergraph to be corrupted. And those validations are actually not super cheap, especially on large supergraphs, so running them feels wasteful. This commit thus disable those validation in production environment (when `NODE_ENV === "production"`) by default. It keeps those validation during dev mainly for 2 reasons: 1. as mentioned above, so that those validate are run when we run our own unit tests, to help catch/identify any subtle mistake we would introduce in the supergraph generation and extaction codes. 2. when debugging something, it can happens that one start a gateway from a statically provided supergraph SDL, SDL that one may have manually edited for some reason (testing something, playing with it, ...). In that case, the validation can provide nicer errors if the manual edits are invalid. The commit also introduce a `validateReceivedSupergraphs` in the gateway config to allows overriding the default behaviour described above.
2d543e9
to
5b39d5b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes lgtm. Maybe we should add some tests around the assumeValid
behavior? I think it would be fine to follow up with those later, just to preserve the behavior we expect going forward.
gateway-js/src/config.ts
Outdated
* for development (mostly to provide better error messages when provided with an incorrect | ||
* supergraph). | ||
*/ | ||
validateReceivedSupergraphs?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small naming nitpick, not a big deal though. validateSupergraph(s)
maybe? "received" seems extraneous. I can see the argument to keep it plural but it doesn't seem necessary, fine either way.
internals-js/src/supergraphs.ts
Outdated
@@ -118,7 +123,7 @@ export class Supergraph { | |||
// Note that `extractSubgraphsFromSupergraph` redo a little bit of work we're already one, like validating | |||
// the supergraph. We could refactor things to avoid it, but it's completely negligible in practice so we | |||
// can leave that to "some day, maybe". | |||
this._subgraphs = extractSubgraphsFromSupergraph(this.schema); | |||
this._subgraphs = extractSubgraphsFromSupergraph(this.schema, !this.assumeValid); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love that we start with validateReceivedSupergraphs
then go to assumeValid
then back to validateExtractedSubgraphs
. It would be nice to not keep negating the poor boolean.
@@ -634,8 +634,8 @@ export class ApolloGateway implements GatewayInterface { | |||
} | |||
|
|||
private createSchemaFromSupergraphSdl(supergraphSdl: string) { | |||
const assumeValid = !(this.config.validateReceivedSupergraphs ?? process.env.NODE_ENV !== 'production'); | |||
const supergraph = Supergraph.build(supergraphSdl, { assumeValid }); | |||
const validateSupergraph = this.config.validateSupergraph ?? process.env.NODE_ENV !== 'production'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is wrong. If they explicitly set validateSupergraph
to false, we should probably honor that, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
false ?? true
equates to false, unlike false || true
, so I think this is doing what we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, of course. Clearly been doing too much rust lately. ship it.
When receiving a supergraph, we were always running all the graphQL validations on both the supergraph itslef, and on each of the subgraphs we extract from it.
This is useful for us when doing dev, as it allows to get decent error message when we break the supergraph generation process or the subgraph extraction process, but this is of very limited value otherwise: even if we don't run those full validations, the supergraph still has to be valid graphQL syntax to parse in the first place, and the subgraph extraction will still throw if the supergraph is not essentially a valid supergraph (and note that the code checking that the
join
spec version is valid, or that the supergraph does not use non-supported feature forSECURITY
orEXECUTION
is not disabled by this).Besides, it's not like uplink will send basically-valid-but-slightly-corrupted supergraphs, and when using
IntrospectAndCompose
there isn't really room for the supergraph to be corrupted.And those validations are actually not super cheap, especially on large supergraphs, so running them feels wasteful.
This commit thus disable those validation in production environment (when
NODE_ENV === "production"
) by default. It keeps those validation during dev mainly for 2 reasons:The commit also introduce a
validateReceivedSupergraphs
in the gateway config to allows overriding the default behaviour described above.