Class: Rails::GraphQL::Source

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload, Helpers::InheritedCollection, Helpers::WithCallbacks, Helpers::WithEvents, Helpers::WithNamespace, Builder
Includes:
Helpers::Instantiable
Defined in:
lib/rails/graphql/source.rb,
lib/rails/graphql/source/base.rb,
lib/rails/graphql/source/builder.rb

Overview

GraphQL Source

Source is an abstract object that can contains fields, objects, and information that them are delivered to the relative schemas throughout proxies, ensuring that it still keeps the main ownership of the objects

Direct Known Subclasses

Base

Defined Under Namespace

Modules: Builder, ScopedArguments Classes: ActiveRecordSource, Base, ScopedConfig

Constant Summary

Constants included from Helpers::InheritedCollection

Helpers::InheritedCollection::DEFAULT_TYPES

Constants included from Helpers::WithCallbacks

Helpers::WithCallbacks::DEFAULT_EVENT_TYPES

Instance Attribute Summary

Attributes included from Helpers::Instantiable

#event

Class Method Summary collapse

Methods included from Helpers::InheritedCollection

inherited_collection

Methods included from Helpers::WithNamespace

namespace, namespaces, set_namespace

Methods included from Helpers::WithEvents

extended, included, on

Methods included from Helpers::WithCallbacks

extended, included, on

Methods included from Builder

build_all, build_all_sources, built?, method_missing, respond_to_missing?

Class Method Details

.attach_fields!(type = :all, from = self) ⇒ Object

Attach all defined schema fields into the schemas using the namespaces configured for the source



132
133
134
# File 'lib/rails/graphql/source.rb', line 132

def attach_fields!(type = :all, from = self)
  schemas.each { |schema| schema.import_into(type, from) }
end

.base_nameObject

Get the main name of the source



87
88
89
90
91
92
93
94
95
# File 'lib/rails/graphql/source.rb', line 87

def base_name
  @base_name ||= begin
    nested = "::#{Type::Creator::NESTED_MODULE}::"

    value = name.delete_prefix('GraphQL::')
    value = name.split(nested).last if name.include?(nested)
    value.chomp('Source')
  end
end

.base_type_classObject

Sources are close related to objects, meaning that they are type based



82
83
84
# File 'lib/rails/graphql/source.rb', line 82

def base_type_class
  :Type
end

.find_for(object) ⇒ Object

:singleton-method: Using the list of base_sources, find the first one that can handle the given object



109
110
111
112
# File 'lib/rails/graphql/source.rb', line 109

def find_for(object)
  object = object.constantize if object.is_a?(String)
  base_sources.reverse_each.find { |source| object <= source.assigned_class }
end

.find_for!(object) ⇒ Object

:singleton-method: Find a source for a given object. If none is found, then raise an exception



100
101
102
103
104
# File 'lib/rails/graphql/source.rb', line 100

def find_for!(object)
  find_for(object) || raise(::ArgumentError, (+<<~MSG).squish)
    Unable to find a source for "#{object.name}".
  MSG
end

.kindObject



77
78
79
# File 'lib/rails/graphql/source.rb', line 77

def kind
  :source
end

.schemasObject

Find all the schemas associated with the configured namespaces



137
138
139
140
141
# File 'lib/rails/graphql/source.rb', line 137

def schemas
  GraphQL.enumerate(namespaces.presence || :base).lazy.filter_map do |ns|
    Schema.find(ns)
  end
end

.step(hook_name, unshift: false, &block) ⇒ Object

Add a new description hook. You can use throw :skip and skip parent hooks. If the class is already built, then execute the hook. Use the unshift: true to add the hook at the beginning of the list, which will then be the last to run

Raises:



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rails/graphql/source.rb', line 118

def step(hook_name, unshift: false, &block)
  raise ArgumentError, (+<<~MSG).squish unless hook_names.include?(hook_name.to_sym)
    The #{hook_name.inspect} is not a valid hook method.
  MSG

  if built?(hook_name)
    hook_scope_for(hook_name).instance_exec(&block)
  else
    hooks[hook_name.to_sym].public_send(unshift ? :unshift : :push, block)
  end
end