Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scalar codegen, and support for LinkedField as well
Reviewed By: captbaritone Differential Revision: D56643224 fbshipit-source-id: d595753fa5709473ae76b4a96f48ed6a3847cc7f
- Loading branch information
1 parent
470c814
commit f4f4c9e
Showing
40 changed files
with
841 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
compiler/crates/relay-codegen/tests/catch_directive_codegen.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
use std::sync::Arc; | ||
|
||
use common::SourceLocationKey; | ||
use fixture_tests::Fixture; | ||
use graphql_ir::build; | ||
use graphql_ir::Program; | ||
use graphql_syntax::parse_executable; | ||
use graphql_test_helpers::diagnostics_to_sorted_string; | ||
use relay_codegen::print_fragment; | ||
use relay_codegen::print_operation; | ||
use relay_codegen::JsModuleFormat; | ||
use relay_config::ProjectConfig; | ||
use relay_test_schema::get_test_schema; | ||
use relay_test_schema::get_test_schema_with_extensions; | ||
use relay_transforms::catch_directive; | ||
|
||
pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> { | ||
let parts: Vec<_> = fixture.content.split("%extensions%").collect(); | ||
let (base, schema) = match parts.as_slice() { | ||
[base, extensions] => (base, get_test_schema_with_extensions(extensions)), | ||
[base] => (base, get_test_schema()), | ||
_ => panic!("Invalid fixture input {}", fixture.content), | ||
}; | ||
|
||
let ast = parse_executable(base, SourceLocationKey::standalone(fixture.file_name)).unwrap(); | ||
let ir = build(&schema, &ast.definitions) | ||
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?; | ||
let program = Program::from_definitions(Arc::clone(&schema), ir); | ||
|
||
catch_directive(&program, true) | ||
.map(|next_program| { | ||
next_program | ||
.fragments() | ||
.map(|def| { | ||
let mut import_statements = Default::default(); | ||
let fragment = print_fragment( | ||
&schema, | ||
def, | ||
&ProjectConfig { | ||
js_module_format: JsModuleFormat::Haste, | ||
..Default::default() | ||
}, | ||
&mut import_statements, | ||
); | ||
format!("{}{}", import_statements, fragment) | ||
}) | ||
.chain(next_program.operations().map(|def| { | ||
let mut import_statements = Default::default(); | ||
let operation = print_operation( | ||
&schema, | ||
def, | ||
&ProjectConfig { | ||
js_module_format: JsModuleFormat::Haste, | ||
..Default::default() | ||
}, | ||
&mut import_statements, | ||
); | ||
format!("{}{}", import_statements, operation) | ||
})) | ||
.collect::<Vec<_>>() | ||
.join("\n\n") | ||
}) | ||
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics)) | ||
} |
35 changes: 35 additions & 0 deletions
35
...iler/crates/relay-codegen/tests/catch_directive_codegen/fixtures/catch_directive.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
==================================== INPUT ==================================== | ||
fragment MyFragment on Node { | ||
id | ||
name @catch(to: RESULT) | ||
} | ||
==================================== OUTPUT =================================== | ||
{ | ||
"argumentDefinitions": [], | ||
"kind": "Fragment", | ||
"metadata": null, | ||
"name": "MyFragment", | ||
"selections": [ | ||
{ | ||
"alias": null, | ||
"args": null, | ||
"kind": "ScalarField", | ||
"name": "id", | ||
"storageKey": null | ||
}, | ||
{ | ||
"kind": "CatchField", | ||
"field": { | ||
"alias": null, | ||
"args": null, | ||
"kind": "ScalarField", | ||
"name": "name", | ||
"storageKey": null | ||
}, | ||
"to": "RESULT", | ||
"path": "name" | ||
} | ||
], | ||
"type": "Node", | ||
"abstractKey": "__isNode" | ||
} |
4 changes: 4 additions & 0 deletions
4
compiler/crates/relay-codegen/tests/catch_directive_codegen/fixtures/catch_directive.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fragment MyFragment on Node { | ||
id | ||
name @catch(to: RESULT) | ||
} |
40 changes: 40 additions & 0 deletions
40
...ests/catch_directive_codegen/fixtures/catch_directive_linked_child_has_to_result.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
==================================== INPUT ==================================== | ||
fragment MyFragment on User { | ||
address { | ||
street @catch(to: RESULT) | ||
} | ||
} | ||
==================================== OUTPUT =================================== | ||
{ | ||
"argumentDefinitions": [], | ||
"kind": "Fragment", | ||
"metadata": null, | ||
"name": "MyFragment", | ||
"selections": [ | ||
{ | ||
"alias": null, | ||
"args": null, | ||
"concreteType": "StreetAddress", | ||
"kind": "LinkedField", | ||
"name": "address", | ||
"plural": false, | ||
"selections": [ | ||
{ | ||
"kind": "CatchField", | ||
"field": { | ||
"alias": null, | ||
"args": null, | ||
"kind": "ScalarField", | ||
"name": "street", | ||
"storageKey": null | ||
}, | ||
"to": "RESULT", | ||
"path": "address.street" | ||
} | ||
], | ||
"storageKey": null | ||
} | ||
], | ||
"type": "User", | ||
"abstractKey": null | ||
} |
5 changes: 5 additions & 0 deletions
5
...tests/catch_directive_codegen/fixtures/catch_directive_linked_child_has_to_result.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fragment MyFragment on User { | ||
address { | ||
street @catch(to: RESULT) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...-codegen/tests/catch_directive_codegen/fixtures/catch_directive_linked_to_result.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
==================================== INPUT ==================================== | ||
fragment MyFragment on User { | ||
address @catch(to: RESULT) { | ||
street | ||
} | ||
} | ||
==================================== OUTPUT =================================== | ||
{ | ||
"argumentDefinitions": [], | ||
"kind": "Fragment", | ||
"metadata": null, | ||
"name": "MyFragment", | ||
"selections": [ | ||
{ | ||
"kind": "CatchField", | ||
"field": { | ||
"alias": null, | ||
"args": null, | ||
"concreteType": "StreetAddress", | ||
"kind": "LinkedField", | ||
"name": "address", | ||
"plural": false, | ||
"selections": [ | ||
{ | ||
"alias": null, | ||
"args": null, | ||
"kind": "ScalarField", | ||
"name": "street", | ||
"storageKey": null | ||
} | ||
], | ||
"storageKey": null | ||
}, | ||
"to": "RESULT", | ||
"path": "address" | ||
} | ||
], | ||
"type": "User", | ||
"abstractKey": null | ||
} |
5 changes: 5 additions & 0 deletions
5
...y-codegen/tests/catch_directive_codegen/fixtures/catch_directive_linked_to_result.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fragment MyFragment on User { | ||
address @catch(to: RESULT) { | ||
street | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...elay-codegen/tests/catch_directive_codegen/fixtures/catch_directive_nested_catch.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
==================================== INPUT ==================================== | ||
fragment MyFragment on User { | ||
address @catch { | ||
street @catch | ||
} | ||
} | ||
==================================== OUTPUT =================================== | ||
{ | ||
"argumentDefinitions": [], | ||
"kind": "Fragment", | ||
"metadata": null, | ||
"name": "MyFragment", | ||
"selections": [ | ||
{ | ||
"kind": "CatchField", | ||
"field": { | ||
"alias": null, | ||
"args": null, | ||
"concreteType": "StreetAddress", | ||
"kind": "LinkedField", | ||
"name": "address", | ||
"plural": false, | ||
"selections": [ | ||
{ | ||
"kind": "CatchField", | ||
"field": { | ||
"alias": null, | ||
"args": null, | ||
"kind": "ScalarField", | ||
"name": "street", | ||
"storageKey": null | ||
}, | ||
"to": "RESULT", | ||
"path": "address.street" | ||
} | ||
], | ||
"storageKey": null | ||
}, | ||
"to": "RESULT", | ||
"path": "address" | ||
} | ||
], | ||
"type": "User", | ||
"abstractKey": null | ||
} |
5 changes: 5 additions & 0 deletions
5
...relay-codegen/tests/catch_directive_codegen/fixtures/catch_directive_nested_catch.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fragment MyFragment on User { | ||
address @catch { | ||
street @catch | ||
} | ||
} |
Oops, something went wrong.