Skip to content

Commit 3a7d000

Browse files
show multipart requests as running until they are finished or cancelled (#2907)
* show multipart requests as running until they are finished or cancelled * Improve changeset wording Co-authored-by: Ted Thibodeau Jr <tthibodeau@openlinksw.com> * fix grammar Co-authored-by: Ted Thibodeau Jr <tthibodeau@openlinksw.com> * fix grammar Co-authored-by: Ted Thibodeau Jr <tthibodeau@openlinksw.com> Co-authored-by: Ted Thibodeau Jr <tthibodeau@openlinksw.com>
1 parent 83364b2 commit 3a7d000

3 files changed

Lines changed: 29 additions & 12 deletions

File tree

.changeset/rich-humans-rescue.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@graphiql/react': minor
3+
---
4+
5+
Clearly separate the fetching and subscription states for multipart
6+
requests (like subscriptions) and show the stop-button as long as the
7+
subscription is running

packages/graphiql-react/src/execution.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ import { createContextHook, createNullableContext } from './utility/context';
1919

2020
export type ExecutionContextType = {
2121
/**
22-
* If there is currently a GraphQL request in-flight. For long-running
23-
* requests like subscriptions this will be `true` until the request is
24-
* stopped manually.
22+
* If there is currently a GraphQL request in-flight. For multi-part
23+
* requests like subscriptions, this will be `true` while fetching the
24+
* first partial response and `false` while fetching subsequent batches.
2525
*/
2626
isFetching: boolean;
27+
/**
28+
* If there is currently a GraphQL request in-flight. For multi-part
29+
* requests like subscriptions, this will be `true` until the last batch
30+
* has been fetched or the connection is closed from the client.
31+
*/
32+
isSubscribed: boolean;
2733
/**
2834
* The operation name that will be sent with all GraphQL requests.
2935
*/
@@ -315,14 +321,16 @@ export function ExecutionContextProvider(props: ExecutionContextProviderProps) {
315321
variableEditor,
316322
]);
317323

324+
const isSubscribed = Boolean(subscription);
318325
const value = useMemo<ExecutionContextType>(
319326
() => ({
320327
isFetching,
328+
isSubscribed,
321329
operationName: props.operationName ?? null,
322330
run,
323331
stop,
324332
}),
325-
[isFetching, props.operationName, run, stop],
333+
[isFetching, isSubscribed, props.operationName, run, stop],
326334
);
327335

328336
return (

packages/graphiql-react/src/toolbar/execute.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@ export function ExecuteButton() {
1010
nonNull: true,
1111
caller: ExecuteButton,
1212
});
13-
const { isFetching, operationName, run, stop } = useExecutionContext({
14-
nonNull: true,
15-
caller: ExecuteButton,
16-
});
13+
const { isFetching, isSubscribed, operationName, run, stop } =
14+
useExecutionContext({
15+
nonNull: true,
16+
caller: ExecuteButton,
17+
});
1718

1819
const operations = queryEditor?.operations || [];
1920
const hasOptions = operations.length > 1 && typeof operationName !== 'string';
21+
const isRunning = isFetching || isSubscribed;
2022

21-
const label = `${isFetching ? 'Stop' : 'Execute'} query (Ctrl-Enter)`;
23+
const label = `${isRunning ? 'Stop' : 'Execute'} query (Ctrl-Enter)`;
2224
const buttonProps = {
2325
type: 'button' as const,
2426
className: 'graphiql-execute-button',
25-
children: isFetching ? <StopIcon /> : <PlayIcon />,
27+
children: isRunning ? <StopIcon /> : <PlayIcon />,
2628
'aria-label': label,
2729
};
2830

29-
return hasOptions && !isFetching ? (
31+
return hasOptions && !isRunning ? (
3032
<Menu>
3133
<Tooltip label={label}>
3234
<Menu.Button {...buttonProps} />
@@ -63,7 +65,7 @@ export function ExecuteButton() {
6365
<button
6466
{...buttonProps}
6567
onClick={() => {
66-
if (isFetching) {
68+
if (isRunning) {
6769
stop();
6870
} else {
6971
run();

0 commit comments

Comments
 (0)