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, read_prepare: nil, write_prepare: nil, null: nil, **options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/graphiform/fields.rb', line 68

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

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



82
83
84
85
86
# File 'lib/graphiform/fields.rb', line 82

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

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



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

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

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

  # Defer filter/sort/grouping wiring — flushed on first access to
  # graphql_filter / graphql_sort / graphql_grouping.
  graphiform_pending_filters   << [name, identifier, options]
  graphiform_pending_sorts     << [name, identifier, options]
  graphiform_pending_groupings << [name, identifier, options]
end

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



88
89
90
91
92
# File 'lib/graphiform/fields.rb', line 88

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, as: nil, **args) ⇒ Object



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
62
63
64
65
66
# File 'lib/graphiform/fields.rb', line 32

def graphql_writable_field(
  name,
  type: nil,
  required: false,
  write_prepare: nil,
  prepare: nil,
  description: nil,
  as: nil,
  **args
)
  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

  Helpers.add_unless_exists(graphql_input, argument_name) do
    graphql_input.class_eval do
      argument(
        argument_name,
        argument_type,
        required: required,
        prepare: prepare,
        description: description,
        as: as,
        **args
      )
    end
  end
end

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



94
95
96
97
98
# File 'lib/graphiform/fields.rb', line 94

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