|
| 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 | +#![warn(clippy::all)] |
| 9 | + |
| 10 | +use std::fmt::Error; |
| 11 | +use std::fmt::Write; |
| 12 | +use std::path::PathBuf; |
| 13 | + |
| 14 | +use common::NamedItem; |
| 15 | +use common::SourceLocationKey; |
| 16 | +use intern::Lookup; |
| 17 | +use relay_codegen::printer::get_module_path; |
| 18 | +use relay_transforms::generate_relay_resolvers_model_fragments::get_resolver_source_hash; |
| 19 | +use relay_transforms::get_fragment_filename; |
| 20 | +use relay_transforms::get_resolver_fragment_dependency_name; |
| 21 | +use relay_transforms::relay_resolvers::get_resolver_info; |
| 22 | +use relay_transforms::relay_resolvers::ResolverInfo; |
| 23 | +use relay_transforms::RESOLVER_BELONGS_TO_BASE_SCHEMA_DIRECTIVE; |
| 24 | +use relay_typegen::TypegenLanguage; |
| 25 | +use schema::SDLSchema; |
| 26 | +use schema::Schema; |
| 27 | +use schema::Type; |
| 28 | +use signedsource::sign_file; |
| 29 | + |
| 30 | +use super::artifact_content::content::generate_docblock_section; |
| 31 | +use super::artifact_content::content::write_export_generated_node; |
| 32 | +use super::artifact_content::content_section::GenericSection; |
| 33 | +use crate::config::Config; |
| 34 | +use crate::config::ProjectConfig; |
| 35 | +use crate::Artifact; |
| 36 | +use crate::ArtifactContent; |
| 37 | +use crate::ArtifactSourceKey; |
| 38 | + |
| 39 | +pub fn generate_resolvers_schema_module( |
| 40 | + config: &Config, |
| 41 | + project_config: &ProjectConfig, |
| 42 | + schema: &SDLSchema, |
| 43 | + output_path: PathBuf, |
| 44 | +) -> Result<Artifact, Error> { |
| 45 | + let mut artifact_source_keys = vec![]; |
| 46 | + let content = generate_resolvers_schema_module_content( |
| 47 | + config, |
| 48 | + project_config, |
| 49 | + schema, |
| 50 | + &output_path, |
| 51 | + &mut artifact_source_keys, |
| 52 | + )?; |
| 53 | + Ok(Artifact { |
| 54 | + artifact_source_keys, |
| 55 | + path: output_path, |
| 56 | + content: ArtifactContent::Generic { |
| 57 | + content: sign_file(&content).into_bytes(), |
| 58 | + }, |
| 59 | + source_file: SourceLocationKey::generated(), |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +fn generate_resolvers_schema_module_content( |
| 64 | + config: &Config, |
| 65 | + project_config: &ProjectConfig, |
| 66 | + schema: &SDLSchema, |
| 67 | + artifact_path: &PathBuf, |
| 68 | + artifact_source_keys: &mut Vec<ArtifactSourceKey>, |
| 69 | +) -> Result<String, Error> { |
| 70 | + let mut content = String::new(); |
| 71 | + |
| 72 | + let docblock = generate_docblock_section(config, project_config, vec![])?; |
| 73 | + writeln!(content, "{}", docblock)?; |
| 74 | + |
| 75 | + let mut schema_resolvers_object = String::new(); |
| 76 | + let mut schema_resolvers_type = String::new(); |
| 77 | + let mut imports = String::new(); |
| 78 | + |
| 79 | + writeln!(schema_resolvers_type, "type SchemaResolvers = {{")?; |
| 80 | + if project_config.typegen_config.language == TypegenLanguage::Flow { |
| 81 | + writeln!( |
| 82 | + schema_resolvers_object, |
| 83 | + "var schema_resolvers /*:: : SchemaResolvers*/ = {{" |
| 84 | + )?; |
| 85 | + } else { |
| 86 | + writeln!( |
| 87 | + schema_resolvers_object, |
| 88 | + "const schema_resolvers: SchemaResolvers = {{" |
| 89 | + )?; |
| 90 | + } |
| 91 | + writeln!( |
| 92 | + imports, |
| 93 | + "import type {{NormalizationSplitOperation}} from 'relay-runtime';" |
| 94 | + )?; |
| 95 | + |
| 96 | + for object in schema.get_objects() { |
| 97 | + let mut has_resolvers = false; |
| 98 | + |
| 99 | + for field in object.fields.iter().map(|field_id| schema.field(*field_id)) { |
| 100 | + if let Some(Ok(ResolverInfo { |
| 101 | + import_path, |
| 102 | + import_name: Some(import_name), |
| 103 | + .. |
| 104 | + })) = get_resolver_info(schema, field, field.name.location) |
| 105 | + { |
| 106 | + if field |
| 107 | + .directives |
| 108 | + .named(*RESOLVER_BELONGS_TO_BASE_SCHEMA_DIRECTIVE) |
| 109 | + .is_some() |
| 110 | + { |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + if !has_resolvers { |
| 115 | + has_resolvers = true; |
| 116 | + writeln!( |
| 117 | + schema_resolvers_type, |
| 118 | + "\t{object_name}: {{", |
| 119 | + object_name = object.name.item |
| 120 | + )?; |
| 121 | + writeln!( |
| 122 | + schema_resolvers_object, |
| 123 | + "\t{object_name}: {{", |
| 124 | + object_name = object.name.item |
| 125 | + )?; |
| 126 | + } |
| 127 | + if let Some(source_hash) = get_resolver_source_hash(field) { |
| 128 | + artifact_source_keys.push(ArtifactSourceKey::ResolverHash(source_hash)); |
| 129 | + } |
| 130 | + |
| 131 | + let js_import_path = project_config.js_module_import_identifier( |
| 132 | + artifact_path, |
| 133 | + &PathBuf::from(import_path.lookup()), |
| 134 | + ); |
| 135 | + let js_import_path_without_suffix = |
| 136 | + get_module_path(project_config.js_module_format, js_import_path); |
| 137 | + |
| 138 | + let parent_type = match field.parent_type.unwrap() { |
| 139 | + Type::Interface(interface_id) => schema.interface(interface_id).name.item.0, |
| 140 | + Type::Object(object_id) => schema.object(object_id).name.item.0, |
| 141 | + _ => panic!("Unexpected parent type for resolver."), |
| 142 | + }; |
| 143 | + let type_name = format!("{}_{}", &parent_type, import_name); |
| 144 | + |
| 145 | + writeln!( |
| 146 | + imports, |
| 147 | + "import typeof {{ {import_name} as {type_name} }} from '{import_path}';", |
| 148 | + import_path = js_import_path_without_suffix, |
| 149 | + import_name = import_name, |
| 150 | + type_name = type_name |
| 151 | + )?; |
| 152 | + |
| 153 | + writeln!(schema_resolvers_type, "\t\t{}: {{", import_name)?; |
| 154 | + writeln!( |
| 155 | + schema_resolvers_type, |
| 156 | + "\t\t\tresolverFunction: {},", |
| 157 | + type_name |
| 158 | + )?; |
| 159 | + writeln!( |
| 160 | + schema_resolvers_type, |
| 161 | + "\t\t\trootFragment: ?NormalizationSplitOperation," |
| 162 | + )?; |
| 163 | + writeln!(schema_resolvers_type, "\t\t}},",)?; |
| 164 | + |
| 165 | + writeln!(schema_resolvers_object, "\t\t{}: {{", import_name)?; |
| 166 | + writeln!( |
| 167 | + schema_resolvers_object, |
| 168 | + "\t\t\tresolverFunction: require('{import_path}').{import_name},", |
| 169 | + import_path = js_import_path_without_suffix, |
| 170 | + import_name = import_name |
| 171 | + )?; |
| 172 | + writeln!( |
| 173 | + schema_resolvers_object, |
| 174 | + "\t\t\trootFragment: {}", |
| 175 | + match get_resolver_fragment_dependency_name(field) { |
| 176 | + None => "null".to_string(), |
| 177 | + Some(fragment_name) => { |
| 178 | + format!( |
| 179 | + "require('{import}')", |
| 180 | + import = get_fragment_filename(fragment_name) |
| 181 | + ) |
| 182 | + } |
| 183 | + } |
| 184 | + )?; |
| 185 | + writeln!(schema_resolvers_object, "\t\t}},",)?; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if has_resolvers { |
| 190 | + writeln!(schema_resolvers_type, "\t}},")?; |
| 191 | + writeln!(schema_resolvers_object, "\t}},")?; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + writeln!(schema_resolvers_type, "}};")?; |
| 196 | + writeln!(schema_resolvers_object, "}};")?; |
| 197 | + |
| 198 | + if project_config.typegen_config.language == TypegenLanguage::Flow { |
| 199 | + writeln!(content, "/*::")?; |
| 200 | + } |
| 201 | + writeln!(content, "{}\n", imports)?; |
| 202 | + writeln!(content, "{}\n", schema_resolvers_type)?; |
| 203 | + if project_config.typegen_config.language == TypegenLanguage::Flow { |
| 204 | + writeln!(content, "*/")?; |
| 205 | + } |
| 206 | + writeln!(content, "{}\n", schema_resolvers_object)?; |
| 207 | + |
| 208 | + let mut export = GenericSection::default(); |
| 209 | + write_export_generated_node( |
| 210 | + &project_config.typegen_config, |
| 211 | + &mut export, |
| 212 | + "schema_resolvers", |
| 213 | + None, |
| 214 | + )?; |
| 215 | + writeln!(content, "{}", export)?; |
| 216 | + |
| 217 | + Ok(content) |
| 218 | +} |
0 commit comments