Skip to content

Commit d7b5663

Browse files
authored
Merge pull request #550 from code0-tech/386-query-functions-and-parameters
Add query for function and parameter definitions
2 parents 1a51864 + bb04aa2 commit d7b5663

25 files changed

+284
-4
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Types
4+
class FunctionDefinitionType < Types::BaseObject
5+
description 'Represents a function definition'
6+
7+
authorize :read_function_definition
8+
9+
field :return_type, Types::DataTypeIdentifierType, null: true, description: 'Return type of the function'
10+
11+
field :parameter_definitions, Types::ParameterDefinitionType.connection_type,
12+
null: true,
13+
description: 'Parameters of the function'
14+
15+
field :descriptions, Types::TranslationType.connection_type, null: true, description: 'Description of the function'
16+
field :names, Types::TranslationType.connection_type, null: true, description: 'Name of the function'
17+
18+
field :documentations, Types::TranslationType.connection_type,
19+
null: true,
20+
description: 'Documentation of the function'
21+
22+
id_field FunctionDefinition
23+
timestamps
24+
end
25+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
module Types
4+
class ParameterDefinitionType < Types::BaseObject
5+
description 'Represents a parameter definition'
6+
7+
authorize :read_parameter_definition
8+
9+
field :data_type, Types::DataTypeIdentifierType, null: true, description: 'Data type of the parameter'
10+
11+
field :descriptions, Types::TranslationType.connection_type, null: true, description: 'Description of the parameter'
12+
field :names, Types::TranslationType.connection_type, null: true, description: 'Name of the parameter'
13+
14+
field :documentations, Types::TranslationType.connection_type,
15+
null: true,
16+
description: 'Documentation of the parameter'
17+
18+
id_field ParameterDefinition
19+
timestamps
20+
end
21+
end

app/graphql/types/runtime_function_definition_type.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class RuntimeFunctionDefinitionType < Types::BaseObject
66

77
authorize :read_flow
88

9+
field :function_definitions, Types::FunctionDefinitionType.connection_type,
10+
null: true,
11+
description: 'Function definitions of the Node Function'
12+
913
id_field RuntimeParameterDefinition
1014
timestamps
1115
end

app/models/function_definition.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class FunctionDefinition < ApplicationRecord
44
belongs_to :runtime_function_definition
55
belongs_to :return_type, class_name: 'DataTypeIdentifier', optional: true
66

7+
has_many :parameter_definitions, inverse_of: :function_definition
8+
79
has_many :names, -> { by_purpose(:name) }, class_name: 'Translation', as: :owner, inverse_of: :owner
810
has_many :descriptions, -> { by_purpose(:description) }, class_name: 'Translation', as: :owner, inverse_of: :owner
911
has_many :documentations, -> { by_purpose(:documentation) }, class_name: 'Translation', as: :owner, inverse_of: :owner

app/models/parameter_definition.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,19 @@ class ParameterDefinition < ApplicationRecord
44
belongs_to :runtime_parameter_definition
55
belongs_to :data_type, class_name: 'DataTypeIdentifier'
66

7+
belongs_to :function_definition, inverse_of: :parameter_definitions
8+
79
has_many :names, -> { by_purpose(:name) }, class_name: 'Translation', as: :owner, inverse_of: :owner
810
has_many :descriptions, -> { by_purpose(:description) }, class_name: 'Translation', as: :owner, inverse_of: :owner
911
has_many :documentations, -> { by_purpose(:documentation) }, class_name: 'Translation', as: :owner, inverse_of: :owner
12+
13+
validate :function_definition_matches_definition
14+
15+
def function_definition_matches_definition
16+
parameter_function_definition_id = runtime_parameter_definition&.runtime_function_definition_id
17+
function_definition_id = function_definition&.runtime_function_definition_id
18+
return if parameter_function_definition_id == function_definition_id
19+
20+
errors.add(:function_definition, :runtime_function_definition_mismatch)
21+
end
1022
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
class FunctionDefinitionPolicy < BasePolicy
4+
delegate { subject.runtime_function_definition }
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
class ParameterDefinitionPolicy < BasePolicy
4+
delegate { subject.runtime_parameter_definition }
5+
end

app/services/runtimes/runtime_function_definitions/update_service.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ def update_runtime_function_definition(runtime_function_definition, t)
3737
runtime_name: runtime_function_definition.runtime_name
3838
)
3939
db_object.removed_at = nil
40-
db_object.parameters = update_parameters(db_object, runtime_function_definition.runtime_parameter_definitions,
41-
db_object.parameters, t)
4240
db_object.return_type = if runtime_function_definition.return_type_identifier.present?
4341
find_data_type_identifier(runtime_function_definition.return_type_identifier,
4442
runtime_function_definition.generic_mappers, t)
@@ -51,7 +49,6 @@ def update_runtime_function_definition(runtime_function_definition, t)
5149
db_object.deprecation_messages)
5250

5351
db_object.error_types = update_error_types(runtime_function_definition.error_type_identifiers, db_object, t)
54-
db_object.generic_mappers = update_mappers(runtime_function_definition.generic_mappers, db_object, t)
5552

5653
if db_object.function_definitions.empty?
5754
definition = FunctionDefinition.new
@@ -64,6 +61,11 @@ def update_runtime_function_definition(runtime_function_definition, t)
6461

6562
db_object.function_definitions << definition
6663
end
64+
65+
db_object.parameters = update_parameters(db_object, runtime_function_definition.runtime_parameter_definitions,
66+
db_object.parameters, t)
67+
db_object.generic_mappers = update_mappers(runtime_function_definition.generic_mappers, db_object, t)
68+
6769
db_object.save
6870
db_object
6971
end
@@ -199,6 +201,7 @@ def update_parameters(runtime_function_definition, parameters, db_parameters, t)
199201
definition.documentations = update_translations(real_param.documentation, definition.documentations)
200202
definition.data_type = db_param.data_type
201203
definition.default_value = db_param.default_value
204+
definition.function_definition = runtime_function_definition.function_definitions.first
202205

203206
db_param.parameter_definitions << definition
204207
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class AddFunctionDefinitionToParameterDefinition < Code0::ZeroTrack::Database::Migration[1.0]
4+
def change
5+
# rubocop:disable Rails/NotNullColumn -- backwards compatibility intentionally ignored
6+
add_reference :parameter_definitions, :function_definition, null: false, foreign_key: { on_delete: :cascade }
7+
# rubocop:enable Rails/NotNullColumn
8+
end
9+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d3649d3eb21c55852c948235ae16319daf78f7260be8393a2718b63c27fb4dd4

0 commit comments

Comments
 (0)