Skip to content

Commit be8f5f7

Browse files
sverrejohfacebook-github-bot
authored andcommitted
Enhance schemaExtensions to Support Both Files and Directories (#4859)
Summary: **Summary** This update enhances the behavior of the `"schemaExtensions"` configuration key, allowing it to specify either: - A folder containing GraphQL schema extension files (as originally documented). - A specific .graphql schema extension file (new functionality). This change unifies behavior between the Watchman query and the walkdir implementation, ensuring consistent handling of schema extensions regardless of configuration. **Details** Previously, the Relay documentation instructed users to configure "schemaExtensions" with a folder path containing the GraphQL schema extensions. However: • Relay did not enforce that the target was a folder. • Users could already specify a single .graphql schema extension file directly and it would work when not using Watchman. This update ensures both approaches work seamlessly with Watchman and enhancing flexibility. The documentation has also been updated to reflect this new functionality. **Why This Change?** This improvement resolves an inconsistency between Watchman and non-Watchman setups, offering better support for varying project configurations without breaking existing workflows. Pull Request resolved: #4859 Reviewed By: evanyeung Differential Revision: D68106213 Pulled By: captbaritone fbshipit-source-id: ca2f2f401429face4b60c88b033099450e6a3e05
1 parent 99cbea8 commit be8f5f7

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -994,12 +994,12 @@ impl SingleProjectConfigFile {
994994
}
995995
})?,
996996
);
997-
for extension_dir in self.schema_extensions.iter() {
997+
for extension_path in self.schema_extensions.iter() {
998998
paths.push(
999-
canonicalize(root_dir.join(extension_dir.clone())).map_err(|_| {
1000-
ConfigValidationError::ExtensionDirNotExistent {
999+
canonicalize(root_dir.join(extension_path.clone())).map_err(|_| {
1000+
ConfigValidationError::ExtensionPathNotExistent {
10011001
project_name: self.project_name,
1002-
extension_dir: extension_dir.clone(),
1002+
extension_path: extension_path.clone(),
10031003
}
10041004
})?,
10051005
);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ pub enum ConfigValidationError {
218218
},
219219

220220
#[error(
221-
"The `schemaExtensions` configured for project `{project_name}` does not exist at `{extension_dir}`."
221+
"The `schemaExtensions` configured for project `{project_name}` does not exist at `{extension_path}`."
222222
)]
223-
ExtensionDirNotExistent {
223+
ExtensionPathNotExistent {
224224
project_name: ProjectName,
225-
extension_dir: PathBuf,
225+
extension_path: PathBuf,
226226
},
227227

228228
#[error(

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn get_watchman_expr(config: &Config) -> Expr {
6767

6868
let extension_roots = config.get_extension_roots();
6969
if !extension_roots.is_empty() {
70-
let extensions_expr = expr_graphql_files_in_dirs(extension_roots);
70+
let extensions_expr = expr_graphql_file_or_dir_contents(extension_roots);
7171
expressions.push(extensions_expr);
7272
}
7373

@@ -132,13 +132,27 @@ fn expr_files_in_dirs(roots: Vec<PathBuf>) -> Expr {
132132

133133
fn expr_graphql_files_in_dirs(roots: Vec<PathBuf>) -> Expr {
134134
Expr::All(vec![
135-
// ending in *.graphql
135+
// ending in *.graphql or *.gql
136136
Expr::Suffix(vec!["graphql".into(), "gql".into()]),
137137
// in one of the extension directories
138138
expr_files_in_dirs(roots),
139139
])
140140
}
141141

142+
// Expression to get all graphql items by path or path of containing folder.
143+
fn expr_graphql_file_or_dir_contents(paths: Vec<PathBuf>) -> Expr {
144+
Expr::All(vec![
145+
Expr::Suffix(vec!["graphql".into(), "gql".into()]),
146+
Expr::Any(vec![
147+
Expr::Name(NameTerm {
148+
paths: paths.clone(),
149+
wholename: true,
150+
}),
151+
expr_files_in_dirs(paths),
152+
]),
153+
])
154+
}
155+
142156
/// Helper to create an `anyof` expression if multiple items are passed or just
143157
/// return the expression for a single item input `Vec`.
144158
/// Panics for empty expressions. These are not valid in Watchman. We could

packages/relay-compiler/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ file sources, and "listen" to the file changes in the "watch" mode. If
6969
[string]
7070
- `excludes` Directories to ignore under `src`. [array] [default:
7171
["\*\*/node_modules/\*\*", "\*\*/__mocks__/\*\*", "\*\*/__generated__/\*\*"]]
72-
- `schemaExtensions` List of directories with schema extensions. [array]
72+
- `schemaExtensions` List of files or directories with schema extensions. [array]
7373
- `schemaConfig`
7474
- `nodeInterfaceIdField` Configure the name of the globally unique ID field on
7575
the Node interface. Useful if you can't use the default `id` field name.

website/docs/guides/client-schema-extensions.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ The Relay Compiler fully supports client-side extensions of the schema, which al
2929

3030
## Extending the server schema
3131

32-
To extend the server schema, create a new `.graphql` file inside your `--src` directory.
33-
Let's call it `./src/clientSchema.graphql`.
34-
This file needs to be in a folder referenced in the `"schemaExtensions"` of your Relay config.
32+
To extend the server schema, create a new `.graphql` file inside your `--src`
33+
directory. Let's call it `./src/clientSchema.graphql`. This file needs to be
34+
referenced in the `"schemaExtensions"` of your Relay config, either directly or
35+
via its folder.
3536

3637
This schema describes what local data can be queried on the client.
3738
It can even be used to extend an existing server schema.

0 commit comments

Comments
 (0)