Module: Rails::GraphQL::Field::ProxiedField

Defined in:
lib/rails/graphql/field/proxied_field.rb

Overview

GraphQL Proxied Field

Proxied fields are a soft way to copy a real field. The good part is that if the field changes for any reason all its copies will change as well.

The owner of a proxy field is different from the owner of the actual field but that doesn’t affect the field operations.

Proxied field also supports aliases, which helps implementing independent fields and then providing them as proxy to other objects.

Proxies can be created from any kind of input

Options

It accepts all the options of any other type of field plus the following

  • :owner - The main object that this field belongs to.

  • :as - The actual name to be used on the field while assigning the proxy (defaults to nil).

  • :alias - Same as the :as key (defaults to nil).

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object

Make sure to check the original field as well TODO: Maybe not use the == method, but so far so good



47
48
49
# File 'lib/rails/graphql/field/proxied_field.rb', line 47

def ==(other)
  super || field == other
end

#all_directivesObject

Prepend the proxy directives and then the source directives



96
97
98
99
100
# File 'lib/rails/graphql/field/proxied_field.rb', line 96

def all_directives
  inherited = field.all_directives
  return inherited unless defined?(@directives)
  inherited.present? ? inherited + super : super
end

#all_ownersObject

Override this to include proxied owners



74
75
76
# File 'lib/rails/graphql/field/proxied_field.rb', line 74

def all_owners
  super + @field.all_owners
end

#apply_changes(**xargs, &block) ⇒ Object

Allow changing most of the general kind-independent initialize settings



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rails/graphql/field/proxied_field.rb', line 52

def apply_changes(**xargs, &block)
  if (deprecated = xargs[:deprecated])
    xargs[:directives] = ::Array.wrap(xargs[:directives])
    xargs[:directives] << Directive::DeprecatedDirective.new(
      reason: (deprecated.is_a?(String) ? deprecated : nil),
    )
  end

  # TODO: Replace by a proper method to build and set @directives
  @directives = GraphQL.directives_to_set(xargs[:directives], source: self) \
    if xargs.key?(:directives)

  self.description = xargs[:desc] if xargs.key?(:desc)
  self.description = xargs[:description] if xargs.key?(:description)
  @enabled = xargs.fetch(:enabled, !xargs.fetch(:disabled, false)) \
    if xargs.key?(:enabled) || xargs.key?(:disabled)

  normalize_name(xargs.fetch(:alias, xargs[:as]))
  super
end

#directives?Boolean

Check if the field has directives locally or in the proxied field

Returns:

  • (Boolean)


103
104
105
# File 'lib/rails/graphql/field/proxied_field.rb', line 103

def directives?
  super || field.directives?
end

#disable!Object

Just ensure that when the field is proxied to an interface it does not allow disabling



85
86
87
# File 'lib/rails/graphql/field/proxied_field.rb', line 85

def disable!
  super unless non_interface_proxy!('disable')
end

#enable!Object

Just ensure that when the field is proxied to an interface it does not allow enabling



91
92
93
# File 'lib/rails/graphql/field/proxied_field.rb', line 91

def enable!
  super unless non_interface_proxy!('enable')
end

#initialize(field, owner:, **xargs, &block) ⇒ Object



33
34
35
36
37
38
# File 'lib/rails/graphql/field/proxied_field.rb', line 33

def initialize(field, owner:, **xargs, &block)
  @field = field
  @owner = owner

  apply_changes(**xargs, &block)
end

#proxied_fieldObject Also known as: field

Return the proxied field



79
80
81
# File 'lib/rails/graphql/field/proxied_field.rb', line 79

def proxied_field
  @field
end

#proxy?Boolean

Once this module is added then the field becomes a proxy

Returns:

  • (Boolean)


41
42
43
# File 'lib/rails/graphql/field/proxied_field.rb', line 41

def proxy?
  true
end

#validate!Object

It is important to ensure that the proxied field is also valid



108
109
110
111
# File 'lib/rails/graphql/field/proxied_field.rb', line 108

def validate!(*)
  super if defined? super
  field.validate!
end