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



56
57
58
59
60
61
62
63
64
65
# File 'lib/graphiform/fields.rb', line 56

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



67
68
69
70
71
# File 'lib/graphiform/fields.rb', line 67

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

#graphql_readable_field(name, **options) ⇒ Object



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

def graphql_readable_field(
  name,
  **options
)
  column_def = column(name)
  association_def = association(name)

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

  graphql_add_scopes_to_filter(name)
  graphql_field_to_sort(name)
end

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



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

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



25
26
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
# File 'lib/graphiform/fields.rb', line 25

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,
  **_options
)
  name = name.to_sym
  argument_name = graphql_resolve_argument_name(name)
  argument_type = graphql_resolve_argument_type(name, type)

  return Helpers.logger.warn "Graphiform: Missing `type` for argument #{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
  end
end

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



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

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