Module: Graphiform::Fields::ClassMethods

Defined in:
lib/graphiform/fields.rb

Instance Method Summary collapse

Instance Method Details

#graphql_field(name, write_name: nil, readable: true, writable: false, **options) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/graphiform/fields.rb', line 63

def graphql_field(
  name,
  write_name: nil,
  readable: true,
  writable: false,
  **options
)
  graphql_readable_field(name, **options) if readable
  graphql_writable_field(write_name || name, **options) if writable
end

#graphql_fields(*names, **options) ⇒ Object



74
75
76
77
78
# File 'lib/graphiform/fields.rb', line 74

def graphql_fields(*names, **options)
  names.each do |name|
    graphql_field(name, **options)
  end
end

#graphql_readable_field(name, as: nil, **options) ⇒ Object



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

def graphql_readable_field(
  name,
  as: nil,
  **options
)
  column_def = column(as || name)
  association_def = association(as || name)

  graphql_add_column_field(name, column_def, as: as, **options) if column_def.present?
  graphql_add_association_field(name, association_def, as: as, **options) if association_def.present?
  graphql_add_method_field(name, as: as, **options) unless column_def.present? || association_def.present?

  graphql_add_scopes_to_filter(name, as || name)
  graphql_field_to_sort(name, as || name)
  graphql_field_to_grouping(name, as || name)
end

#graphql_readable_fields(*names, **options) ⇒ Object



80
81
82
83
84
# File 'lib/graphiform/fields.rb', line 80

def graphql_readable_fields(*names, **options)
  names.each do |name|
    graphql_readable_field(name, **options)
  end
end

#graphql_writable_field(name, type: nil, required: false, write_prepare: nil, prepare: nil, description: nil, default_value: ::GraphQL::Schema::Argument::NO_DEFAULT, as: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphiform/fields.rb', line 27

def graphql_writable_field(
  name,
  type: nil,
  required: false,
  write_prepare: nil,
  prepare: nil,
  description: nil,
  default_value: ::GraphQL::Schema::Argument::NO_DEFAULT,
  as: nil,
  **
)
  name = name.to_sym
  has_nested_attributes_method = instance_methods.include?("#{as || name}_attributes=".to_sym)

  argument_name = has_nested_attributes_method ? "#{name}_attributes".to_sym : name
  argument_type = graphql_resolve_argument_type(as || name, type)
  as = has_nested_attributes_method ? "#{as}_attributes".to_sym : as.to_sym if as

  return Helpers.logger.warn "Graphiform: Missing `type` for argument `#{name}` in model `#{self.name}`" if argument_type.nil?

  prepare = write_prepare || prepare

  graphql_input.class_eval do
    argument(
      argument_name,
      argument_type,
      required: required,
      prepare: prepare,
      description: description,
      default_value: default_value,
      as: as,
      method_access: false
    )
  end
end

#graphql_writable_fields(*names, **options) ⇒ Object



86
87
88
89
90
# File 'lib/graphiform/fields.rb', line 86

def graphql_writable_fields(*names, **options)
  names.each do |name|
    graphql_writable_field(name, **options)
  end
end