Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying @relay_test_operation to write metadata for client fields #4047

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions compiler/crates/relay-transforms/src/test_operation_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,26 +224,22 @@ impl RelayTestOperationMetadata {
match selection {
Selection::ScalarField(scalar_field) => {
let field = schema.field(scalar_field.definition.item);
if !field.is_extension {
let alias_or_name = scalar_field.alias_or_name(schema);
let next_path = next_path(path, alias_or_name);
selection_type_info.insert(
next_path,
RelayTestOperationSelectionTypeInfo::new(schema, field),
);
}
let alias_or_name = scalar_field.alias_or_name(schema);
let next_path = next_path(path, alias_or_name);
selection_type_info.insert(
next_path,
RelayTestOperationSelectionTypeInfo::new(schema, field),
);
}
Selection::LinkedField(linked_field) => {
let field = schema.field(linked_field.definition.item);
if !field.is_extension {
let alias_or_name = linked_field.alias_or_name(schema);
let next_path = next_path(path, alias_or_name);
selection_type_info.insert(
next_path,
RelayTestOperationSelectionTypeInfo::new(schema, field),
);
processing_queue.push((Some(next_path), &linked_field.selections));
}
let alias_or_name = linked_field.alias_or_name(schema);
let next_path = next_path(path, alias_or_name);
selection_type_info.insert(
next_path,
RelayTestOperationSelectionTypeInfo::new(schema, field),
);
processing_queue.push((Some(next_path), &linked_field.selections));
}
Selection::Condition(condition) => {
processing_queue.push((path, &condition.selections));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ query ProdQuery @relay_test_operation {
id
}
}
# %extensions%
Original file line number Diff line number Diff line change
@@ -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"}}) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the new behavior with the feature flag enabled. This extra metadata will allow MockPayloadGenerator to auto-mock client fields when they're not provided in the associated resolver

node(id: "test-id") {
id
... on User {
name
client_info {
name
description
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ query QueryWitEnums @relay_test_operation {
}
}
}
# %extensions%
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ query SimpleQuery @relay_test_operation {
}
}
}
# %extensions%
51 changes: 46 additions & 5 deletions compiler/crates/relay-transforms/tests/relay_test_operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,55 @@
* 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;

pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
let test_path_regex = Some(Regex::new(r#"^test"#).unwrap());
apply_transform_for_test(fixture, |program| {
generate_test_operation_metadata(program, &test_path_regex)
})
let parts: Vec<_> = fixture.content.split("%extensions%").collect();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I referenced other mod.rs files in order to get this working


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)
.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::<Vec<_>>();
printed.sort();
Ok(printed.join("\n\n"))
} else {
panic!("Expected exactly one %extensions% section marker.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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;
Expand All @@ -19,6 +19,13 @@ 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_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, "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");
Expand Down