Class: Hai::GraphQL::UpdateMutations

Inherits:
Object
  • Object
show all
Defined in:
lib/hai/graphql/update_mutations.rb

Class Method Summary collapse

Class Method Details

.add(mutation_type, model) ⇒ Object



5
6
7
8
# File 'lib/hai/graphql/update_mutations.rb', line 5

def add(mutation_type, model)
  define_resolver(model)
  add_field(mutation_type, model)
end

.add_field(mutation_type, model) ⇒ Object



27
28
29
30
# File 'lib/hai/graphql/update_mutations.rb', line 27

def add_field(mutation_type, model)
  mutation_type.field("update_#{model.name.downcase}",
                      mutation: Hai::GraphQL::Types.const_get("Update#{model}"))
end

.define_resolver(model) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hai/graphql/update_mutations.rb', line 10

def define_resolver(model)
  klass = Class.new(Hai::GraphQL::Types::BaseCreate)
  klass.send(:graphql_name, "Update#{model}")
  klass.description("Mutation to Update #{model}.")

  klass.argument(:attributes, "Types::#{model}Attributes")
  klass.argument(:id, ::GraphQL::Types::ID)

  klass.field(:result, ::Types.const_get("#{model}Type"))

  klass.define_method(:resolve) do |id:, attributes:|
    Hai::Update.new(model, context).execute(id: id, attributes: attributes.to_h)
  end

  Hai::GraphQL::Types.const_set("Update#{model}", klass)
end