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

fix(core): Add missing error patching and support incremental results in subscriptionExchange #3055

Merged
merged 4 commits into from
Mar 15, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/fuzzy-baboons-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': minor
---

Update `subscriptionExchange` to support incremental results out of the box. If a subscription proactively completes, results are also now updated with `hasNext: false`.
5 changes: 5 additions & 0 deletions .changeset/poor-pumpkins-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Fix incremental results not merging `errors` from subsequent non-incremental results.
18 changes: 15 additions & 3 deletions packages/core/src/exchanges/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
makeResult,
makeErrorResult,
makeOperation,
mergeResultPatch,
} from '../utils';

import {
Expand Down Expand Up @@ -158,14 +159,23 @@ export const subscriptionExchange = ({
return make<OperationResult>(({ next, complete }) => {
let isComplete = false;
let sub: Subscription | void;
let result: OperationResult | void;

Promise.resolve().then(() => {
if (isComplete) return;

sub = observableish.subscribe({
next: result => next(makeResult(operation, result)),
error: err => next(makeErrorResult(operation, err)),
complete: () => {
next(nextResult) {
next(
(result = result
? mergeResultPatch(result, nextResult)
: makeResult(operation, nextResult))
);
},
error(error) {
next(makeErrorResult(operation, error));
},
complete() {
if (!isComplete) {
isComplete = true;
if (operation.kind === 'subscription') {
Expand All @@ -174,6 +184,8 @@ export const subscriptionExchange = ({
);
}

if (result && result.hasNext)
next(mergeResultPatch(result, { hasNext: false }));
complete();
}
},
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/utils/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ export const mergeResultPatch = (
response?: any
): OperationResult => {
let data: ExecutionResult['data'];
let errors = prevResult.error ? prevResult.error.graphQLErrors : [];
let hasExtensions = !!prevResult.extensions || !!nextResult.extensions;
const extensions = { ...prevResult.extensions, ...nextResult.extensions };
const errors = prevResult.error ? prevResult.error.graphQLErrors : [];

let incremental = nextResult.incremental;

Expand Down Expand Up @@ -115,6 +115,7 @@ export const mergeResultPatch = (
}
} else {
data = nextResult.data || prevResult.data;
errors = (nextResult.errors as any[]) || errors;
}

return {
Expand Down