From 031b3c02878f39ec9bcd195104f5acc1d8d26887 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 | 173 ++++++++++++------ 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, 359 insertions(+), 74 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 86ed156720a2e..6bcb9bbfbb0b0 100644 --- a/compiler/Cargo.lock +++ b/compiler/Cargo.lock @@ -68,9 +68,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "beef" @@ -457,7 +457,7 @@ dependencies = [ "colored", "diff", "lazy_static", - "parking_lot", + "parking_lot 0.11.2", "signedsource", ] @@ -617,7 +617,7 @@ checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.10.2+wasi-snapshot-preview1", ] [[package]] @@ -833,7 +833,7 @@ dependencies = [ "httpdate", "itoa 0.4.7", "pin-project-lite", - "socket2 0.4.0", + "socket2", "tokio", "tower-service", "tracing", @@ -894,7 +894,7 @@ dependencies = [ "hashbrown", "indexmap", "once_cell", - "parking_lot", + "parking_lot 0.11.2", "rand", "serde", "serde_bytes", @@ -910,7 +910,7 @@ dependencies = [ "fnv", "lazy_static", "once_cell", - "parking_lot", + "parking_lot 0.11.2", "serde", ] @@ -1093,25 +1093,14 @@ dependencies = [ [[package]] name = "mio" -version = "0.7.9" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5dede4e2065b3842b8b0af444119f3aa331cc7cc2dd20388bfb0f5d5a38823a" +checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" dependencies = [ "libc", "log", - "miow", - "ntapi", - "winapi", -] - -[[package]] -name = "miow" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897" -dependencies = [ - "socket2 0.3.19", - "winapi", + "wasi 0.11.0+wasi-snapshot-preview1", + "windows-sys 0.36.1", ] [[package]] @@ -1132,15 +1121,6 @@ dependencies = [ "tempfile", ] -[[package]] -name = "ntapi" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" -dependencies = [ - "winapi", -] - [[package]] name = "num-integer" version = "0.1.44" @@ -1256,7 +1236,17 @@ checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" dependencies = [ "instant", "lock_api", - "parking_lot_core", + "parking_lot_core 0.8.5", +] + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core 0.9.4", ] [[package]] @@ -1273,6 +1263,19 @@ dependencies = [ "winapi", ] +[[package]] +name = "parking_lot_core" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dc9e0dc2adc1c69d09143aff38d3d30c5c3f0df0dad82e6d25547af174ebec0" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys 0.42.0", +] + [[package]] name = "pathdiff" version = "0.2.1" @@ -1577,6 +1580,7 @@ dependencies = [ "graphql-test-helpers", "intern", "lazy_static", + "relay-schema", "relay-test-schema", "schema", "thiserror", @@ -1625,6 +1629,7 @@ version = "0.0.0" dependencies = [ "common", "intern", + "lazy_static", "schema", ] @@ -1658,7 +1663,7 @@ dependencies = [ "itertools", "lazy_static", "once_cell", - "parking_lot", + "parking_lot 0.11.2", "regex", "relay-config", "relay-test-schema", @@ -1682,6 +1687,7 @@ dependencies = [ "lazy_static", "relay-codegen", "relay-config", + "relay-schema", "relay-test-schema", "relay-transforms", "schema", @@ -1719,7 +1725,7 @@ dependencies = [ "io-lifetimes", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.36.1", ] [[package]] @@ -1984,20 +1990,9 @@ dependencies = [ [[package]] name = "socket2" -version = "0.3.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" -dependencies = [ - "cfg-if", - "libc", - "winapi", -] - -[[package]] -name = "socket2" -version = "0.4.0" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e3dfc207c526015c632472a77be09cf1b6e46866581aecae5cc38fb4235dea2" +checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd" dependencies = [ "libc", "winapi", @@ -2080,7 +2075,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8440c860cf79def6164e4a0a983bcc2305d82419177a0e0c71930d049e3ac5a1" dependencies = [ "rustix", - "windows-sys", + "windows-sys 0.36.1", ] [[package]] @@ -2140,19 +2135,20 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.15.0" +version = "1.21.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbbf1c778ec206785635ce8ad57fe52b3009ae9e0c9f574a728f3049d3e55838" +checksum = "a9e03c497dc955702ba729190dc4aac6f2a0ce97f913e5b1b5912fc5039d9099" dependencies = [ + "autocfg", "bytes 1.0.1", "libc", "memchr", "mio", "num_cpus", - "once_cell", - "parking_lot", + "parking_lot 0.12.1", "pin-project-lite", "signal-hook-registry", + "socket2", "tokio-macros", "tracing", "winapi", @@ -2347,6 +2343,12 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "watchman_client" version = "0.8.0" @@ -2402,43 +2404,100 @@ 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", + "windows_aarch64_msvc 0.36.1", + "windows_i686_gnu 0.36.1", + "windows_i686_msvc 0.36.1", + "windows_x86_64_gnu 0.36.1", + "windows_x86_64_msvc 0.36.1", ] +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc 0.42.0", + "windows_i686_gnu 0.42.0", + "windows_i686_msvc 0.42.0", + "windows_x86_64_gnu 0.42.0", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc 0.42.0", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" + [[package]] name = "windows_aarch64_msvc" version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" + [[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_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" + [[package]] name = "windows_i686_msvc" version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" +[[package]] +name = "windows_i686_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" + [[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_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" + [[package]] name = "windows_x86_64_msvc" version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" + [[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 4dee91e95cfc2..9683533de1430 100644 --- a/compiler/crates/common/src/feature_flags.rs +++ b/compiler/crates/common/src/feature_flags.rs @@ -76,6 +76,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 b2afc708b3697..c0c618134da9e 100644 --- a/compiler/crates/relay-compiler/tests/compile_relay_artifacts/mod.rs +++ b/compiler/crates/relay-compiler/tests/compile_relay_artifacts/mod.rs @@ -120,6 +120,7 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result { use_named_imports_for_relay_resolvers: false, relay_resolver_model_syntax_enabled: false, relay_resolver_enable_terse_syntax: false, + 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 9e9c93e0a1966..deb8cfa6986aa 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 @@ -90,6 +90,7 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result { use_named_imports_for_relay_resolvers: false, relay_resolver_model_syntax_enabled: false, relay_resolver_enable_terse_syntax: false, + 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 f00184c1df0c7..2c7ef855d421e 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");