|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +use common::Diagnostic; |
| 9 | +use common::DiagnosticsResult; |
| 10 | +use common::FeatureFlag; |
| 11 | +use common::NamedItem; |
| 12 | +use docblock_shared::RELAY_RESOLVER_DIRECTIVE_NAME; |
| 13 | +use graphql_ir::ConstantValue; |
| 14 | +use graphql_ir::Field; |
| 15 | +use graphql_ir::FragmentDefinition; |
| 16 | +use graphql_ir::FragmentSpread; |
| 17 | +use graphql_ir::LinkedField; |
| 18 | +use graphql_ir::OperationDefinition; |
| 19 | +use graphql_ir::Program; |
| 20 | +use graphql_ir::ScalarField; |
| 21 | +use graphql_ir::ValidationMessage; |
| 22 | +use graphql_ir::Validator; |
| 23 | +use schema::Schema; |
| 24 | + |
| 25 | +use crate::ACTION_ARGUMENT; |
| 26 | +use crate::REQUIRED_DIRECTIVE_NAME; |
| 27 | +use crate::THROW_ACTION; |
| 28 | + |
| 29 | +/// Some Relay features will cause a field to throw or suspend at read time. |
| 30 | +/// These behaviors are incompatible with our mutation APIs. |
| 31 | +/// This validator checks that no such features are used in mutations. |
| 32 | +pub fn disallow_readtime_features_in_mutations( |
| 33 | + program: &Program, |
| 34 | + allow_resolvers_mutation_response: &FeatureFlag, |
| 35 | + allow_required_in_mutation_response: &FeatureFlag, |
| 36 | + enable_relay_resolver_mutations: bool, |
| 37 | +) -> DiagnosticsResult<()> { |
| 38 | + let mut validator = DisallowReadtimeFeaturesInMutations::new( |
| 39 | + program, |
| 40 | + allow_resolvers_mutation_response.clone(), |
| 41 | + allow_required_in_mutation_response.clone(), |
| 42 | + enable_relay_resolver_mutations, |
| 43 | + ); |
| 44 | + validator.validate_program(program) |
| 45 | +} |
| 46 | + |
| 47 | +struct DisallowReadtimeFeaturesInMutations<'program> { |
| 48 | + program: &'program Program, |
| 49 | + allow_resolvers_mutation_response: FeatureFlag, |
| 50 | + allow_required_in_mutation_response: FeatureFlag, |
| 51 | + enable_relay_resolver_mutations: bool, |
| 52 | + allow_resolvers_for_this_mutation: bool, |
| 53 | + allow_required_for_this_mutation: bool, |
| 54 | +} |
| 55 | + |
| 56 | +impl<'program> DisallowReadtimeFeaturesInMutations<'program> { |
| 57 | + fn new( |
| 58 | + program: &'program Program, |
| 59 | + allow_resolvers_mutation_response: FeatureFlag, |
| 60 | + allow_required_in_mutation_response: FeatureFlag, |
| 61 | + enable_relay_resolver_mutations: bool, |
| 62 | + ) -> Self { |
| 63 | + Self { |
| 64 | + program, |
| 65 | + allow_resolvers_mutation_response, |
| 66 | + allow_required_in_mutation_response, |
| 67 | + enable_relay_resolver_mutations, |
| 68 | + allow_resolvers_for_this_mutation: false, |
| 69 | + allow_required_for_this_mutation: false, |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + fn validate_field(&self, field: &impl Field) -> DiagnosticsResult<()> { |
| 74 | + if !self.allow_required_for_this_mutation { |
| 75 | + if let Some(directive) = field.directives().named(*REQUIRED_DIRECTIVE_NAME) { |
| 76 | + let action = directive |
| 77 | + .arguments |
| 78 | + .named(*ACTION_ARGUMENT) |
| 79 | + .and_then(|arg| arg.value.item.get_constant()); |
| 80 | + if let Some(ConstantValue::Enum(action)) = action { |
| 81 | + if *action == *THROW_ACTION { |
| 82 | + return Err(vec![Diagnostic::error( |
| 83 | + ValidationMessage::RequiredInMutation, |
| 84 | + directive.name.location, |
| 85 | + )]); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + if !self.allow_resolvers_for_this_mutation |
| 91 | + && self |
| 92 | + .program |
| 93 | + .schema |
| 94 | + .field(field.definition().item) |
| 95 | + .directives |
| 96 | + .named(*RELAY_RESOLVER_DIRECTIVE_NAME) |
| 97 | + .is_some() |
| 98 | + { |
| 99 | + return Err(vec![Diagnostic::error( |
| 100 | + ValidationMessage::ResolverInMutation, |
| 101 | + field.alias_or_name_location(), |
| 102 | + )]); |
| 103 | + } |
| 104 | + |
| 105 | + Ok(()) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl Validator for DisallowReadtimeFeaturesInMutations<'_> { |
| 110 | + const NAME: &'static str = "disallow_readtime_features_in_mutations"; |
| 111 | + const VALIDATE_ARGUMENTS: bool = false; |
| 112 | + const VALIDATE_DIRECTIVES: bool = false; |
| 113 | + |
| 114 | + fn validate_operation(&mut self, operation: &OperationDefinition) -> DiagnosticsResult<()> { |
| 115 | + if !operation.is_mutation() { |
| 116 | + // No need to traverse into non-mutation operations |
| 117 | + return Ok(()); |
| 118 | + } |
| 119 | + self.allow_resolvers_for_this_mutation = self.enable_relay_resolver_mutations |
| 120 | + || self |
| 121 | + .allow_resolvers_mutation_response |
| 122 | + .is_enabled_for(operation.name.item.0); |
| 123 | + self.allow_required_for_this_mutation = self |
| 124 | + .allow_required_in_mutation_response |
| 125 | + .is_enabled_for(operation.name.item.0); |
| 126 | + let result = self.default_validate_operation(operation); |
| 127 | + |
| 128 | + // Reset state |
| 129 | + self.allow_resolvers_for_this_mutation = false; |
| 130 | + self.allow_required_for_this_mutation = false; |
| 131 | + |
| 132 | + result |
| 133 | + } |
| 134 | + |
| 135 | + fn validate_fragment(&mut self, _fragment: &FragmentDefinition) -> DiagnosticsResult<()> { |
| 136 | + // We only care about mutations |
| 137 | + Ok(()) |
| 138 | + } |
| 139 | + |
| 140 | + fn validate_fragment_spread(&mut self, _spread: &FragmentSpread) -> DiagnosticsResult<()> { |
| 141 | + // Values nested within fragment spreads are fine since they are not read as part of the |
| 142 | + // mutation response. |
| 143 | + Ok(()) |
| 144 | + } |
| 145 | + |
| 146 | + fn validate_scalar_field(&mut self, field: &ScalarField) -> DiagnosticsResult<()> { |
| 147 | + self.validate_field(field) |
| 148 | + } |
| 149 | + fn validate_linked_field(&mut self, field: &LinkedField) -> DiagnosticsResult<()> { |
| 150 | + self.validate_field(field)?; |
| 151 | + self.default_validate_linked_field(field) |
| 152 | + } |
| 153 | +} |
0 commit comments