-
Notifications
You must be signed in to change notification settings - Fork 329
Implement demand control directive validations #7376
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2a41ace
Implement validators for cost and listSize applications
tninesling b04f107
Merge branch 'dev' into tninesling/cost-validations
tninesling 4d79343
Merge remote-tracking branch 'origin/dev' into tninesling/cost-valida…
tninesling a8864c3
Enable cost and listSize validation tests
tninesling 6b90602
Merge branch 'dev' into tninesling/cost-validations
tninesling 1edd707
Add argument merge strategies for cost and listSize; remove prints
tninesling be3b19c
Merge remote-tracking branch 'origin/dev' into tninesling/cost-valida…
tninesling 3219dfa
Use object_or_interface_fields utility
tninesling c85340a
Merge branch 'dev' into tninesling/cost-validations
tninesling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,16 +1,19 @@ | ||
| use std::collections::HashSet; | ||
| use std::sync::LazyLock; | ||
|
|
||
| use apollo_compiler::Name; | ||
| use apollo_compiler::Node; | ||
| use apollo_compiler::ast::Argument; | ||
| use apollo_compiler::ast::Directive; | ||
| use apollo_compiler::ast::DirectiveList; | ||
| use apollo_compiler::ast::DirectiveLocation; | ||
| use apollo_compiler::ast::FieldDefinition; | ||
| use apollo_compiler::ast::InputValueDefinition; | ||
| use apollo_compiler::name; | ||
| use apollo_compiler::schema::Component; | ||
| use apollo_compiler::schema::ExtendedType; | ||
| use apollo_compiler::schema::Value; | ||
| use apollo_compiler::ty; | ||
| use indexmap::IndexSet; | ||
|
|
||
| use crate::error::FederationError; | ||
| use crate::internal_error; | ||
|
|
@@ -21,9 +24,13 @@ use crate::link::spec::Version; | |
| use crate::link::spec_definition::SpecDefinition; | ||
| use crate::link::spec_definition::SpecDefinitions; | ||
| use crate::schema::FederationSchema; | ||
| use crate::schema::argument_composition_strategies::ArgumentCompositionStrategy; | ||
| use crate::schema::position::EnumTypeDefinitionPosition; | ||
| use crate::schema::position::ObjectTypeDefinitionPosition; | ||
| use crate::schema::position::ScalarTypeDefinitionPosition; | ||
| use crate::schema::type_and_directive_specification::ArgumentSpecification; | ||
| use crate::schema::type_and_directive_specification::DirectiveArgumentSpecification; | ||
| use crate::schema::type_and_directive_specification::DirectiveSpecification; | ||
| use crate::schema::type_and_directive_specification::TypeAndDirectiveSpecification; | ||
|
|
||
| const COST_DIRECTIVE_NAME: Name = name!("cost"); | ||
|
|
@@ -171,7 +178,9 @@ impl CostSpecDefinition { | |
| /// Returns the name of the `@cost` directive in the given schema, accounting for import aliases or specification name | ||
| /// prefixes such as `@federation__cost`. This checks the linked cost specification, if there is one, and falls back | ||
| /// to the federation spec. | ||
| fn cost_directive_name(schema: &FederationSchema) -> Result<Option<Name>, FederationError> { | ||
| pub(crate) fn cost_directive_name( | ||
| schema: &FederationSchema, | ||
| ) -> Result<Option<Name>, FederationError> { | ||
| if let Some(spec) = Self::for_federation_schema(schema) { | ||
| spec.directive_name_in_schema(schema, &COST_DIRECTIVE_NAME) | ||
| } else if let Ok(fed_spec) = get_federation_spec_definition_from_subgraph(schema) { | ||
|
|
@@ -184,7 +193,7 @@ impl CostSpecDefinition { | |
| /// Returns the name of the `@listSize` directive in the given schema, accounting for import aliases or specification name | ||
| /// prefixes such as `@federation__listSize`. This checks the linked cost specification, if there is one, and falls back | ||
| /// to the federation spec. | ||
| fn list_size_directive_name( | ||
| pub(crate) fn list_size_directive_name( | ||
| schema: &FederationSchema, | ||
| ) -> Result<Option<Name>, FederationError> { | ||
| if let Some(spec) = Self::for_federation_schema(schema) { | ||
|
|
@@ -235,6 +244,79 @@ impl CostSpecDefinition { | |
| Ok(None) | ||
| } | ||
| } | ||
|
|
||
| fn cost_directive_specification() -> DirectiveSpecification { | ||
| DirectiveSpecification::new( | ||
| COST_DIRECTIVE_NAME, | ||
| &[DirectiveArgumentSpecification { | ||
| base_spec: ArgumentSpecification { | ||
| name: COST_DIRECTIVE_WEIGHT_ARGUMENT_NAME, | ||
| get_type: |_, _| Ok(ty!(Int!)), | ||
| default_value: None, | ||
| }, | ||
| composition_strategy: Some(ArgumentCompositionStrategy::Max), | ||
| }], | ||
| false, | ||
| &[ | ||
| DirectiveLocation::ArgumentDefinition, | ||
| DirectiveLocation::Enum, | ||
| DirectiveLocation::FieldDefinition, | ||
| DirectiveLocation::InputFieldDefinition, | ||
| DirectiveLocation::Object, | ||
| DirectiveLocation::Scalar, | ||
| ], | ||
| false, | ||
| // TODO: Set up supergraph spec later, but the type is hard to work with at the moment | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be handled in a follow-up ticket |
||
| None, | ||
| None, | ||
| ) | ||
| } | ||
|
|
||
| fn list_size_directive_specification() -> DirectiveSpecification { | ||
| DirectiveSpecification::new( | ||
| LIST_SIZE_DIRECTIVE_NAME, | ||
| &[ | ||
| DirectiveArgumentSpecification { | ||
| base_spec: ArgumentSpecification { | ||
| name: LIST_SIZE_DIRECTIVE_ASSUMED_SIZE_ARGUMENT_NAME, | ||
| get_type: |_, _| Ok(ty!(Int)), | ||
| default_value: None, | ||
| }, | ||
| composition_strategy: Some(ArgumentCompositionStrategy::Max), | ||
| }, | ||
| DirectiveArgumentSpecification { | ||
| base_spec: ArgumentSpecification { | ||
| name: LIST_SIZE_DIRECTIVE_SLICING_ARGUMENTS_ARGUMENT_NAME, | ||
| get_type: |_, _| Ok(ty!([String!])), | ||
| default_value: None, | ||
| }, | ||
| composition_strategy: Some(ArgumentCompositionStrategy::Union), | ||
| }, | ||
| DirectiveArgumentSpecification { | ||
| base_spec: ArgumentSpecification { | ||
| name: LIST_SIZE_DIRECTIVE_SIZED_FIELDS_ARGUMENT_NAME, | ||
| get_type: |_, _| Ok(ty!([String!])), | ||
| default_value: None, | ||
| }, | ||
| composition_strategy: Some(ArgumentCompositionStrategy::Union), | ||
| }, | ||
| DirectiveArgumentSpecification { | ||
| base_spec: ArgumentSpecification { | ||
| name: LIST_SIZE_DIRECTIVE_REQUIRE_ONE_SLICING_ARGUMENT_ARGUMENT_NAME, | ||
| get_type: |_, _| Ok(ty!(Boolean)), | ||
| default_value: Some(Value::Boolean(true)), | ||
| }, | ||
| composition_strategy: Some(ArgumentCompositionStrategy::Max), | ||
| }, | ||
| ], | ||
| false, | ||
| &[DirectiveLocation::FieldDefinition], | ||
| false, | ||
tninesling marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // TODO: Set up supergraph spec later, but the type is hard to work with at the moment | ||
| None, | ||
| None, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl SpecDefinition for CostSpecDefinition { | ||
|
|
@@ -243,11 +325,14 @@ impl SpecDefinition for CostSpecDefinition { | |
| } | ||
|
|
||
| fn directive_specs(&self) -> Vec<Box<dyn TypeAndDirectiveSpecification>> { | ||
| todo!() | ||
| vec![ | ||
| Box::new(Self::cost_directive_specification()), | ||
| Box::new(Self::list_size_directive_specification()), | ||
| ] | ||
| } | ||
|
|
||
| fn type_specs(&self) -> Vec<Box<dyn TypeAndDirectiveSpecification>> { | ||
| todo!() | ||
| vec![] | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -267,15 +352,18 @@ impl CostDirective { | |
| self.weight as f64 | ||
| } | ||
|
|
||
| fn from_directives(directive_name: &Name, directives: &DirectiveList) -> Option<Self> { | ||
| pub(crate) fn from_directives( | ||
| directive_name: &Name, | ||
| directives: &DirectiveList, | ||
| ) -> Option<Self> { | ||
| directives | ||
| .get(directive_name)? | ||
| .specified_argument_by_name(&COST_DIRECTIVE_WEIGHT_ARGUMENT_NAME)? | ||
| .to_i32() | ||
| .map(|weight| Self { weight }) | ||
| } | ||
|
|
||
| fn from_schema_directives( | ||
| pub(crate) fn from_schema_directives( | ||
| directive_name: &Name, | ||
| directives: &apollo_compiler::schema::DirectiveList, | ||
| ) -> Option<Self> { | ||
|
|
@@ -289,8 +377,8 @@ impl CostDirective { | |
|
|
||
| pub struct ListSizeDirective { | ||
| pub assumed_size: Option<i32>, | ||
| pub slicing_argument_names: Option<HashSet<String>>, | ||
| pub sized_fields: Option<HashSet<String>>, | ||
| pub slicing_argument_names: Option<IndexSet<String>>, | ||
| pub sized_fields: Option<IndexSet<String>>, | ||
| pub require_one_slicing_argument: bool, | ||
| } | ||
|
|
||
|
|
@@ -320,7 +408,7 @@ impl ListSizeDirective { | |
| .to_i32() | ||
| } | ||
|
|
||
| fn slicing_argument_names(directive: &Directive) -> Option<HashSet<String>> { | ||
| fn slicing_argument_names(directive: &Directive) -> Option<IndexSet<String>> { | ||
| let names = directive | ||
| .specified_argument_by_name(&LIST_SIZE_DIRECTIVE_SLICING_ARGUMENTS_ARGUMENT_NAME)? | ||
| .as_list()? | ||
|
|
@@ -331,7 +419,7 @@ impl ListSizeDirective { | |
| Some(names) | ||
| } | ||
|
|
||
| fn sized_fields(directive: &Directive) -> Option<HashSet<String>> { | ||
| fn sized_fields(directive: &Directive) -> Option<IndexSet<String>> { | ||
| let fields = directive | ||
| .specified_argument_by_name(&LIST_SIZE_DIRECTIVE_SIZED_FIELDS_ARGUMENT_NAME)? | ||
| .as_list()? | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.