From b011dc19aabf2788202e22ded19b6f08d217dd4c Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Sun, 19 Oct 2025 21:07:14 +0200 Subject: [PATCH] Add parent type rule to GraphQL schema --- app/graphql/types/data_type_rule_type.rb | 12 +++++++++++- app/graphql/types/data_type_rules/config_type.rb | 6 ++++-- .../data_type_rules/contains_key_config_type.rb | 2 -- .../data_type_rules/contains_type_config_type.rb | 2 -- .../data_type_rules/input_type_config_type.rb | 2 -- .../data_type_rules/input_types_config_type.rb | 2 -- .../item_of_collection_config_type.rb | 2 -- .../data_type_rules/number_range_config_type.rb | 2 -- .../data_type_rules/parent_type_config_type.rb | 16 ++++++++++++++++ .../types/data_type_rules/regex_config_type.rb | 2 -- .../data_type_rules/return_type_config_type.rb | 2 -- .../types/data_type_rules/variant_enum.rb | 2 ++ app/policies/data_type_rule_policy.rb | 5 +++++ docs/graphql/enum/datatyperulesvariant.md | 1 + .../object/datatyperulesparenttypeconfig.md | 12 ++++++++++++ docs/graphql/union/datatyperulesconfig.md | 1 + 16 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 app/graphql/types/data_type_rules/parent_type_config_type.rb create mode 100644 app/policies/data_type_rule_policy.rb create mode 100644 docs/graphql/object/datatyperulesparenttypeconfig.md diff --git a/app/graphql/types/data_type_rule_type.rb b/app/graphql/types/data_type_rule_type.rb index 9a52c604..a02f18e8 100644 --- a/app/graphql/types/data_type_rule_type.rb +++ b/app/graphql/types/data_type_rule_type.rb @@ -4,6 +4,8 @@ module Types class DataTypeRuleType < Types::BaseObject description 'Represents a rule that can be applied to a data type.' + authorize :read_datatype + field :variant, Types::DataTypeRules::VariantEnum, null: false, description: 'The type of the rule' field :config, Types::DataTypeRules::ConfigType, null: false, @@ -12,8 +14,16 @@ class DataTypeRuleType < Types::BaseObject id_field ::DataTypeRule timestamps + def variant + object.variant.to_sym + end + def config - object.config.merge(variant: object.variant) + if object.variant_parent_type? + object + else + object.config.merge(variant: object.variant) + end end end end diff --git a/app/graphql/types/data_type_rules/config_type.rb b/app/graphql/types/data_type_rules/config_type.rb index 72a35ebf..a1b5a8f0 100644 --- a/app/graphql/types/data_type_rules/config_type.rb +++ b/app/graphql/types/data_type_rules/config_type.rb @@ -6,10 +6,10 @@ class ConfigType < Types::BaseUnion description 'Represents a rule that can be applied to a data type.' possible_types ContainsKeyConfigType, ContainsTypeConfigType, NumberRangeConfigType, ItemOfCollectionConfigType, - RegexConfigType, InputTypesConfigType, ReturnTypeConfigType + RegexConfigType, InputTypesConfigType, ReturnTypeConfigType, ParentTypeConfigType def self.resolve_type(object, _context) - case object[:variant] + case object[:variant].to_sym when :contains_key Types::DataTypeRules::ContainsKeyConfigType when :contains_type @@ -24,6 +24,8 @@ def self.resolve_type(object, _context) Types::DataTypeRules::InputTypesConfigType when :return_type Types::DataTypeRules::ReturnTypeConfigType + when :parent_type + Types::DataTypeRules::ParentTypeConfigType else raise GraphQL::ExecutionError, "Unknown data type rule variant: #{object[:variant]}" end diff --git a/app/graphql/types/data_type_rules/contains_key_config_type.rb b/app/graphql/types/data_type_rules/contains_key_config_type.rb index be87ff93..f858b3a8 100644 --- a/app/graphql/types/data_type_rules/contains_key_config_type.rb +++ b/app/graphql/types/data_type_rules/contains_key_config_type.rb @@ -5,8 +5,6 @@ module DataTypeRules class ContainsKeyConfigType < Types::BaseObject description 'Represents a rule that can be applied to a data type.' - authorize :read_datatype - field :data_type_identifier, Types::DataTypeIdentifierType, null: false, description: 'The identifier of the data type this rule belongs to' field :key, String, null: false, description: 'The key of the rule' diff --git a/app/graphql/types/data_type_rules/contains_type_config_type.rb b/app/graphql/types/data_type_rules/contains_type_config_type.rb index 6de24033..7fbc240a 100644 --- a/app/graphql/types/data_type_rules/contains_type_config_type.rb +++ b/app/graphql/types/data_type_rules/contains_type_config_type.rb @@ -5,8 +5,6 @@ module DataTypeRules class ContainsTypeConfigType < Types::BaseObject description 'Represents a rule that can be applied to a data type.' - authorize :read_datatype - field :data_type_identifier, Types::DataTypeIdentifierType, null: false, description: 'The identifier of the data type this rule belongs to' end diff --git a/app/graphql/types/data_type_rules/input_type_config_type.rb b/app/graphql/types/data_type_rules/input_type_config_type.rb index c92262c0..4f43d492 100644 --- a/app/graphql/types/data_type_rules/input_type_config_type.rb +++ b/app/graphql/types/data_type_rules/input_type_config_type.rb @@ -5,8 +5,6 @@ module DataTypeRules class InputTypeConfigType < Types::BaseObject description 'Represents a subtype of input type configuration for a input data type.' - authorize :read_datatype - field :data_type_identifier, Types::DataTypeIdentifierType, null: false, description: 'The identifier of the data type this input type belongs to' diff --git a/app/graphql/types/data_type_rules/input_types_config_type.rb b/app/graphql/types/data_type_rules/input_types_config_type.rb index 7e68191a..f5246eb7 100644 --- a/app/graphql/types/data_type_rules/input_types_config_type.rb +++ b/app/graphql/types/data_type_rules/input_types_config_type.rb @@ -5,8 +5,6 @@ module DataTypeRules class InputTypesConfigType < Types::BaseObject description 'Represents a rule that can be applied to a data type.' - authorize :read_datatype - field :input_types, [Types::DataTypeRules::InputTypeConfigType], null: false, description: 'The input types that can be used in this data type rule' end diff --git a/app/graphql/types/data_type_rules/item_of_collection_config_type.rb b/app/graphql/types/data_type_rules/item_of_collection_config_type.rb index 06c59a1e..523f5937 100644 --- a/app/graphql/types/data_type_rules/item_of_collection_config_type.rb +++ b/app/graphql/types/data_type_rules/item_of_collection_config_type.rb @@ -5,8 +5,6 @@ module DataTypeRules class ItemOfCollectionConfigType < Types::BaseObject description 'Represents a rule that can be applied to a data type.' - authorize :read_datatype - field :items, [GraphQL::Types::JSON], null: true, description: 'The items that can be configured for this rule.' end diff --git a/app/graphql/types/data_type_rules/number_range_config_type.rb b/app/graphql/types/data_type_rules/number_range_config_type.rb index d7c37008..54bcfc1b 100644 --- a/app/graphql/types/data_type_rules/number_range_config_type.rb +++ b/app/graphql/types/data_type_rules/number_range_config_type.rb @@ -5,8 +5,6 @@ module DataTypeRules class NumberRangeConfigType < Types::BaseObject description 'Represents a rule that can be applied to a data type.' - authorize :read_datatype - field :from, Integer, null: false, description: 'The minimum value of the range' field :steps, Integer, null: true, diff --git a/app/graphql/types/data_type_rules/parent_type_config_type.rb b/app/graphql/types/data_type_rules/parent_type_config_type.rb new file mode 100644 index 00000000..fe5a06fb --- /dev/null +++ b/app/graphql/types/data_type_rules/parent_type_config_type.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Types + module DataTypeRules + class ParentTypeConfigType < Types::BaseObject + description 'Represents a rule that can be applied to a data type.' + + field :data_type_identifier, Types::DataTypeIdentifierType, + null: false, description: 'The data type identifier for the parent type.' + + def data_type_identifier + object.data_type.parent_type + end + end + end +end diff --git a/app/graphql/types/data_type_rules/regex_config_type.rb b/app/graphql/types/data_type_rules/regex_config_type.rb index 87309c85..4b161dfe 100644 --- a/app/graphql/types/data_type_rules/regex_config_type.rb +++ b/app/graphql/types/data_type_rules/regex_config_type.rb @@ -5,8 +5,6 @@ module DataTypeRules class RegexConfigType < Types::BaseObject description 'Represents a rule that can be applied to a data type.' - authorize :read_datatype - field :pattern, String, null: false, description: 'The regex pattern to match against the data type value.' end diff --git a/app/graphql/types/data_type_rules/return_type_config_type.rb b/app/graphql/types/data_type_rules/return_type_config_type.rb index 9482c1c6..1532aa55 100644 --- a/app/graphql/types/data_type_rules/return_type_config_type.rb +++ b/app/graphql/types/data_type_rules/return_type_config_type.rb @@ -5,8 +5,6 @@ module DataTypeRules class ReturnTypeConfigType < Types::BaseObject description 'Represents a rule that can be applied to a data type.' - authorize :read_datatype - field :data_type_identifier, Types::DataTypeIdentifierType, null: false, description: 'The data type identifier for the return type.' end diff --git a/app/graphql/types/data_type_rules/variant_enum.rb b/app/graphql/types/data_type_rules/variant_enum.rb index 88f162ee..b0b33157 100644 --- a/app/graphql/types/data_type_rules/variant_enum.rb +++ b/app/graphql/types/data_type_rules/variant_enum.rb @@ -19,6 +19,8 @@ class VariantEnum < Types::BaseEnum value: :input_type value :RETURN_TYPE, 'The rule checks if the data type matches a specific return type.', value: :return_type + value :PARENT_TYPE, 'The rule checks if the data type is a child of a specific parent type.', + value: :parent_type end end end diff --git a/app/policies/data_type_rule_policy.rb b/app/policies/data_type_rule_policy.rb new file mode 100644 index 00000000..8bb40a9f --- /dev/null +++ b/app/policies/data_type_rule_policy.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class DataTypeRulePolicy < BasePolicy + delegate { subject.data_type } +end diff --git a/docs/graphql/enum/datatyperulesvariant.md b/docs/graphql/enum/datatyperulesvariant.md index 0c62ebca..e5b90ef1 100644 --- a/docs/graphql/enum/datatyperulesvariant.md +++ b/docs/graphql/enum/datatyperulesvariant.md @@ -11,5 +11,6 @@ The type of rule that can be applied to a data type. | `INPUT_TYPE` | The rule checks if the data type matches a specific input type. | | `ITEM_OF_COLLECTION` | The rule checks if an item is part of a collection in the data type. | | `NUMBER_RANGE` | The rule checks if a number falls within a specified range. | +| `PARENT_TYPE` | The rule checks if the data type is a child of a specific parent type. | | `REGEX` | The rule checks if a string matches a specified regular expression. | | `RETURN_TYPE` | The rule checks if the data type matches a specific return type. | diff --git a/docs/graphql/object/datatyperulesparenttypeconfig.md b/docs/graphql/object/datatyperulesparenttypeconfig.md new file mode 100644 index 00000000..01b1e613 --- /dev/null +++ b/docs/graphql/object/datatyperulesparenttypeconfig.md @@ -0,0 +1,12 @@ +--- +title: DataTypeRulesParentTypeConfig +--- + +Represents a rule that can be applied to a data type. + +## Fields without arguments + +| Name | Type | Description | +|------|------|-------------| +| `dataTypeIdentifier` | [`DataTypeIdentifier!`](../object/datatypeidentifier.md) | The data type identifier for the parent type. | + diff --git a/docs/graphql/union/datatyperulesconfig.md b/docs/graphql/union/datatyperulesconfig.md index ed9cdbb4..d696d442 100644 --- a/docs/graphql/union/datatyperulesconfig.md +++ b/docs/graphql/union/datatyperulesconfig.md @@ -11,5 +11,6 @@ Represents a rule that can be applied to a data type. - [`DataTypeRulesInputTypesConfig`](../object/datatyperulesinputtypesconfig.md) - [`DataTypeRulesItemOfCollectionConfig`](../object/datatyperulesitemofcollectionconfig.md) - [`DataTypeRulesNumberRangeConfig`](../object/datatyperulesnumberrangeconfig.md) +- [`DataTypeRulesParentTypeConfig`](../object/datatyperulesparenttypeconfig.md) - [`DataTypeRulesRegexConfig`](../object/datatyperulesregexconfig.md) - [`DataTypeRulesReturnTypeConfig`](../object/datatyperulesreturntypeconfig.md)