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



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

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



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

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
# 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?

  graphql_add_scopes_to_filter(name, identifier, **options)
  graphql_field_to_sort(name, identifier, **options)
  graphql_field_to_grouping(name, identifier, **options)
end

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



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

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



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

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,
  **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

  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,
      **args
    )
  end unless graphql_input.arguments.keys.any? { |key| Helpers.equal_graphql_names?(key, argument_name) }
end

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



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

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