Skip to content

Commit 94f107a

Browse files
gordyffacebook-github-bot
authored andcommitted
required-on-nonnull-field checking to descend into inline fragments
Reviewed By: captbaritone Differential Revision: D77555111 fbshipit-source-id: e9c871235abe4899b12d9cf09ce158cc4d4d27da
1 parent cfd2ebd commit 94f107a

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

compiler/crates/relay-transforms/src/validations/disallow_required_on_non_null_field.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,15 @@ impl<'a> DisallowRequiredOnNonNullField<'a> {
134134
Selection::ScalarField(scalar_field) => {
135135
self.validate_required_field(scalar_field, errors_are_caught)
136136
}
137+
Selection::InlineFragment(fragment) => {
138+
self.validate_selection_fields(&fragment.selections, errors_are_caught)
139+
}
137140
_ => Ok(()),
138141
}))?;
139142
Ok(())
140143
}
141144
}
145+
142146
impl Validator for DisallowRequiredOnNonNullField<'_> {
143147
const NAME: &'static str = "disallow_required_on_non_null_field";
144148
const VALIDATE_ARGUMENTS: bool = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
==================================== INPUT ====================================
2+
fragment MyFragment on User @throwOnFieldError {
3+
... on User {
4+
some_field @required(action: THROW)
5+
}
6+
}
7+
8+
# %extensions%
9+
extend type User {
10+
some_field: Int!
11+
}
12+
==================================== OUTPUT ===================================
13+
OK; warnings: ℹ Unexpected `@required` directive on a non-null field. This field is already non-null and does not need the `@required` directive.
14+
15+
fragment_with_inline_fragment_required_non_null_fields.invalid.graphql:3:16
16+
2 │ ... on User {
17+
3 │ some_field @required(action: THROW)
18+
│ ^^^^^^^^^^^^^^^^^^^^^^^^
19+
4 │ }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fragment MyFragment on User @throwOnFieldError {
2+
... on User {
3+
some_field @required(action: THROW)
4+
}
5+
}
6+
7+
# %extensions%
8+
extend type User {
9+
some_field: Int!
10+
}

compiler/crates/relay-transforms/tests/disallow_required_on_non_null_field_test.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<12d7bd117492254376fcbcd30baf95f6>>
7+
* @generated SignedSource<<b7654cbc0db7924c3b85107ada9f49ba>>
88
*/
99

1010
mod disallow_required_on_non_null_field;
1111

1212
use disallow_required_on_non_null_field::transform_fixture;
1313
use fixture_tests::test_fixture;
1414

15+
#[tokio::test]
16+
async fn fragment_with_inline_fragment_required_non_null_fields_invalid() {
17+
let input = include_str!("disallow_required_on_non_null_field/fixtures/fragment_with_inline_fragment_required_non_null_fields.invalid.graphql");
18+
let expected = include_str!("disallow_required_on_non_null_field/fixtures/fragment_with_inline_fragment_required_non_null_fields.invalid.expected");
19+
test_fixture(transform_fixture, file!(), "fragment_with_inline_fragment_required_non_null_fields.invalid.graphql", "disallow_required_on_non_null_field/fixtures/fragment_with_inline_fragment_required_non_null_fields.invalid.expected", input, expected).await;
20+
}
21+
1522
#[tokio::test]
1623
async fn fragment_with_multiple_required_non_null_fields_invalid() {
1724
let input = include_str!("disallow_required_on_non_null_field/fixtures/fragment_with_multiple_required_non_null_fields.invalid.graphql");

0 commit comments

Comments
 (0)