Class: Hai::GraphQL::CreateMutations
- Inherits:
-
Object
- Object
- Hai::GraphQL::CreateMutations
- Defined in:
- lib/hai/graphql/create_mutations.rb
Class Method Summary collapse
- .add(mutation_type, model) ⇒ Object
- .add_field(mutation_type, model) ⇒ Object
- .define_resolver(model) ⇒ Object
Class Method Details
.add(mutation_type, model) ⇒ Object
7 8 9 10 |
# File 'lib/hai/graphql/create_mutations.rb', line 7 def add(mutation_type, model) define_resolver(model) add_field(mutation_type, model) end |
.add_field(mutation_type, model) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/hai/graphql/create_mutations.rb', line 38 def add_field(mutation_type, model) mutation_type.field( "create_#{model.name.downcase}", mutation: Hai::GraphQL::Types.const_get("Create#{model}") ) end |
.define_resolver(model) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/hai/graphql/create_mutations.rb', line 12 def define_resolver(model) klass = Class.new(Hai::GraphQL::Types::BaseCreate) klass.send(:graphql_name, "Create#{model}") klass.description("Attributes for creating or updating a #{model}.") model.attribute_types.each do |attr, type| next if %w[id created_at updated_at].include?(attr) next if attr.blank? # if the model has no other attributes presence_validators = get_presence_validators(model) klass.argument( attr, Hai::GraphQL::TYPE_CAST[type.class] || Hai::GraphQL::TYPE_CAST[type.class.superclass], required: presence_validators.include?(attr), ) end klass.field(:result, ::Types.const_get("#{model}Type")) klass.define_method(:resolve) do |args = {}| Hai::Create.new(model, context).execute(**args) end Hai::GraphQL::Types.const_set("Create#{model}", klass) end |