Skip to content
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

Make ApolloServer.stop invoke ApolloGateway.stop #4907

Merged
merged 3 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The version headers in this history reflect the versions of Apollo Server itself

> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. With few exceptions, the format of the entry should follow convention (i.e., prefix with package name, use markdown `backtick formatting` for package names and code, suffix with a link to the change-set à la `[PR #YYY](https://link/pull/YYY)`, etc.). When a release is being prepared, a new header will be (manually) created below and the appropriate changes within that release will be moved into the new section.

- `apollo-server-core`: When used with `ApolloGateway`, `ApolloServer.stop` now invokes `ApolloGateway.stop`. (This makes sense because `ApolloServer` already invokes `ApolloGateway.load` which is what starts the behavior stopped by `ApolloGateway.stop`.) Note that `@apollo/gateway` 0.23 will expect to be stopped in order for natural program shutdown to occur. [Issue #4428](https://github.com/apollographql/apollo-server/issues/4428)
- `apollo-server-core`: Avoid instrumenting schemas for the old `graphql-extensions` library unless extensions are provided. [PR #4893](https://github.com/apollographql/apollo-server/pull/4893) [Issue #4889](https://github.com/apollographql/apollo-server/issues/4889)
- `apollo-server-plugin-response-cache`: The `shouldReadFromCache` and `shouldWriteToCache` hooks were always documented as returning `ValueOrPromise<boolean>` (ie, that they could be either sync or async), but they actually only worked if they returned a bool. Now they can be either sync or async as intended. [PR #4890](http://github.com/apollographql/apollo-server/pull/4890) [Issue #4886](https://github.com/apollographql/apollo-server/issues/4886)

Expand Down
21 changes: 14 additions & 7 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ export class ApolloServerBase {
// Store the unsubscribe handles, which are returned from
// `onSchemaChange`, for later disposal when the server stops
gateway.onSchemaChange(
schema =>
(schema) =>
(this.schemaDerivedData = Promise.resolve(
this.generateSchemaDerivedData(schema),
)),
Expand All @@ -415,17 +415,24 @@ export class ApolloServerBase {
// a federated schema!
this.requestOptions.executor = gateway.executor;

return gateway.load({ apollo: this.apolloConfig, engine: engineConfig })
.then(config => config.schema)
.catch(err => {
return gateway
.load({ apollo: this.apolloConfig, engine: engineConfig })
.then((config) => {
this.toDispose.add(
async () => gateway.stop && (await gateway.stop()),
Copy link
Member

@trevor-scheer trevor-scheer Feb 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I don't think this needs to be awaited here since the toDispose array is wrapped in an awaited Promise.all in the server's stop fn. Also added an optional chain in my suggestion.

Suggested change
async () => gateway.stop && (await gateway.stop()),
() => gateway.stop?.(),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, I totally forgot about ?.() and was confused about why I couldn't get gateway?.stop() to work.

It does still need to be gateway?.stop?.() because I added stop as an optional method on the GraphQLService interface ... just in case there is somebody out there who for some reason has a non-@apollo/gateway implementation of GraphQLService. (I think in AS3 we should get rid of that abstraction and just have gateway be ApolloGateway.)

I do actually find async () => await gateway?.stop?.() to be clearer than () => gateway?.stop?.() in that it makes it really obvious that stop is an async function. Plenty of stop functions exist in the Node world that aren't async, and so to me it adds some clarity. Is that weird of me?

Copy link
Member Author

@glasser glasser Feb 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you told me the same thing last year and I ignored you that time. #4450 (comment)
Though in that one there was some extra complexity around Promise.all and typing that made dropping the async/await not work trivially.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd only need the first ? gateway? if gateway was possibly nullish, but we know here that it isn't. It's only the stop fn on the gateway object that might not exist so just the one is sufficient async () => await gateway.stop?.().

And that's a fine reason to me to keep it around if you prefer!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually... I'm continuing to find myself uncomfortable with functions that return ValueOrPromise<void>. After all "void" means "I'm not going to look at the return value" which is sorta incompatible with "but I need to see if it's a Promise or not". When it's in our public API then we're kinda stuck with it, but toDispose is entirely private, so I'd like to just make it Set<() => Promise<void>> by making the one place where we have a sync function just not be sync.

And once you do that, you actually do need the async/await here, since in the "there is no stop" case it's not returning a Promise.

Also completely ignore my comment above about needing a question mark after gateway, that was wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we only stuck with it as far as compile-time typings go? Might we consider changing that typing in our public API within v3? If so, let's make a ticket and put it on the milestone?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, though I would really just be tempted to eliminate GraphQLService and change the parameter to gateway?: ApolloGateway too. (Or even just add "executeOperation" and "loadSchema" hooks to the plugin interface and make gateway into a plugin.) #4919

);
return config.schema;
})
.catch((err) => {
// We intentionally do not re-throw the exact error from the gateway
// configuration as it may contain implementation details and this
// error will propagate to the client. We will, however, log the error
// for observation in the logs.
const message = "This data graph is missing a valid configuration.";
this.logger.error(message + " " + (err && err.message || err));
const message = 'This data graph is missing a valid configuration.';
this.logger.error(message + ' ' + ((err && err.message) || err));
throw new Error(
message + " More details may be available in the server logs.");
message + ' More details may be available in the server logs.',
);
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/apollo-server-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface GraphQLService {
executor<TContext>(
requestContext: GraphQLRequestContextExecutionDidStart<TContext>,
): ValueOrPromise<GraphQLExecutionResult>;
stop?(): Promise<void>;
}

// This configuration is shared between all integrations and should include
Expand Down