|
| 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(¶ms.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 = ¶ms.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 | + ¶ms.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