Skip to content

Commit f571ad8

Browse files
Shashank Kambhampatimeta-codesync[bot]
authored andcommitted
Relay Flatbuffer SchemaLocation
Reviewed By: tyao1 Differential Revision: D94971587 fbshipit-source-id: 6c7a971407c70b49bb1dc335cf5ab42abb30074f
1 parent 745bd3e commit f571ad8

7 files changed

Lines changed: 55 additions & 76 deletions

File tree

compiler/crates/relay-compiler/relay-compiler-config-schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@
250250
"type": "string"
251251
}
252252
},
253+
"schemaFlatbuffer": {
254+
"type": [
255+
"string",
256+
"null"
257+
]
258+
},
253259
"schemaName": {
254260
"description": "Schema name, if differs from project name.\nIf schema name is unset, the project name will be used as schema name.",
255261
"anyOf": [

compiler/crates/relay-compiler/src/build_project/build_schema.rs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
use std::sync::Arc;
99

10+
use common::Diagnostic;
1011
use common::DiagnosticsResult;
12+
use common::Location;
1113
use common::PerfLogEvent;
1214
use fnv::FnvHashMap;
1315
use relay_config::ProjectName;
@@ -52,49 +54,17 @@ pub fn build_schema(
5254
)
5355
}
5456

55-
fn load_flatbuffer_schema(project_config: &ProjectConfig) -> Option<SDLSchema> {
56-
let fb_path = match &project_config.schema_location {
57-
SchemaLocation::Directory(dir) => {
58-
let mut fb_path = dir.clone();
59-
let dir_name = fb_path.file_name()?.to_str()?.to_string();
60-
fb_path.pop();
61-
fb_path.push(format!("{}.flatbuffer", dir_name));
62-
fb_path
63-
}
64-
SchemaLocation::File(file) => file.with_extension("flatbuffer"),
65-
};
66-
if !fb_path.exists() {
67-
return None;
68-
}
69-
match std::fs::read(&fb_path) {
70-
Ok(bytes) => {
71-
log::info!(
72-
"Loading FlatBuffer schema from {:?} ({} bytes)",
73-
fb_path,
74-
bytes.len()
75-
);
76-
Some(build_schema_with_flat_buffer_unchecked(bytes))
77-
}
78-
Err(e) => {
79-
log::warn!(
80-
"Failed to read FlatBuffer schema {:?}: {}, falling back to SDL",
81-
fb_path,
82-
e,
83-
);
84-
None
85-
}
86-
}
87-
}
88-
8957
fn build_schema_impl(
9058
compiler_state: &CompilerState,
9159
project_config: &ProjectConfig,
9260
log_event: &impl PerfLogEvent,
9361
config: &Config,
9462
graphql_asts_map: &FnvHashMap<ProjectName, GraphQLAsts>,
9563
) -> DiagnosticsResult<Arc<SDLSchema>> {
96-
if let Some(schema) = load_flatbuffer_schema(project_config) {
97-
return Ok(Arc::new(schema));
64+
if let SchemaLocation::FlatbufferFile(fb_path) = &project_config.schema_location {
65+
let contents = std::fs::read(config.root_dir.join(fb_path))
66+
.map_err(|e| vec![Diagnostic::error(e.to_string(), Location::generated())])?;
67+
return Ok(Arc::new(build_schema_with_flat_buffer_unchecked(contents)));
9868
}
9969

10070
let schema_sources = get_schema_sources(compiler_state, project_config);

compiler/crates/relay-compiler/src/config.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -401,23 +401,27 @@ impl Config {
401401
let projects = projects
402402
.into_iter()
403403
.map(|(project_name, config_file_project)| {
404-
let schema_location =
405-
match (config_file_project.schema, config_file_project.schema_dir) {
406-
(Some(schema_file), None) => Ok(SchemaLocation::File(
407-
normalize_relative_path(&root_dir, &schema_file),
408-
)),
409-
(None, Some(schema_dir)) => Ok(SchemaLocation::Directory(
410-
normalize_relative_path(&root_dir, &schema_dir),
411-
)),
412-
_ => Err(Error::ConfigFileValidation {
413-
config_path: config_path.clone(),
414-
validation_errors: vec![
415-
ConfigValidationError::ProjectNeedsSchemaXorSchemaDir {
416-
project_name,
417-
},
418-
],
419-
}),
420-
}?;
404+
let schema_location = match (
405+
config_file_project.schema,
406+
config_file_project.schema_dir,
407+
config_file_project.schema_flatbuffer,
408+
) {
409+
(Some(schema_file), None, None) => Ok(SchemaLocation::File(
410+
normalize_relative_path(&root_dir, &schema_file),
411+
)),
412+
(None, Some(schema_dir), None) => Ok(SchemaLocation::Directory(
413+
normalize_relative_path(&root_dir, &schema_dir),
414+
)),
415+
(None, None, Some(schema_flatbuffer)) => Ok(SchemaLocation::FlatbufferFile(
416+
normalize_relative_path(&root_dir, &schema_flatbuffer),
417+
)),
418+
_ => Err(Error::ConfigFileValidation {
419+
config_path: config_path.clone(),
420+
validation_errors: vec![
421+
ConfigValidationError::ProjectNeedsSchemaXorSchemaDir { project_name },
422+
],
423+
}),
424+
}?;
421425

422426
let shard_strip_regex = config_file_project
423427
.shard_strip_regex
@@ -654,7 +658,7 @@ impl Config {
654658

655659
for (_, project) in &self.projects {
656660
match &project.schema_location {
657-
SchemaLocation::File(schema_file) => {
661+
SchemaLocation::FlatbufferFile(schema_file) | SchemaLocation::File(schema_file) => {
658662
validator.assert_is_included_schema_file(schema_file);
659663
}
660664
SchemaLocation::Directory(schema_dir) => {
@@ -734,6 +738,7 @@ impl Config {
734738
.filter_map(|project_config| match &project_config.schema_location {
735739
SchemaLocation::File(schema_file) => Some(schema_file.clone()),
736740
SchemaLocation::Directory(_) => None,
741+
SchemaLocation::FlatbufferFile(schema_file) => Some(schema_file.clone()),
737742
})
738743
.collect()
739744
}
@@ -745,6 +750,7 @@ impl Config {
745750
.filter_map(|project_config| match &project_config.schema_location {
746751
SchemaLocation::File(_) => None,
747752
SchemaLocation::Directory(schema_dir) => Some(schema_dir.clone()),
753+
SchemaLocation::FlatbufferFile(_) => None,
748754
})
749755
.collect()
750756
}
@@ -1241,6 +1247,7 @@ pub struct ConfigFileProject {
12411247
/// Exactly 1 of these options needs to be defined.
12421248
schema: Option<PathBuf>,
12431249
schema_dir: Option<PathBuf>,
1250+
schema_flatbuffer: Option<PathBuf>,
12441251

12451252
/// Schema name, if differs from project name.
12461253
/// If schema name is unset, the project name will be used as schema name.

compiler/crates/relay-compiler/src/file_source/file_categorizer.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,29 +179,20 @@ impl FileCategorizer {
179179
}
180180

181181
let mut schema_file_mapping: HashMap<PathBuf, ProjectSet> = Default::default();
182-
for (&project_name, project_config) in &config.projects {
183-
if let SchemaLocation::File(schema_file) = &project_config.schema_location {
184-
match schema_file_mapping.entry(schema_file.clone()) {
185-
Entry::Vacant(entry) => {
186-
entry.insert(ProjectSet::of(project_name));
187-
}
188-
Entry::Occupied(mut entry) => {
189-
entry.get_mut().insert(project_name);
190-
}
191-
}
192-
}
193-
}
194-
195182
let mut schema_dir_mapping_map: HashMap<PathBuf, ProjectSet> = Default::default();
196183
for (&project_name, project_config) in &config.projects {
197-
if let SchemaLocation::Directory(directory) = &project_config.schema_location {
198-
match schema_dir_mapping_map.entry(directory.clone()) {
199-
Entry::Vacant(entry) => {
200-
entry.insert(ProjectSet::of(project_name));
201-
}
202-
Entry::Occupied(mut entry) => {
203-
entry.get_mut().insert(project_name);
204-
}
184+
match &project_config.schema_location {
185+
SchemaLocation::FlatbufferFile(schema_file) | SchemaLocation::File(schema_file) => {
186+
schema_file_mapping
187+
.entry(schema_file.clone())
188+
.and_modify(|project_set| project_set.insert(project_name))
189+
.or_insert_with(|| ProjectSet::of(project_name));
190+
}
191+
SchemaLocation::Directory(directory) => {
192+
schema_dir_mapping_map
193+
.entry(directory.clone())
194+
.and_modify(|project_set| project_set.insert(project_name))
195+
.or_insert_with(|| ProjectSet::of(project_name));
205196
}
206197
}
207198
}

compiler/crates/relay-compiler/src/file_source/file_filter.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ fn get_extra_roots(config: &Config, enabled_projects: &FnvHashSet<ProjectName>)
7575
roots.push(output_dir);
7676
}
7777
match &project_config.schema_location {
78-
SchemaLocation::File(path) | SchemaLocation::Directory(path) => roots.push(path),
78+
SchemaLocation::FlatbufferFile(path)
79+
| SchemaLocation::File(path)
80+
| SchemaLocation::Directory(path) => roots.push(path),
7981
}
8082
}
8183
unify_roots(roots)

compiler/crates/relay-compiler/src/subschema_extraction.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub async fn compile_and_extract_subschema(
8484
SchemaLocation::Directory(_) => {
8585
return Err(SubschemaError::DirectorySchemaNotSupported);
8686
}
87+
SchemaLocation::FlatbufferFile(file) => file.clone(),
8788
};
8889

8990
// Normalize the full schema path relative to root_dir

compiler/crates/relay-config/src/project_config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ pub enum SchemaLocation {
174174
File(PathBuf),
175175
/// A directory containing multiple schema files.
176176
Directory(PathBuf),
177+
/// A single file containing a Flatbuffer-serialized schema.
178+
FlatbufferFile(PathBuf),
177179
}
178180

179181
pub struct ExtraArtifactsConfig {

0 commit comments

Comments
 (0)