Skip to content

Commit

Permalink
Merge branch 'graphiti-api:master' into feature/caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeen authored Feb 27, 2024
2 parents af53acf + c94cd33 commit fff41d7
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/graphiti.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ def self.cache(name, kwargs = {}, &block)
require "graphiti/request_validator"
require "graphiti/request_validators/validator"
require "graphiti/request_validators/update_validator"
require "graphiti/query"
require "graphiti/scope"
require "graphiti/deserializer"
require "graphiti/renderer"
Expand Down Expand Up @@ -182,6 +181,7 @@ def self.cache(name, kwargs = {}, &block)
require "graphiti/extensions/boolean_attribute"
require "graphiti/extensions/temp_id"
require "graphiti/serializer"
require "graphiti/query"
require "graphiti/debugger"

if defined?(ActiveRecord)
Expand Down
9 changes: 6 additions & 3 deletions lib/graphiti/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ def sideload_hash
end
end

class RemoteSideloadResource < ::Graphiti::Resource
self.remote = "_remote_sideload_".freeze
self.abstract_class = true # exclude from schema
end

def resource_for_sideload(sideload)
if @resource.remote?
Class.new(Graphiti::Resource) {
self.remote = "_remote_sideload_"
}.new
RemoteSideloadResource.new
else
sideload.resource
end
Expand Down
2 changes: 1 addition & 1 deletion lib/graphiti/resource/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def relationship_option(options, name)
options[name] ||= send(:"relationships_#{name}_by_default")
end
end
private :attribute_option
private :relationship_option
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/graphiti/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def generate

def generate_types
{}.tap do |types|
Graphiti::Types.map.each_pair do |name, config|
Graphiti::Types.map.sort.each_entry do |name, config|
types[name] = config.slice(:kind, :description)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/graphiti/scoping/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ def parse_string_arrays(value, singular_filter)
# Find the quoted strings
quotes = value.scan(/{{.*?}}/)
# remove them from the rest
quotes.each { |q| value.gsub!(q, "") }
non_quotes = quotes.inject(value) { |v, q| v.gsub(q, "") }
# remove the quote characters from the quoted strings
quotes.each { |q| q.gsub!("{{", "").gsub!("}}", "") }
# merge everything back together into an array
value = if singular_filter
Array(value) + quotes
Array(non_quotes) + quotes
else
Array(value.split(",")) + quotes
Array(non_quotes.split(",")) + quotes
end
# remove any blanks that are left
value.reject! { |v| v.length.zero? }
Expand Down
14 changes: 14 additions & 0 deletions spec/filtering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ def self.name
expect(records.map(&:id)).to eq([employee1.id])
end

context "retains filtering value" do
it "when value includes curly brackets" do
params[:filter] = {first_name: "{{John}}"}
records
expect(params[:filter]).to eq(first_name: "{{John}}")
end

it "when value does not include curly brackets" do
params[:filter] = {first_name: "John"}
records
expect(params[:filter]).to eq(first_name: "John")
end
end

context "when filter is type hash" do
before do
resource.filter :by_json, :hash do
Expand Down
25 changes: 22 additions & 3 deletions spec/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
before do
employee_resource.has_many :positions,
resource: position_resource
employee_resource.belongs_to :remote, remote: true
position_resource.belongs_to :department,
resource: department_resource
employee_resource.attribute :name, :string
Expand Down Expand Up @@ -986,11 +987,29 @@
end

describe "sideloads" do
before { params[:include] = "positions" }
subject(:sideloads) { instance.sideloads }

it "does not cascate the action" do
expect(sideloads.values.map(&:action)).to eq([:all])
context "when including an has_many resource" do
before { params[:include] = "positions" }

it "does not cascate the action" do
expect(sideloads.values.map(&:action)).to eq([:all])
end
end

context "when including a resource from a remote resource" do
before { params[:include] = "remote.resource" }

let(:sideloads_of_another_query) { described_class.new(resource, params).sideloads }

def resource_class_of_remote_sideload(sideloads)
sideloads.fetch(:remote).sideloads.fetch(:resource).resource.class
end

it "re-uses resource class across multiple queries (avoid memory leak)" do
expect(resource_class_of_remote_sideload(sideloads))
.to eq(resource_class_of_remote_sideload(sideloads_of_another_query))
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ def self.name
expect(schema[:types]).to eq(expected[:types])
end

it "has sorted types" do
expect(schema[:types].to_a).to eq(expected[:types].sort)
end

# Dynamically-created resources, e.g. remote resources
context "when resource has missing name" do
let(:no_name) do
Expand Down

0 comments on commit fff41d7

Please sign in to comment.