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

Allow protocol errors to be retryable in RetryLink #12318

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions .changeset/thin-oranges-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
"@apollo/client": patch
---

Allow `RetryLink` to retry an operation when fatal [transport-level errors](https://www.apollographql.com/docs/graphos/routing/operations/subscriptions/multipart-protocol#message-and-error-format) are emitted from multipart subscriptions.

```js
const retryLink = new RetryLink({
attempts: (count, operation, error) => {
if (error instanceof ApolloError) {
// errors available on the `protocolErrors` field in `ApolloError`
console.log(error.protocolErrors)
}

return true;
}
});
```
66 changes: 65 additions & 1 deletion src/link/retry/__tests__/retryLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { execute } from "../../core/execute";
import { Observable } from "../../../utilities/observables/Observable";
import { fromError } from "../../utils/fromError";
import { RetryLink } from "../retryLink";
import { ObservableStream } from "../../../testing/internal";
import {
mockMultipartSubscriptionStream,
ObservableStream,
} from "../../../testing/internal";
import { ApolloError } from "../../../core";

const query = gql`
{
Expand Down Expand Up @@ -210,4 +214,64 @@ describe("RetryLink", () => {
[3, operation, standardError],
]);
});

it("handles protocol errors from multipart subscriptions", async () => {
const subscription = gql`
subscription MySubscription {
aNewDieWasCreated {
die {
roll
sides
color
}
}
}
`;

const attemptStub = jest.fn();
attemptStub.mockReturnValueOnce(true);

const retryLink = new RetryLink({
delay: { initial: 1 },
attempts: attemptStub,
});

const { httpLink, enqueuePayloadResult, enqueueProtocolErrors } =
mockMultipartSubscriptionStream();
const link = ApolloLink.from([retryLink, httpLink]);
const stream = new ObservableStream(execute(link, { query: subscription }));

enqueueProtocolErrors([
{ message: "Error field", extensions: { code: "INTERNAL_SERVER_ERROR" } },
]);

enqueuePayloadResult({
data: {
aNewDieWasCreated: { die: { color: "blue", roll: 2, sides: 6 } },
},
});

await expect(stream).toEmitValue({
data: {
aNewDieWasCreated: { die: { color: "blue", roll: 2, sides: 6 } },
},
});

expect(attemptStub).toHaveBeenCalledTimes(1);
expect(attemptStub).toHaveBeenCalledWith(
1,
expect.objectContaining({
operationName: "MySubscription",
query: subscription,
}),
new ApolloError({
protocolErrors: [
{
message: "Error field",
extensions: { code: "INTERNAL_SERVER_ERROR" },
},
],
})
);
});
});
21 changes: 20 additions & 1 deletion src/link/retry/retryLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { buildDelayFunction } from "./delayFunction.js";
import type { RetryFunction, RetryFunctionOptions } from "./retryFunction.js";
import { buildRetryFunction } from "./retryFunction.js";
import type { SubscriptionObserver } from "zen-observable-ts";
import {
ApolloError,
graphQLResultHasProtocolErrors,
PROTOCOL_ERRORS_SYMBOL,
} from "../../errors/index.js";

export namespace RetryLink {
export interface Options {
Expand Down Expand Up @@ -54,7 +59,21 @@ class RetryableOperation {

private try() {
this.currentSubscription = this.forward(this.operation).subscribe({
next: this.observer.next.bind(this.observer),
next: (result) => {
if (graphQLResultHasProtocolErrors(result)) {
this.onError(
new ApolloError({
protocolErrors: result.extensions[PROTOCOL_ERRORS_SYMBOL],
})
);
// Unsubscribe from the current subscription to prevent the `complete`
// handler to be called as a result of the stream closing.
this.currentSubscription?.unsubscribe();
return;
}

this.observer.next(result);
},
error: this.onError,
complete: this.observer.complete.bind(this.observer),
});
Expand Down