Skip to content

Commit

Permalink
Run requried transform on IR before validating @required on semantic …
Browse files Browse the repository at this point in the history
…non null fields in LSP

Reviewed By: gordyf

Differential Revision: D67414659

fbshipit-source-id: 15eb3ea99729355cd7d00996c8e1c68ad6cc2230
  • Loading branch information
captbaritone authored and facebook-github-bot committed Dec 18, 2024
1 parent f115017 commit 911e083
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
3 changes: 1 addition & 2 deletions compiler/crates/relay-codemod/src/codemod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ pub async fn run_codemod(
}
}
AvailableCodemod::RemoveUnnecessaryRequiredDirectives => {
disallow_required_on_non_null_field(&programs.source.schema, &programs.source)
.unwrap_or_default()
disallow_required_on_non_null_field(&programs.source).unwrap_or_default()
}
})
.collect::<Vec<_>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ lazy_static! {
// refactor the places that run this transform to pre-apply all required
// transforms.
pub fn disallow_required_on_non_null_field(
schema: &Arc<SDLSchema>,
program: &Program,
) -> DiagnosticsResult<Vec<Diagnostic>> {
// This validation depends on metadata directives added by the
// required_directive transform. This validation is run on untransformed
// versions of IR both in the LSP and as a codemod, so we apply the transform
// ourselves locally.
let program = required_directive(program)?;
let mut validator = DisallowRequiredOnNonNullField::new(schema);
let mut validator = DisallowRequiredOnNonNullField::new(&program.schema);
validator.validate_program(&program)?;
Ok(validator.warnings)
}
Expand All @@ -62,13 +61,8 @@ pub fn disallow_required_on_non_null_field_for_executable_definition(
schema: &Arc<SDLSchema>,
definition: &ExecutableDefinition,
) -> DiagnosticsResult<Vec<Diagnostic>> {
let mut validator = DisallowRequiredOnNonNullField::new(schema);

match definition {
ExecutableDefinition::Fragment(fragment) => validator.validate_fragment(fragment),
ExecutableDefinition::Operation(operation) => validator.validate_operation(operation),
}?;
Ok(validator.warnings)
let program = Program::from_definitions(Arc::clone(schema), vec![definition.clone()]);
disallow_required_on_non_null_field(&program)
}

struct DisallowRequiredOnNonNullField<'a> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String>
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);
let results = disallow_required_on_non_null_field(&Arc::clone(&schema), &program)
let results = disallow_required_on_non_null_field(&program)
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?;

Ok(format!(
Expand Down

0 comments on commit 911e083

Please sign in to comment.