From 7e6ce8dabfdd2b62c991bb446d8cce5e843abdaa Mon Sep 17 00:00:00 2001 From: Stewart Avery Date: Fri, 19 Aug 2022 10:21:30 -0400 Subject: [PATCH] modifying directive to allow for client fields --- compiler/Cargo.lock | 186 ++++++++++++------ compiler/crates/common/src/feature_flags.rs | 4 + .../tests/compile_relay_artifacts/mod.rs | 1 + .../mod.rs | 1 + .../relay-transforms/src/apply_transforms.rs | 8 +- .../src/test_operation_metadata.rs | 35 +++- .../fixtures/prod_query.invalid.expected | 1 + .../fixtures/prod_query.invalid.graphql | 1 + ...client_fields_not_generated_query.expected | 37 ++++ ..._client_fields_not_generated_query.graphql | 23 +++ .../test_client_fields_query.expected | 37 ++++ .../fixtures/test_client_fields_query.graphql | 23 +++ .../fixtures/test_query_with_enums.expected | 1 + .../fixtures/test_query_with_enums.graphql | 1 + .../fixtures/test_simple_query.expected | 1 + .../fixtures/test_simple_query.graphql | 1 + .../tests/relay_test_operation/mod.rs | 68 ++++++- .../tests/relay_test_operation_test.rs | 17 +- 18 files changed, 374 insertions(+), 72 deletions(-) create mode 100644 compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_not_generated_query.expected create mode 100644 compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_not_generated_query.graphql create mode 100644 compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_query.expected create mode 100644 compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_query.graphql diff --git a/compiler/Cargo.lock b/compiler/Cargo.lock index 40707328fbc50..5eea9f7f3ec35 100644 --- a/compiler/Cargo.lock +++ b/compiler/Cargo.lock @@ -157,9 +157,9 @@ dependencies = [ [[package]] name = "clap" -version = "3.2.10" +version = "3.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69c5a7f9997be616e47f0577ee38c91decb33392c5be4866494f34cdf329a9aa" +checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750" dependencies = [ "atty", "bitflags", @@ -177,9 +177,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.2.7" +version = "3.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "759bf187376e1afa7b85b959e6a664a3e7a95203415dba952ad19139e798f902" +checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" dependencies = [ "heck 0.4.0", "proc-macro-error", @@ -402,6 +402,27 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "errors" version = "0.0.0" @@ -490,9 +511,9 @@ checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678" [[package]] name = "futures" -version = "0.3.13" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f55667319111d593ba876406af7c409c0ebb44dc4be6132a783ccf163ea14c1" +checksum = "7f21eda599937fba36daeb58a22e8f5cee2d14c4a17b5b7739c7c8e5e3b8230c" dependencies = [ "futures-channel", "futures-core", @@ -505,9 +526,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.13" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c2dd2df839b57db9ab69c2c9d8f3e8c81984781937fe2807dc6dcf3b2ad2939" +checksum = "30bdd20c28fadd505d0fd6712cdfcb0d4b5648baf45faef7f852afb2399bb050" dependencies = [ "futures-core", "futures-sink", @@ -515,15 +536,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.13" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15496a72fabf0e62bdc3df11a59a3787429221dd0710ba8ef163d6f7a9112c94" +checksum = "4e5aa3de05362c3fb88de6531e6296e85cde7739cccad4b9dfeeb7f6ebce56bf" [[package]] name = "futures-executor" -version = "0.3.13" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891a4b7b96d84d5940084b2a37632dd65deeae662c114ceaa2c879629c9c0ad1" +checksum = "9ff63c23854bee61b6e9cd331d523909f238fc7636290b96826e9cfa5faa00ab" dependencies = [ "futures-core", "futures-task", @@ -532,17 +553,16 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.13" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71c2c65c57704c32f5241c1223167c2c3294fd34ac020c807ddbe6db287ba59" +checksum = "bbf4d2a7a308fd4578637c0b17c7e1c7ba127b8f6ba00b29f717e9655d85eb68" [[package]] name = "futures-macro" -version = "0.3.13" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea405816a5139fb39af82c2beb921d52143f556038378d6db21183a5c37fbfb7" +checksum = "42cd15d1c7456c04dbdf7e88bcd69760d74f3a798d6444e16974b505b0e62f17" dependencies = [ - "proc-macro-hack", "proc-macro2", "quote", "syn", @@ -550,21 +570,21 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.13" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85754d98985841b7d4f5e8e6fbfa4a4ac847916893ec511a2917ccd8525b8bb3" +checksum = "21b20ba5a92e727ba30e72834706623d94ac93a725410b6a6b6fbc1b07f7ba56" [[package]] name = "futures-task" -version = "0.3.13" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa189ef211c15ee602667a6fcfe1c1fd9e07d42250d2156382820fba33c9df80" +checksum = "a6508c467c73851293f390476d4491cf4d227dbabcd4170f3bb6044959b294f1" [[package]] name = "futures-util" -version = "0.3.13" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1812c7ab8aedf8d6f2701a43e1243acdbcc2b36ab26e2ad421eb99ac963d96d1" +checksum = "44fb6cb1be61cc1d2e43b262516aafcf63b241cffdb1d3fa115f91d9c7b09c90" dependencies = [ "futures 0.1.31", "futures-channel", @@ -576,8 +596,6 @@ dependencies = [ "memchr", "pin-project-lite", "pin-utils", - "proc-macro-hack", - "proc-macro-nested", "slab", ] @@ -692,6 +710,7 @@ dependencies = [ "graphql-syntax", "intern", "relay-test-schema", + "relay-transforms", "schema", ] @@ -726,9 +745,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.11.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" dependencies = [ "ahash", "serde", @@ -847,9 +866,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.8.0" +version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" dependencies = [ "autocfg", "hashbrown", @@ -895,6 +914,12 @@ dependencies = [ "serde", ] +[[package]] +name = "io-lifetimes" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ea37f355c05dde75b84bba2d767906ad522e97cd9e2eef2be7a4ab7fb442c06" + [[package]] name = "iovec" version = "0.1.4" @@ -952,9 +977,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.112" +version = "0.2.134" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "329c933548736bc49fd575ee68c89e8be4d260064184389a5b77517cddd99ffb" + +[[package]] +name = "linux-raw-sys" +version = "0.0.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" +checksum = "d4d2456c373231a208ad294c33dc5bff30051eafd954cd4caae83a712b12854d" [[package]] name = "lock_api" @@ -1314,18 +1345,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "proc-macro-hack" -version = "0.5.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" - -[[package]] -name = "proc-macro-nested" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" - [[package]] name = "proc-macro2" version = "1.0.39" @@ -1488,7 +1507,7 @@ dependencies = [ "extract-graphql", "fixture-tests", "fnv", - "futures 0.3.13", + "futures 0.3.24", "glob", "graphql-cli", "graphql-ir", @@ -1689,6 +1708,20 @@ dependencies = [ "schema", ] +[[package]] +name = "rustix" +version = "0.35.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbb2fda4666def1433b1b05431ab402e42a1084285477222b72d6c564c417cef" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "ryu" version = "1.0.5" @@ -2042,19 +2075,19 @@ dependencies = [ [[package]] name = "terminal_size" -version = "0.1.17" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" +checksum = "8440c860cf79def6164e4a0a983bcc2305d82419177a0e0c71930d049e3ac5a1" dependencies = [ - "libc", - "winapi", + "rustix", + "windows-sys", ] [[package]] name = "textwrap" -version = "0.15.0" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" +checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16" dependencies = [ "terminal_size", "unicode-width", @@ -2062,18 +2095,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.30" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" +checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.30" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" +checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" dependencies = [ "proc-macro2", "quote", @@ -2321,7 +2354,7 @@ checksum = "839fea2d85719bb69089290d7970bba2131f544448db8f990ea75813c30775ca" dependencies = [ "anyhow", "bytes 1.0.1", - "futures 0.3.13", + "futures 0.3.24", "maplit", "serde", "serde_bser", @@ -2362,6 +2395,49 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" + +[[package]] +name = "windows_i686_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" + +[[package]] +name = "windows_i686_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" + [[package]] name = "zstd" version = "0.11.1+zstd.1.5.2" diff --git a/compiler/crates/common/src/feature_flags.rs b/compiler/crates/common/src/feature_flags.rs index 7349cf11f15ee..422f38dee378f 100644 --- a/compiler/crates/common/src/feature_flags.rs +++ b/compiler/crates/common/src/feature_flags.rs @@ -62,6 +62,10 @@ pub struct FeatureFlags { /// Print queries in compact form #[serde(default)] pub compact_query_text: FeatureFlag, + + // Enables @relay_test_operation support for client data. + #[serde(default)] + pub enable_mock_client_data_metadata: FeatureFlag, } #[derive(Debug, Deserialize, Clone, Serialize)] diff --git a/compiler/crates/relay-compiler/tests/compile_relay_artifacts/mod.rs b/compiler/crates/relay-compiler/tests/compile_relay_artifacts/mod.rs index 4e322bb3c9e32..c7b4c95943616 100644 --- a/compiler/crates/relay-compiler/tests/compile_relay_artifacts/mod.rs +++ b/compiler/crates/relay-compiler/tests/compile_relay_artifacts/mod.rs @@ -117,6 +117,7 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result { skip_printing_nulls: FeatureFlag::Disabled, enable_fragment_aliases: FeatureFlag::Enabled, compact_query_text: FeatureFlag::Disabled, + enable_mock_client_data_metadata: FeatureFlag::Disabled, }; let default_project_config = ProjectConfig { diff --git a/compiler/crates/relay-compiler/tests/compile_relay_artifacts_with_custom_id/mod.rs b/compiler/crates/relay-compiler/tests/compile_relay_artifacts_with_custom_id/mod.rs index a6d76518a8dbb..7bb176d69424c 100644 --- a/compiler/crates/relay-compiler/tests/compile_relay_artifacts_with_custom_id/mod.rs +++ b/compiler/crates/relay-compiler/tests/compile_relay_artifacts_with_custom_id/mod.rs @@ -87,6 +87,7 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result { skip_printing_nulls: FeatureFlag::Disabled, enable_fragment_aliases: FeatureFlag::Enabled, compact_query_text: FeatureFlag::Disabled, + enable_mock_client_data_metadata: FeatureFlag::Disabled, }; let project_config = ProjectConfig { diff --git a/compiler/crates/relay-transforms/src/apply_transforms.rs b/compiler/crates/relay-transforms/src/apply_transforms.rs index 1bc9833ea5536..7ab4367db0917 100644 --- a/compiler/crates/relay-transforms/src/apply_transforms.rs +++ b/compiler/crates/relay-transforms/src/apply_transforms.rs @@ -479,7 +479,13 @@ fn apply_normalization_transforms( } program = log_event.time("generate_test_operation_metadata", || { - generate_test_operation_metadata(&program, &project_config.test_path_regex) + generate_test_operation_metadata( + &program, + &project_config.test_path_regex, + &project_config + .feature_flags + .enable_mock_client_data_metadata, + ) })?; if let Some(print_stats) = maybe_print_stats { print_stats("generate_test_operation_metadata", &program); diff --git a/compiler/crates/relay-transforms/src/test_operation_metadata.rs b/compiler/crates/relay-transforms/src/test_operation_metadata.rs index a0e7443fa28ec..8fc709dac1013 100644 --- a/compiler/crates/relay-transforms/src/test_operation_metadata.rs +++ b/compiler/crates/relay-transforms/src/test_operation_metadata.rs @@ -9,6 +9,7 @@ use common::ArgumentName; use common::Diagnostic; use common::DiagnosticsResult; use common::DirectiveName; +use common::FeatureFlag; use common::NamedItem; use common::WithLocation; use graphql_ir::ConstantArgument; @@ -45,6 +46,7 @@ lazy_static! { static ref NULLABLE_KEY: ArgumentName = ArgumentName("nullable".intern()); static ref PLURAL_KEY: ArgumentName = ArgumentName("plural".intern()); static ref TYPE_KEY: ArgumentName = ArgumentName("type".intern()); + static ref MOCK_CLIENT_DATA: ArgumentName = ArgumentName("mock_client_data".intern()); } /// Transforms the @relay_test_operation directive to @__metadata thats printed @@ -54,8 +56,13 @@ lazy_static! { pub fn generate_test_operation_metadata( program: &Program, test_path_regex: &Option, + enable_mock_client_data_metadata_flag: &FeatureFlag, ) -> DiagnosticsResult { - let mut transformer = GenerateTestOperationMetadata::new(program, test_path_regex); + let mut transformer = GenerateTestOperationMetadata::new( + program, + test_path_regex, + enable_mock_client_data_metadata_flag, + ); let next_program = transformer .transform_program(program) .replace_or_else(|| program.clone()); @@ -67,23 +74,29 @@ pub fn generate_test_operation_metadata( } } -struct GenerateTestOperationMetadata<'a> { - program: &'a Program, - test_path_regex: &'a Option, +struct GenerateTestOperationMetadata<'program, 'flag> { + program: &'program Program, + test_path_regex: &'program Option, errors: Vec, + enable_mock_client_data_metadata_flag: &'flag FeatureFlag, } -impl<'a> GenerateTestOperationMetadata<'a> { - fn new(program: &'a Program, test_path_regex: &'a Option) -> Self { +impl<'program, 'flag> GenerateTestOperationMetadata<'program, 'flag> { + fn new( + program: &'program Program, + test_path_regex: &'program Option, + enable_mock_client_data_metadata_flag: &'flag FeatureFlag, + ) -> Self { GenerateTestOperationMetadata { program, test_path_regex, errors: Vec::new(), + enable_mock_client_data_metadata_flag: &enable_mock_client_data_metadata_flag, } } } -impl<'a> Transformer for GenerateTestOperationMetadata<'a> { +impl<'program, 'flag> Transformer for GenerateTestOperationMetadata<'program, 'flag> { const NAME: &'static str = "GenerateTestOperationMetadata"; const VISIT_ARGUMENTS: bool = false; const VISIT_DIRECTIVES: bool = false; @@ -116,6 +129,8 @@ impl<'a> Transformer for GenerateTestOperationMetadata<'a> { ConstantValue::Object(From::from(RelayTestOperationMetadata::new( self.program, &operation.selections, + self.enable_mock_client_data_metadata_flag + .is_fully_enabled(), ))), )); } else { @@ -211,7 +226,7 @@ pub struct RelayTestOperationMetadata { } impl RelayTestOperationMetadata { - pub fn new(program: &Program, selections: &[Selection]) -> Self { + pub fn new(program: &Program, selections: &[Selection], mock_client_data: bool) -> Self { let schema = program.schema.as_ref(); let mut selection_type_info: IndexMap = Default::default(); @@ -224,7 +239,7 @@ impl RelayTestOperationMetadata { match selection { Selection::ScalarField(scalar_field) => { let field = schema.field(scalar_field.definition.item); - if !field.is_extension { + if !field.is_extension || mock_client_data { let alias_or_name = scalar_field.alias_or_name(schema); let next_path = next_path(path, alias_or_name); selection_type_info.insert( @@ -235,7 +250,7 @@ impl RelayTestOperationMetadata { } Selection::LinkedField(linked_field) => { let field = schema.field(linked_field.definition.item); - if !field.is_extension { + if !field.is_extension || mock_client_data { let alias_or_name = linked_field.alias_or_name(schema); let next_path = next_path(path, alias_or_name); selection_type_info.insert( diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/prod_query.invalid.expected b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/prod_query.invalid.expected index a07827b0a110c..c206d1baa742d 100644 --- a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/prod_query.invalid.expected +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/prod_query.invalid.expected @@ -6,6 +6,7 @@ query ProdQuery @relay_test_operation { id } } +# %extensions% ==================================== ERROR ==================================== ✖︎ The `@relay_test_operation` directive is only allowed within test files because it creates larger generated files we don't want to include in production. File does not match test regex: ^test diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/prod_query.invalid.graphql b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/prod_query.invalid.graphql index d2da5fa230a06..1a34320349ec9 100644 --- a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/prod_query.invalid.graphql +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/prod_query.invalid.graphql @@ -5,3 +5,4 @@ query ProdQuery @relay_test_operation { id } } +# %extensions% \ No newline at end of file diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_not_generated_query.expected b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_not_generated_query.expected new file mode 100644 index 0000000000000..a39e375936bb9 --- /dev/null +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_not_generated_query.expected @@ -0,0 +1,37 @@ +==================================== INPUT ==================================== +query ClientFieldsQuery @relay_test_operation { + node(id: "test-id") { + id + ... on User { + name + client_info { + name + description + } + } + } +} + +# %extensions% + +extend type User { + client_info: ClientInfo +} + +type ClientInfo { + name: String + description: String +} +==================================== OUTPUT =================================== +query ClientFieldsQuery @__metadata(relayTestingSelectionTypeInfo: {node: {enumValues: null, nullable: true, plural: false, type: "Node"}, node.id: {enumValues: null, nullable: false, plural: false, type: "ID"}, node.name: {enumValues: null, nullable: true, plural: false, type: "String"}}) { + node(id: "test-id") { + id + ... on User { + name + client_info { + name + description + } + } + } +} diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_not_generated_query.graphql b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_not_generated_query.graphql new file mode 100644 index 0000000000000..0d2e191413010 --- /dev/null +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_not_generated_query.graphql @@ -0,0 +1,23 @@ +query ClientFieldsQuery @relay_test_operation { + node(id: "test-id") { + id + ... on User { + name + client_info { + name + description + } + } + } +} + +# %extensions% + +extend type User { + client_info: ClientInfo +} + +type ClientInfo { + name: String + description: String +} diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_query.expected b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_query.expected new file mode 100644 index 0000000000000..85e54cea82fe2 --- /dev/null +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_query.expected @@ -0,0 +1,37 @@ +==================================== INPUT ==================================== +query ClientFieldsQuery @relay_test_operation { + node(id: "test-id") { + id + ... on User { + name + client_info { + name + description + } + } + } +} + +# %extensions% + +extend type User { + client_info: ClientInfo +} + +type ClientInfo { + name: String + description: String +} +==================================== OUTPUT =================================== +query ClientFieldsQuery @__metadata(relayTestingSelectionTypeInfo: {node: {enumValues: null, nullable: true, plural: false, type: "Node"}, node.id: {enumValues: null, nullable: false, plural: false, type: "ID"}, node.name: {enumValues: null, nullable: true, plural: false, type: "String"}, node.client_info: {enumValues: null, nullable: true, plural: false, type: "ClientInfo"}, node.client_info.name: {enumValues: null, nullable: true, plural: false, type: "String"}, node.client_info.description: {enumValues: null, nullable: true, plural: false, type: "String"}}) { + node(id: "test-id") { + id + ... on User { + name + client_info { + name + description + } + } + } +} diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_query.graphql b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_query.graphql new file mode 100644 index 0000000000000..0d2e191413010 --- /dev/null +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_client_fields_query.graphql @@ -0,0 +1,23 @@ +query ClientFieldsQuery @relay_test_operation { + node(id: "test-id") { + id + ... on User { + name + client_info { + name + description + } + } + } +} + +# %extensions% + +extend type User { + client_info: ClientInfo +} + +type ClientInfo { + name: String + description: String +} diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_query_with_enums.expected b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_query_with_enums.expected index 0df9d69cbc73b..5c4584075b223 100644 --- a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_query_with_enums.expected +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_query_with_enums.expected @@ -8,6 +8,7 @@ query QueryWitEnums @relay_test_operation { } } } +# %extensions% ==================================== OUTPUT =================================== query QueryWitEnums @__metadata(relayTestingSelectionTypeInfo: {node: {enumValues: null, nullable: true, plural: false, type: "Node"}, node.id: {enumValues: null, nullable: false, plural: false, type: "ID"}, node.name: {enumValues: null, nullable: true, plural: false, type: "String"}, node.environment: {enumValues: ["WEB", "MOBILE"], nullable: true, plural: false, type: "Environment"}}) { node(id: "test-id") { diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_query_with_enums.graphql b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_query_with_enums.graphql index c5629f34a9054..6c4be87f92099 100644 --- a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_query_with_enums.graphql +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_query_with_enums.graphql @@ -7,3 +7,4 @@ query QueryWitEnums @relay_test_operation { } } } +# %extensions% \ No newline at end of file diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_simple_query.expected b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_simple_query.expected index 09e0385f19552..e0d1b47d7d646 100644 --- a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_simple_query.expected +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_simple_query.expected @@ -7,6 +7,7 @@ query SimpleQuery @relay_test_operation { } } } +# %extensions% ==================================== OUTPUT =================================== query SimpleQuery @__metadata(relayTestingSelectionTypeInfo: {node: {enumValues: null, nullable: true, plural: false, type: "Node"}, node.id: {enumValues: null, nullable: false, plural: false, type: "ID"}, node.name: {enumValues: null, nullable: true, plural: false, type: "String"}}) { node(id: "test-id") { diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_simple_query.graphql b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_simple_query.graphql index f9428e244f38a..985b6c4356ce2 100644 --- a/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_simple_query.graphql +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/fixtures/test_simple_query.graphql @@ -6,3 +6,4 @@ query SimpleQuery @relay_test_operation { } } } +# %extensions% \ No newline at end of file diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation/mod.rs b/compiler/crates/relay-transforms/tests/relay_test_operation/mod.rs index bf63e2ac636c4..3a88bfd19566c 100644 --- a/compiler/crates/relay-transforms/tests/relay_test_operation/mod.rs +++ b/compiler/crates/relay-transforms/tests/relay_test_operation/mod.rs @@ -5,14 +5,72 @@ * LICENSE file in the root directory of this source tree. */ +use std::sync::Arc; + +use common::FeatureFlag; +use common::SourceLocationKey; use fixture_tests::Fixture; -use graphql_test_helpers::apply_transform_for_test; +use graphql_ir::build; +use graphql_ir::Program; +use graphql_syntax::parse_executable; +use graphql_test_helpers::diagnostics_to_sorted_string; +use graphql_text_printer::print_fragment; +use graphql_text_printer::print_operation; +use graphql_text_printer::PrinterOptions; use regex::Regex; +use relay_test_schema::get_test_schema_with_extensions; use relay_transforms::generate_test_operation_metadata; +fn transform_fixture_inner( + fixture: &Fixture<'_>, + enable_mock_client_data_metadata_flag: &FeatureFlag, +) -> Result { + let parts: Vec<_> = fixture.content.split("%extensions%").collect(); + + if let [base, extensions] = parts.as_slice() { + let source_location = SourceLocationKey::standalone(fixture.file_name); + let ast = parse_executable(base, source_location).unwrap(); + let schema = get_test_schema_with_extensions(extensions); + + let ir = build(&schema, &ast.definitions).unwrap(); + let program = Program::from_definitions(Arc::clone(&schema), ir); + + let test_path_regex = Some(Regex::new(r#"^test"#).unwrap()); + + let next_program = generate_test_operation_metadata( + &program, + &test_path_regex, + enable_mock_client_data_metadata_flag, + ) + .map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?; + + let printer_options = PrinterOptions { + debug_directive_data: true, + ..Default::default() + }; + + let mut printed = next_program + .operations() + .map(|def| print_operation(&schema, def, printer_options.clone())) + .chain( + next_program + .fragments() + .map(|def| print_fragment(&schema, def, printer_options.clone())), + ) + .collect::>(); + printed.sort(); + Ok(printed.join("\n\n")) + } else { + panic!("Expected exactly one %extensions% section marker.") + } +} + pub fn transform_fixture(fixture: &Fixture<'_>) -> Result { - let test_path_regex = Some(Regex::new(r#"^test"#).unwrap()); - apply_transform_for_test(fixture, |program| { - generate_test_operation_metadata(program, &test_path_regex) - }) + transform_fixture_inner(fixture, &FeatureFlag::Disabled) +} + +pub fn transform_fixture_with_mock_client_data_enabled( + fixture: &Fixture<'_>, +) -> Result { + transform_fixture_inner(fixture, &FeatureFlag::Enabled) } diff --git a/compiler/crates/relay-transforms/tests/relay_test_operation_test.rs b/compiler/crates/relay-transforms/tests/relay_test_operation_test.rs index 7f4e0dc5f36ae..c525f5ea65279 100644 --- a/compiler/crates/relay-transforms/tests/relay_test_operation_test.rs +++ b/compiler/crates/relay-transforms/tests/relay_test_operation_test.rs @@ -4,12 +4,13 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<13a74be1bf3601ce595319e9b12ebb4b>> + * @generated SignedSource<<83ef3f0f07b6241de41d1c8d97f48ff4>> */ mod relay_test_operation; use relay_test_operation::transform_fixture; +use relay_test_operation::transform_fixture_with_mock_client_data_enabled; use fixture_tests::test_fixture; #[test] @@ -19,6 +20,20 @@ fn prod_query_invalid() { test_fixture(transform_fixture, "prod_query.invalid.graphql", "relay_test_operation/fixtures/prod_query.invalid.expected", input, expected); } +#[test] +fn test_client_fields_not_generated_query() { + let input = include_str!("relay_test_operation/fixtures/test_client_fields_not_generated_query.graphql"); + let expected = include_str!("relay_test_operation/fixtures/test_client_fields_not_generated_query.expected"); + test_fixture(transform_fixture, "test_client_fields_not_generated_query.graphql", "relay_test_operation/fixtures/test_client_fields_not_generated_query.expected", input, expected); +} + +#[test] +fn test_client_fields_query() { + let input = include_str!("relay_test_operation/fixtures/test_client_fields_query.graphql"); + let expected = include_str!("relay_test_operation/fixtures/test_client_fields_query.expected"); + test_fixture(transform_fixture_with_mock_client_data_enabled, "test_client_fields_query.graphql", "relay_test_operation/fixtures/test_client_fields_query.expected", input, expected); +} + #[test] fn test_query_with_enums() { let input = include_str!("relay_test_operation/fixtures/test_query_with_enums.graphql");