Skip to content

Commit

Permalink
show multipart requests as running until they are finished or cancell…
Browse files Browse the repository at this point in the history
…ed (#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>
  • Loading branch information
thomasheyenbrock and TallTed committed Nov 25, 2022
1 parent 83364b2 commit 3a7d000
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .changeset/rich-humans-rescue.md
@@ -0,0 +1,7 @@
---
'@graphiql/react': minor
---

Clearly separate the fetching and subscription states for multipart
requests (like subscriptions) and show the stop-button as long as the
subscription is running
16 changes: 12 additions & 4 deletions packages/graphiql-react/src/execution.tsx
Expand Up @@ -19,11 +19,17 @@ import { createContextHook, createNullableContext } from './utility/context';

export type ExecutionContextType = {
/**
* If there is currently a GraphQL request in-flight. For long-running
* requests like subscriptions this will be `true` until the request is
* stopped manually.
* If there is currently a GraphQL request in-flight. For multi-part
* requests like subscriptions, this will be `true` while fetching the
* first partial response and `false` while fetching subsequent batches.
*/
isFetching: boolean;
/**
* If there is currently a GraphQL request in-flight. For multi-part
* requests like subscriptions, this will be `true` until the last batch
* has been fetched or the connection is closed from the client.
*/
isSubscribed: boolean;
/**
* The operation name that will be sent with all GraphQL requests.
*/
Expand Down Expand Up @@ -315,14 +321,16 @@ export function ExecutionContextProvider(props: ExecutionContextProviderProps) {
variableEditor,
]);

const isSubscribed = Boolean(subscription);
const value = useMemo<ExecutionContextType>(
() => ({
isFetching,
isSubscribed,
operationName: props.operationName ?? null,
run,
stop,
}),
[isFetching, props.operationName, run, stop],
[isFetching, isSubscribed, props.operationName, run, stop],
);

return (
Expand Down
18 changes: 10 additions & 8 deletions packages/graphiql-react/src/toolbar/execute.tsx
Expand Up @@ -10,23 +10,25 @@ export function ExecuteButton() {
nonNull: true,
caller: ExecuteButton,
});
const { isFetching, operationName, run, stop } = useExecutionContext({
nonNull: true,
caller: ExecuteButton,
});
const { isFetching, isSubscribed, operationName, run, stop } =
useExecutionContext({
nonNull: true,
caller: ExecuteButton,
});

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

const label = `${isFetching ? 'Stop' : 'Execute'} query (Ctrl-Enter)`;
const label = `${isRunning ? 'Stop' : 'Execute'} query (Ctrl-Enter)`;
const buttonProps = {
type: 'button' as const,
className: 'graphiql-execute-button',
children: isFetching ? <StopIcon /> : <PlayIcon />,
children: isRunning ? <StopIcon /> : <PlayIcon />,
'aria-label': label,
};

return hasOptions && !isFetching ? (
return hasOptions && !isRunning ? (
<Menu>
<Tooltip label={label}>
<Menu.Button {...buttonProps} />
Expand Down Expand Up @@ -63,7 +65,7 @@ export function ExecuteButton() {
<button
{...buttonProps}
onClick={() => {
if (isFetching) {
if (isRunning) {
stop();
} else {
run();
Expand Down

0 comments on commit 3a7d000

Please sign in to comment.