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

show multipart requests as running until they are finished or cancelled #2907

Merged
merged 4 commits into from
Nov 25, 2022
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
7 changes: 7 additions & 0 deletions .changeset/rich-humans-rescue.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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