Module: Graphiti

Defined in:
lib/graphiti/debugger.rb,
lib/graphiti.rb,
lib/graphiti/cli.rb,
lib/graphiti/audit.rb,
lib/graphiti/query.rb,
lib/graphiti/rails.rb,
lib/graphiti/scope.rb,
lib/graphiti/types.rb,
lib/graphiti/errors.rb,
lib/graphiti/runner.rb,
lib/graphiti/schema.rb,
lib/graphiti/context.rb,
lib/graphiti/version.rb,
lib/graphiti/renderer.rb,
lib/graphiti/resource.rb,
lib/graphiti/sideload.rb,
lib/graphiti/stats/dsl.rb,
lib/graphiti/util/hash.rb,
lib/graphiti/util/link.rb,
lib/graphiti/serializer.rb,
lib/graphiti/util/class.rb,
lib/graphiti/schema_diff.rb,
lib/graphiti/audit/report.rb,
lib/graphiti/resource/dsl.rb,
lib/graphiti/scoping/base.rb,
lib/graphiti/scoping/sort.rb,
lib/graphiti/spec_helpers.rb,
lib/graphiti/adapters/null.rb,
lib/graphiti/configuration.rb,
lib/graphiti/hash_renderer.rb,
lib/graphiti/rails/context.rb,
lib/graphiti/rails/railtie.rb,
lib/graphiti/stats/payload.rb,
lib/graphiti/util/sideload.rb,
lib/graphiti/resource/links.rb,
lib/graphiti/resource_proxy.rb,
lib/graphiti/scoping/filter.rb,
lib/graphiti/rails/debugging.rb,
lib/graphiti/resource/remote.rb,
lib/graphiti/filter_operators.rb,
lib/graphiti/rails/controller.rb,
lib/graphiti/rails/responders.rb,
lib/graphiti/scoping/paginate.rb,
lib/graphiti/util/cache_debug.rb,
lib/graphiti/util/uri_decoder.rb,
lib/graphiti/adapters/abstract.rb,
lib/graphiti/request_validator.rb,
lib/graphiti/spec_helpers/node.rb,
lib/graphiti/util/field_params.rb,
lib/graphiti/extensions/temp_id.rb,
lib/graphiti/rails/rake_helpers.rb,
lib/graphiti/rails/test_helpers.rb,
lib/graphiti/resource/interface.rb,
lib/graphiti/scoping/filterable.rb,
lib/graphiti/spec_helpers/rspec.rb,
lib/graphiti/spec_helpers/rspec.rb,
lib/graphiti/util/remote_params.rb,
lib/graphiti/util/simple_errors.rb,
lib/graphiti/spec_helpers/errors.rb,
lib/graphiti/util/include_params.rb,
lib/graphiti/delegates/pagination.rb,
lib/graphiti/resource/persistence.rb,
lib/graphiti/resource/sideloading.rb,
lib/graphiti/spec_helpers/helpers.rb,
lib/graphiti/util/attribute_check.rb,
lib/graphiti/adapters/graphiti_api.rb,
lib/graphiti/resource/polymorphism.rb,
lib/graphiti/spec_helpers/matchers.rb,
lib/graphiti/adapters/active_record.rb,
lib/graphiti/resource/configuration.rb,
lib/graphiti/resource/documentation.rb,
lib/graphiti/scoping/default_filter.rb,
lib/graphiti/util/remote_serializer.rb,
lib/graphiti/jsonapi_serializable_ext.rb,
lib/graphiti/rails/exception_handlers.rb,
lib/graphiti/scoping/extra_attributes.rb,
lib/graphiti/spec_helpers/errors_proxy.rb,
lib/graphiti/util/relationship_payload.rb,
lib/generators/graphiti/generator_mixin.rb,
lib/graphiti/extensions/extra_attribute.rb,
lib/graphiti/util/serializer_attributes.rb,
lib/generators/graphiti/install_generator.rb,
lib/graphiti/error_serializers/validation.rb,
lib/graphiti/extensions/boolean_attribute.rb,
lib/graphiti/request_validators/validator.rb,
lib/generators/graphiti/api_test_generator.rb,
lib/generators/graphiti/resource_generator.rb,
lib/graphiti/util/serializer_relationships.rb,
lib/graphiti/scoping/filter_group_validator.rb,
lib/graphiti/util/transaction_hooks_recorder.rb,
lib/graphiti/adapters/persistence/associations.rb,
lib/graphiti/error_serializers/invalid_request.rb,
lib/generators/graphiti/resource_test_generator.rb,
lib/graphiti/error_serializers/conflict_request.rb,
lib/graphiti/request_validators/update_validator.rb

Overview

For "Rails STI" behavior CreditCard.all # => [, , etc]

Defined Under Namespace

Modules: Adapters, Context, Delegates, ErrorSerializers, Errors, Extensions, GeneratorMixin, JsonapiSerializableExt, Links, Rails, RequestValidators, Scoping, SerializableHash, SerializableTempId, SpecHelpers, Stats, Util Classes: ApiTestGenerator, Audit, CLI, Configuration, Debugger, Deserializer, FilterOperators, HashRenderer, InstallGenerator, Query, Renderer, RequestValidator, Resource, ResourceGenerator, ResourceProxy, ResourceTestGenerator, Runner, Schema, SchemaDiff, Scope, Serializer, Sideload, Types

Constant Summary collapse

DEPRECATOR =
ActiveSupport::Deprecation.new("3.0", "Graphiti")
Responders =

Deprecated. Was core's own responders mixin, superseded by the one that came in with graphiti-rails. Remove in 3.0.

ActiveSupport::Deprecation::DeprecatedConstantProxy.new(
  "Graphiti::Responders",
  "Graphiti::Rails::Responders",
  DEPRECATOR
)
Railtie =

Deprecated. Core's railtie merged into graphiti-rails'. Remove in 3.0.

ActiveSupport::Deprecation::DeprecatedConstantProxy.new(
  "Graphiti::Railtie",
  "Graphiti::Rails::Railtie",
  DEPRECATOR
)
VERSION =
"2.0.0.beta.10"

Class Method Summary collapse

Class Method Details

.broadcast(name, payload) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/graphiti.rb', line 105

def self.broadcast(name, payload)
  # AS::N prefers domain naming format with more specific towards end
  name = "#{name}.graphiti"

  ActiveSupport::Notifications.instrument(name, payload) do
    yield payload if block_given?
  end
end

.cacheObject



163
164
165
# File 'lib/graphiti.rb', line 163

def self.cache
  @cache
end

.cache=(val) ⇒ Object



159
160
161
# File 'lib/graphiti.rb', line 159

def self.cache=(val)
  @cache = val
end

.configObject



75
76
77
# File 'lib/graphiti.rb', line 75

def self.config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Examples:

Graphiti.configure do |c|
  c.raise_on_missing_sideload = false
end

Yields:

See Also:



85
86
87
# File 'lib/graphiti.rb', line 85

def self.configure
  yield config
end

.contextObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



53
54
55
# File 'lib/graphiti.rb', line 53

def self.context
  Thread.current[:context] ||= {}
end

.context=(val) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/graphiti.rb', line 58

def self.context=(val)
  Thread.current[:context] = val
end

.guarded_relationshipsObject

Every guarded relationship, e.g. ["EmployeeResource.positions"]



94
95
96
97
98
99
100
101
102
103
# File 'lib/graphiti.rb', line 94

def self.guarded_relationships
  ::Rails.application.eager_load! if defined?(::Rails) && ::Rails.respond_to?(:application) && ::Rails.application

  resources.flat_map { |resource_class|
    resource_class.config[:sideloads]
      .select { |_name, sideload| sideload.guarded? }
      .keys
      .map { |relationship_name| "#{resource_class.name || "(anonymous resource)"}.#{relationship_name}" }
  }.uniq.sort
end

.log(msg, color = :white, bold = false) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/graphiti.rb', line 130

def self.log(msg, color = :white, bold = false)
  colored = if ::ActiveSupport.version >= Gem::Version.new("7.1")
    ActiveSupport::LogSubscriber.new.send(:color, msg, color, bold: bold)
  else
    ActiveSupport::LogSubscriber.new.send(:color, msg, color, bold)
  end

  logger.debug(colored)
end

.loggerObject



114
115
116
# File 'lib/graphiti.rb', line 114

def self.logger
  @logger ||= stdout_logger
end

.logger=(val) ⇒ Object



126
127
128
# File 'lib/graphiti.rb', line 126

def self.logger=(val)
  @logger = val
end

.resourcesObject



89
90
91
# File 'lib/graphiti.rb', line 89

def self.resources
  @resources ||= []
end

.setup!Object

When we add a sideload, we need to do configuration, such as adding the relationship to the Resource's serializer. However, the sideload's Resource class may not be loaded yet.

This is not a problem when Rails autoloading, but is a problem when eager loading, or not using Rails.

So, load every Resource class then call Graphiti.setup!



148
149
150
151
152
153
# File 'lib/graphiti.rb', line 148

def self.setup!
  resources.each do |r|
    r.apply_sideloads_to_serializer
  end
  @setup = true
end

.setup?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/graphiti.rb', line 155

def self.setup?
  !!@setup
end

.stdout_loggerObject



118
119
120
121
122
123
124
# File 'lib/graphiti.rb', line 118

def self.stdout_logger
  logger = Logger.new($stdout)
  logger.formatter = proc do |severity, datetime, progname, msg|
    "#{msg}\n"
  end
  logger
end

.with_context(obj, namespace = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphiti.rb', line 63

def self.with_context(obj, namespace = nil)
  prior = context
  self.context = {object: obj, namespace: namespace}
  yield
ensure
  self.context = prior

  resources.each do |resource_class|
    resource_class.sideloads.values.each(&:clear_resources)
  end
end