-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: I often find myself in a situation where I want to quickly run a Relay query outside of the configured Relay environment, i.e. a GraphQL IDE or another tool. When not using persisted operations, I need to go to the artifact, copy out the value of the `text` property and get rid of all the `\n` line breaks in the text. For persisted operations it's less tedious, I just grab the `id` from the artifact and look up the operation in the operation store, but it's still more cumbersome than it has to be. This implements a new `relay/printOperation` request in the LSP that returns the transformed operation text of the operation under the cursor or the first operation inside the document. The VS Code Extension is also updated with a `relay.copyOperation` command that invokes that LSP request and copies the operation text to the clipboard. <img src="https://github.com/user-attachments/assets/52cdff85-dad2-4236-b4d8-c155c02c2c97" width="500" alt="Example of the command in action" /> Pull Request resolved: #4778 Reviewed By: tyao1 Differential Revision: D63033129 Pulled By: captbaritone fbshipit-source-id: d5caf83b4b0135e4b30ce8cfaf4ef23108f52db6
- Loading branch information
1 parent
36eecfe
commit 8d2380b
Showing
8 changed files
with
227 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
use graphql_ir::OperationDefinitionName; | ||
use lsp_types::request::Request; | ||
use lsp_types::TextDocumentPositionParams; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
use crate::GlobalState; | ||
use crate::LSPRuntimeError; | ||
use crate::LSPRuntimeResult; | ||
|
||
pub(crate) fn on_print_operation( | ||
state: &impl GlobalState, | ||
params: <PrintOperation as Request>::Params, | ||
) -> LSPRuntimeResult<<PrintOperation as Request>::Result> { | ||
let text_document_uri = params | ||
.text_document_position_params | ||
.text_document | ||
.uri | ||
.clone(); | ||
|
||
let project_name = state.extract_project_name_from_url(&text_document_uri)?; | ||
let executable_document_under_cursor = | ||
state.extract_executable_document_from_text(¶ms.text_document_position_params, 1); | ||
|
||
let operation_name = match executable_document_under_cursor { | ||
Ok((document, _)) => { | ||
get_first_operation_name(&document.definitions).ok_or(LSPRuntimeError::ExpectedError) | ||
} | ||
Err(_) => { | ||
let executable_definitions = | ||
state.resolve_executable_definitions(&text_document_uri)?; | ||
|
||
if executable_definitions.is_empty() { | ||
return Err(LSPRuntimeError::ExpectedError); | ||
} | ||
|
||
get_first_operation_name(&executable_definitions).ok_or(LSPRuntimeError::ExpectedError) | ||
} | ||
}?; | ||
|
||
state | ||
.get_operation_text(operation_name, &project_name) | ||
.map(|operation_text| PrintOperationResponse { | ||
operation_name: operation_name.0.to_string(), | ||
operation_text, | ||
}) | ||
} | ||
|
||
fn get_first_operation_name( | ||
executable_definitions: &[graphql_syntax::ExecutableDefinition], | ||
) -> Option<OperationDefinitionName> { | ||
executable_definitions.iter().find_map(|definition| { | ||
if let graphql_syntax::ExecutableDefinition::Operation(operation) = definition { | ||
if let Some(name) = &operation.name { | ||
return Some(OperationDefinitionName(name.value)); | ||
} | ||
|
||
None | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
|
||
pub(crate) enum PrintOperation {} | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub(crate) struct PrintOperationParams { | ||
#[serde(flatten)] | ||
pub text_document_position_params: TextDocumentPositionParams, | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub(crate) struct PrintOperationResponse { | ||
pub operation_name: String, | ||
pub operation_text: String, | ||
} | ||
|
||
impl Request for PrintOperation { | ||
type Params = PrintOperationParams; | ||
type Result = PrintOperationResponse; | ||
const METHOD: &'static str = "relay/printOperation"; | ||
} |
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,62 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import * as semver from 'semver'; | ||
import {window, env} from 'vscode'; | ||
import {RequestType, TextDocumentPositionParams} from 'vscode-languageclient'; | ||
import {RelayExtensionContext} from '../context'; | ||
|
||
export function handleCopyOperation(context: RelayExtensionContext): void { | ||
const {binaryVersion} = context.relayBinaryExecutionOptions; | ||
|
||
if (binaryVersion) { | ||
const isSupportedCompilerVersion = | ||
semver.satisfies(binaryVersion, '>18.0') || | ||
semver.prerelease(binaryVersion) != null; | ||
|
||
if (!isSupportedCompilerVersion) { | ||
window.showWarningMessage( | ||
'Unsupported relay-compiler version. Requires >18.0.0', | ||
); | ||
return; | ||
} | ||
} | ||
|
||
if (!context.client || !context.client.isRunning()) { | ||
return; | ||
} | ||
|
||
const activeEditor = window.activeTextEditor; | ||
|
||
if (!activeEditor) { | ||
return; | ||
} | ||
|
||
const request = new RequestType< | ||
TextDocumentPositionParams, | ||
PrintOperationResponse, | ||
void | ||
>('relay/printOperation'); | ||
|
||
const params: TextDocumentPositionParams = { | ||
textDocument: {uri: activeEditor.document.uri.toString()}, | ||
position: activeEditor.selection.active, | ||
}; | ||
|
||
context.client.sendRequest(request, params).then(response => { | ||
env.clipboard.writeText(response.operationText).then(() => { | ||
window.showInformationMessage( | ||
`Copied operation "${response.operationName}" to clipboard`, | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
type PrintOperationResponse = { | ||
operationName: string; | ||
operationText: string; | ||
}; |
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