Skip to content

Commit ad4b15c

Browse files
kassensfacebook-github-bot
authored andcommitted
Relay type information tool
Reviewed By: captbaritone Differential Revision: D78378445 fbshipit-source-id: 4e95ea3797c87fd7a66edfcdf485a3b9549779b5
1 parent 4660b13 commit ad4b15c

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

compiler/crates/relay-lsp/src/explore_schema_for_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::lsp_runtime_error::LSPRuntimeError;
1515
use crate::lsp_runtime_error::LSPRuntimeResult;
1616
use crate::server::GlobalState;
1717

18-
mod types;
18+
pub(crate) mod types;
1919

2020
pub(crate) struct ExploreSchemaForType {}
2121

compiler/crates/relay-lsp/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod shutdown;
3333
mod status_reporter;
3434
pub mod status_updater;
3535
pub mod text_documents;
36+
pub mod type_information;
3637
pub mod utils;
3738
use std::path::Path;
3839
use std::sync::Arc;

compiler/crates/relay-lsp/src/server.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ use crate::text_documents::on_did_change_text_document;
102102
use crate::text_documents::on_did_close_text_document;
103103
use crate::text_documents::on_did_open_text_document;
104104
use crate::text_documents::on_did_save_text_document;
105+
use crate::type_information::TypeInformation;
106+
use crate::type_information::on_type_information;
105107

106108
/// Initializes an LSP connection, handling the `initialize` message and `initialized` notification
107109
/// handshake.
@@ -266,6 +268,7 @@ fn dispatch_request(request: lsp_server::Request, lsp_state: &impl GlobalState)
266268
let request = LSPRequestDispatch::new(request, lsp_state)
267269
.on_request_sync::<ResolvedTypesAtLocation>(on_get_resolved_types_at_location)?
268270
.on_request_sync::<SearchSchemaItems>(on_search_schema_items)?
271+
.on_request_sync::<TypeInformation>(on_type_information)?
269272
.on_request_sync::<ExploreSchemaForType>(on_explore_schema_for_type)?
270273
.on_request_sync::<GetSourceLocationOfTypeDefinition>(
271274
on_get_source_location_of_type_definition,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
//! Type information LSP request
9+
10+
use intern::Lookup;
11+
use intern::string_key::Intern;
12+
use lsp_types::Url;
13+
use lsp_types::request::Request;
14+
use schema::Schema;
15+
use serde::Deserialize;
16+
use serde::Serialize;
17+
18+
use crate::LSPRuntimeError;
19+
use crate::explore_schema_for_type::types;
20+
use crate::lsp_runtime_error::LSPRuntimeResult;
21+
use crate::server::GlobalState;
22+
23+
pub(crate) fn on_type_information(
24+
lsp_state: &impl GlobalState,
25+
params: TypeInformationParams,
26+
) -> LSPRuntimeResult<TypeInformationResponse> {
27+
let Ok(project_name) = lsp_state.extract_project_name_from_url(&params.uri) else {
28+
return Err(LSPRuntimeError::UnexpectedError(format!(
29+
"Unable to extract Relay GraphQL project from uri: {:?}",
30+
params.uri
31+
)));
32+
};
33+
34+
let type_name = &params.type_name;
35+
36+
let schema = lsp_state.get_schema(&project_name)?;
37+
38+
let type_ = if params.type_name == "Query" {
39+
schema.query_type()
40+
} else if params.type_name == "Subscription" {
41+
schema.subscription_type()
42+
} else if params.type_name == "Mutation" {
43+
schema.mutation_type()
44+
} else {
45+
schema.get_type(type_name.intern())
46+
};
47+
48+
let Some(type_) = type_ else {
49+
return Err(LSPRuntimeError::UnexpectedError(format!(
50+
"Unable to find type information for {type_name}",
51+
)));
52+
};
53+
54+
// TODO these should not be expected errors
55+
let schema_item = types::get_full_schema_explorer_type_reference(
56+
type_,
57+
&params.type_name,
58+
&schema,
59+
&lsp_state.get_schema_documentation(project_name.lookup()),
60+
&None,
61+
None,
62+
);
63+
64+
Ok(TypeInformationResponse { schema_item })
65+
}
66+
67+
pub(crate) enum TypeInformation {}
68+
69+
#[derive(Deserialize, Serialize, Debug)]
70+
#[serde(rename_all = "camelCase")]
71+
pub(crate) struct TypeInformationParams {
72+
pub uri: Url,
73+
pub type_name: String,
74+
}
75+
76+
#[derive(Deserialize, Serialize)]
77+
#[serde(rename_all = "camelCase")]
78+
pub(crate) struct TypeInformationResponse {
79+
schema_item: crate::explore_schema_for_type::types::SchemaExplorerTypeReference<
80+
crate::explore_schema_for_type::types::SchemaExplorerSchemaType,
81+
>,
82+
}
83+
84+
impl Request for TypeInformation {
85+
type Params = TypeInformationParams;
86+
type Result = TypeInformationResponse;
87+
const METHOD: &'static str = "relay/typeInformation";
88+
}

0 commit comments

Comments
 (0)