Skip to content

WIP: incremental payloads UI #3601

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

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 @@ import {
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 @@ -195,6 +201,12 @@ export function ExecutionContextProvider({

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

let isSubscription = false;
if (queryEditor.documentAST) {
const operation = getOperationAST(queryEditor.documentAST, opName);
isSubscription = operation?.operation === OperationTypeNode.SUBSCRIPTION;
}

history?.addToHistory({
query,
variables: variablesString,
Expand Down Expand Up @@ -247,15 +259,35 @@ export function ExecutionContextProvider({
},
);

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

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(
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;
},
error(error: Error) {
setIsFetching(false);
Expand All @@ -274,9 +306,20 @@ export function ExecutionContextProvider({
setSubscription({
unsubscribe: () => value[Symbol.asyncIterator]().return?.(),
});

for await (const result of value) {
handleResponse(result, true);
handleResponse(
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;
}

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