-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Execute queries from the variables panel (#509)
- Loading branch information
Showing
10 changed files
with
251 additions
and
189 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { StateEffect, StateField } from "@codemirror/state"; | ||
import type { Command, EditorView } from "@codemirror/view"; | ||
import { executeGraphql, executeUserQuery } from "~/screens/database/connection/connection"; | ||
import { getActiveConnection } from "~/util/connection"; | ||
import { tryParseParams } from "~/util/helpers"; | ||
|
||
const queryEditorEffect = StateEffect.define<EditorView>(); | ||
|
||
/** | ||
* The field holding the target query editor | ||
*/ | ||
export const queryEditorField = StateField.define<EditorView | undefined>({ | ||
create() { | ||
return undefined; | ||
}, | ||
update(set, tr) { | ||
for (const e of tr.effects) { | ||
if (e.is(queryEditorEffect)) { | ||
return e.value; | ||
} | ||
} | ||
|
||
return set; | ||
}, | ||
}); | ||
|
||
/** | ||
* Set the query editor the current view is using | ||
* | ||
* @param currentView The view to update | ||
* @param queryView The view holding the query | ||
*/ | ||
export function setQueryEditor(currentView: EditorView, queryView?: EditorView) { | ||
currentView.dispatch({ | ||
effects: queryEditorEffect.of(queryView ?? currentView), | ||
}); | ||
} | ||
|
||
/** | ||
* Execute the contents of the editor as a query | ||
* The source editor can be overriden using the `queryEditorEffect` | ||
*/ | ||
export const executeEditorQuery: Command = (view: EditorView) => { | ||
const editor = view.state.field(queryEditorField, false) ?? view; | ||
const query = editor.state.doc.toString(); | ||
const selection = editor.state.selection.main; | ||
|
||
executeUserQuery({ | ||
override: selection?.empty === false ? query.slice(selection.from, selection.to) : query, | ||
}); | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Execute the contents of the editor as a GraphQL query | ||
*/ | ||
export const executeGraphqlEditorQuery: Command = () => { | ||
const connection = getActiveConnection(); | ||
const params = tryParseParams(connection.graphqlVariables); | ||
|
||
executeGraphql(connection.graphqlQuery, params); | ||
|
||
return true; | ||
}; |
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
Oops, something went wrong.