From 9ba16506c19eabaa3919d68563210d9bd707d84b Mon Sep 17 00:00:00 2001 From: sabrina Date: Thu, 6 Feb 2025 15:23:15 -0300 Subject: [PATCH 1/3] resolve_find_before_create_or_update --- lib/graphoid/mutations/create_or_update.rb | 4 +-- spec/tester_mongo/app/models/account.rb | 4 +++ .../mutations/create_or_update_one_spec.rb | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/graphoid/mutations/create_or_update.rb b/lib/graphoid/mutations/create_or_update.rb index 2d7e0d80..867f66fc 100644 --- a/lib/graphoid/mutations/create_or_update.rb +++ b/lib/graphoid/mutations/create_or_update.rb @@ -21,8 +21,8 @@ def self.build(model) begin user = context[:current_user] - object = if model.respond_to?(:resolve_one) - model.resolve_one(self, nil, where.to_h) + object = if model.respond_to?(:resolve_find_before_create_or_update) + model.resolve_find_before_create_or_update(self, where.to_h) else model.where(where.to_h).first end diff --git a/spec/tester_mongo/app/models/account.rb b/spec/tester_mongo/app/models/account.rb index 9b4af809..9f636420 100644 --- a/spec/tester_mongo/app/models/account.rb +++ b/spec/tester_mongo/app/models/account.rb @@ -67,6 +67,10 @@ def self.resolve_find_before_delete(resolver, id) where.not(string_field: 'hook').find(id) end + def self.resolve_find_before_create_or_update(resolver, filter) + resolve_one(resolver, nil, filter) + end + def self.resolve_one(resolver, id, filter) result = where.not(string_field: 'hook') result = Graphoid::Queries::Processor.execute(result, filter.to_h) diff --git a/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb b/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb index 632060c7..ca0ac9b2 100644 --- a/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb +++ b/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb @@ -97,4 +97,29 @@ expect(existing2.float_field).to eq(3.0) end end + + context 'intercepting the matching record' do + it 'does not find the matching record and creates an object' do + existing = Account.create!(string_field: 'hook', integerField: 5) + + @query = %{ + mutation { + createOrUpdateAccount( + where: { integerField: 5 } + data: { + floatField: 3.2, + }) { + id + } + } + } + + subject + existing.reload + persisted = Account.find(subject['id']) + + expect(existing.float_field).to be_nil + expect(persisted.float_field).to eq(3.2) + end + end end From 8e7f2807b693903a31edf064cc31faa17b1aa588 Mon Sep 17 00:00:00 2001 From: sabrina Date: Thu, 6 Feb 2025 15:35:41 -0300 Subject: [PATCH 2/3] fix tests --- .../spec/graphoid/mutations/create_or_update_one_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb b/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb index ca0ac9b2..235dc518 100644 --- a/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb +++ b/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb @@ -100,7 +100,8 @@ context 'intercepting the matching record' do it 'does not find the matching record and creates an object' do - existing = Account.create!(string_field: 'hook', integerField: 5) + existing = Account.create!(integerField: 5) + existing.set(string_field: 'hook') @query = %{ mutation { From 7e7f8c814bc2eff5e4727c29f68aae78d9ec2a02 Mon Sep 17 00:00:00 2001 From: sabrina Date: Thu, 6 Feb 2025 15:41:17 -0300 Subject: [PATCH 3/3] fix tests again --- .../spec/graphoid/mutations/create_or_update_one_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb b/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb index 235dc518..ab94c168 100644 --- a/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb +++ b/spec/tester_mongo/spec/graphoid/mutations/create_or_update_one_spec.rb @@ -100,8 +100,7 @@ context 'intercepting the matching record' do it 'does not find the matching record and creates an object' do - existing = Account.create!(integerField: 5) - existing.set(string_field: 'hook') + existing = Account.create!(string_field: 'hook', integer_field: 5) @query = %{ mutation {