-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
feat: add an opt-in feature to run multiple queries #1673
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
packages/graphiql/src/components/BatchExecuteButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/** | ||
* Copyright (c) 2020 GraphQL Contributors. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import React, { MouseEventHandler } from 'react'; | ||
import { OperationDefinitionNode } from 'graphql'; | ||
|
||
/** | ||
* BatchBatchExecuteButton | ||
* | ||
* What a nice round shiny button. Shows a drop-down checkbox when there are multiple | ||
* queries to run. | ||
*/ | ||
|
||
type BatchExecuteButtonProps = { | ||
operations?: OperationDefinitionNode[]; | ||
isRunning: boolean; | ||
onStop: () => void; | ||
onRun: (value?: Array<string>) => void; | ||
}; | ||
|
||
type BatchExecuteButtonState = { | ||
optionsOpen: boolean; | ||
highlight: OperationDefinitionNode | null; | ||
}; | ||
|
||
export class BatchExecuteButton extends React.Component< | ||
BatchExecuteButtonProps, | ||
BatchExecuteButtonState | ||
> { | ||
constructor(props: BatchExecuteButtonProps) { | ||
super(props); | ||
|
||
this.state = { | ||
optionsOpen: false, | ||
highlight: null, | ||
}; | ||
} | ||
|
||
render() { | ||
const operations = this.props.operations || []; | ||
const optionsOpen = this.state.optionsOpen; | ||
const hasOptions = operations && operations.length > 1; | ||
|
||
let options = null; | ||
if (hasOptions && optionsOpen) { | ||
const highlight = this.state.highlight; | ||
options = ( | ||
<ul className="execute-options"> | ||
<li | ||
key="executeAll" | ||
onMouseOver={event => event.currentTarget.classList.add('selected')} | ||
onMouseOut={event => | ||
event.currentTarget.classList.remove('selected') | ||
} | ||
onMouseUp={() => this._onOptionSelected('executeAll')}> | ||
{'Execute All'} | ||
</li> | ||
{operations.map((operation, i) => { | ||
const opName = operation.name | ||
? operation.name.value | ||
: `<Unnamed ${operation.operation}>`; | ||
return ( | ||
<li | ||
key={`${opName}-${i}`} | ||
className={operation === highlight ? 'selected' : undefined} | ||
onMouseOver={() => this.setState({ highlight: operation })} | ||
onMouseOut={() => this.setState({ highlight: null })} | ||
onMouseUp={() => this._onOptionSelected(operation)}> | ||
{opName} | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
); | ||
} | ||
|
||
// Allow click event if there is a running query or if there are not options | ||
// for which operation to run. | ||
let onClick; | ||
if (this.props.isRunning || !hasOptions) { | ||
onClick = this._onClick; | ||
} | ||
|
||
// Allow mouse down if there is no running query, there are options for | ||
// which operation to run, and the dropdown is currently closed. | ||
let onMouseDown: MouseEventHandler<HTMLButtonElement> = () => {}; | ||
if (!this.props.isRunning && hasOptions && !optionsOpen) { | ||
onMouseDown = this._onOptionsOpen; | ||
} | ||
|
||
const pathJSX = this.props.isRunning ? ( | ||
<path d="M 10 10 L 23 10 L 23 23 L 10 23 z" /> | ||
) : ( | ||
<path d="M 11 9 L 24 16 L 11 23 z" /> | ||
); | ||
|
||
return ( | ||
<div className="execute-button-wrap"> | ||
<button | ||
type="button" | ||
className="execute-button" | ||
onMouseDown={onMouseDown} | ||
onClick={onClick} | ||
title="Execute Query (Ctrl-Enter)"> | ||
<svg width="34" height="34"> | ||
{pathJSX} | ||
</svg> | ||
</button> | ||
{options} | ||
</div> | ||
); | ||
} | ||
|
||
_onClick = () => { | ||
if (this.props.isRunning) { | ||
this.props.onStop(); | ||
} else { | ||
this.props.onRun(); | ||
} | ||
}; | ||
|
||
_onOptionSelected = (operation: OperationDefinitionNode | string) => { | ||
this.setState({ optionsOpen: false }); | ||
const selectedOperations: string[] = []; | ||
if (typeof operation === 'string') { | ||
this.props.operations?.map(operation => { | ||
selectedOperations.push((operation.name && operation.name.value)!); | ||
}); | ||
} else { | ||
selectedOperations.push((operation.name && operation.name.value)!); | ||
} | ||
this.props.onRun(selectedOperations); | ||
}; | ||
|
||
_onOptionsOpen: MouseEventHandler<HTMLButtonElement> = downEvent => { | ||
let initialPress = true; | ||
const downTarget = downEvent.currentTarget; | ||
this.setState({ highlight: null, optionsOpen: true }); | ||
|
||
type MouseUpEventHandler = (upEvent: MouseEvent) => void; | ||
let onMouseUp: MouseUpEventHandler | null = upEvent => { | ||
if (initialPress && upEvent.target === downTarget) { | ||
initialPress = false; | ||
} else { | ||
document.removeEventListener('mouseup', onMouseUp!); | ||
onMouseUp = null; | ||
const isOptionsMenuClicked = | ||
upEvent.currentTarget && | ||
downTarget.parentNode?.compareDocumentPosition( | ||
upEvent.currentTarget as Node, | ||
) && | ||
Node.DOCUMENT_POSITION_CONTAINED_BY; | ||
if (!isOptionsMenuClicked) { | ||
// menu calls setState if it was clicked | ||
this.setState({ optionsOpen: false }); | ||
} | ||
} | ||
}; | ||
|
||
document.addEventListener('mouseup', onMouseUp); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we link this to the spec document? is this what the spec document calls this feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature name is as suggested in the issue. Also I could not find the spec document for graphiql, it would be great if you could share the link!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i mean the spec document for this language feature. in the language spec. can you link to it in the readme here?