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

WIP: incremental payloads UI #3601

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
49 changes: 46 additions & 3 deletions packages/graphiql-react/src/execution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
isObservable,
Unsubscribable,
} from '@graphiql/toolkit';
import { ExecutionResult, FragmentDefinitionNode, print } from 'graphql';
import {
ExecutionResult,
FragmentDefinitionNode,
getOperationAST,
OperationTypeNode,
print,
} from 'graphql';
import { getFragmentDependenciesForAST } from 'graphql-language-service';
import { ReactNode, useCallback, useMemo, useRef, useState } from 'react';

Expand Down Expand Up @@ -120,24 +126,24 @@
}

// Clear any incremental results of previous runs
updateActiveTabValues({ incrementalPayloads: [] });

Check warning on line 129 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L129

Added line #L129 was not covered by tests

const startTime = Date.now();
const incrementalPayloads: IncrementalPayload[] = [];
const setResponse = (

Check warning on line 133 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L131-L133

Added lines #L131 - L133 were not covered by tests
value: string,
incrementalPayload?: ExecutionResult | IncrementalResult[],
) => {
responseEditor.setValue(value);

if (incrementalPayload) {
incrementalPayloads.push({

Check warning on line 140 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L140

Added line #L140 was not covered by tests
timing: Date.now() - startTime,
payload: incrementalPayload,
});
}

updateActiveTabValues({ response: value, incrementalPayloads });

Check warning on line 146 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L146

Added line #L146 was not covered by tests
};

queryIdRef.current += 1;
Expand Down Expand Up @@ -195,6 +201,12 @@

const opName = operationName ?? queryEditor.operationName ?? undefined;

let isSubscription = false;

Check warning on line 204 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L204

Added line #L204 was not covered by tests
if (queryEditor.documentAST) {
const operation = getOperationAST(queryEditor.documentAST, opName);
isSubscription = operation?.operation === OperationTypeNode.SUBSCRIPTION;

Check warning on line 207 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L206-L207

Added lines #L206 - L207 were not covered by tests
}

history?.addToHistory({
query,
variables: variablesString,
Expand Down Expand Up @@ -227,7 +239,7 @@
}

setIsFetching(false);
setResponse(formatResult(fullResponse), maybeMultipart);

Check warning on line 242 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L242

Added line #L242 was not covered by tests
} else {
const response = formatResult(result);
setIsFetching(false);
Expand All @@ -247,15 +259,35 @@
},
);

// Subscriptions are always considered streamed responses
let isStreaming = isSubscription;

Check warning on line 263 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L263

Added line #L263 was not covered by tests

const value = await Promise.resolve(fetch);
if (isObservable(value)) {
// If the fetcher returned an Observable, then subscribe to it, calling
// the callback on each next value, and handling both errors and the
// completion of the Observable.
//
// Note: The naming of the React state assumes that in this case the
// operation is a subscription (which in practice it most likely is),
// but technically it can also be a query or mutation where the fetcher
// decided to return an observable with either a single payload or
// multiple payloads (defer/stream). As the naming is part of the
// public API of this context we decide not to change it until defer/
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// public API of this context we decide not to change it until defer/
// public API of this context, we decided not to change it until defer/

// stream is officially part of the GraphQL spec.
setSubscription(
value.subscribe({
next(result) {
handleResponse(result, true);
handleResponse(

Check warning on line 281 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L281

Added line #L281 was not covered by tests
result,
// If the initial payload contains `hasNext` for a query or
// mutation then we know it's a streamed response.
isStreaming || result.hasNext,
);

// If there's more than one payload then we're streaming, so set
// this flag to `true` for any future calls to `next`.
isStreaming = true;

Check warning on line 290 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L290

Added line #L290 was not covered by tests
},
error(error: Error) {
setIsFetching(false);
Expand All @@ -274,9 +306,20 @@
setSubscription({
unsubscribe: () => value[Symbol.asyncIterator]().return?.(),
});

for await (const result of value) {
handleResponse(result, true);
handleResponse(

Check warning on line 311 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L311

Added line #L311 was not covered by tests
result,
// If the initial payload contains `hasNext` for a query or
// mutation then we know it's a streamed response.
isStreaming || result.hasNext,
);

// If there's more than one payload then we're streaming, so set this
// flag to `true` for any future loop iterations.
isStreaming = true;

Check warning on line 320 in packages/graphiql-react/src/execution.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql-react/src/execution.tsx#L320

Added line #L320 was not covered by tests
}

setIsFetching(false);
setSubscription(null);
} else {
Expand Down
Loading