Simplify the way GraphQL 'resolve' is used

This commit is contained in:
Alberto Miedes Garcés
2017-01-12 19:51:58 +01:00
parent 6940b90260
commit d1f14ffb93
14 changed files with 27 additions and 210 deletions

View File

@@ -1,59 +0,0 @@
require 'rails_helper'
describe GraphQL::AssociationResolver do
let(:comments_resolver) { GraphQL::AssociationResolver.new(:comments, Comment) }
let(:geozone_resolver) { GraphQL::AssociationResolver.new(:geozone, Geozone) }
let(:geozones_resolver) { GraphQL::AssociationResolver.new(:geozones, Geozone) }
describe '#initialize' do
it 'sets allowed elements for unscoped models' do
geozone_1 = create(:geozone)
geozone_2 = create(:geozone)
expect(geozones_resolver.allowed_elements).to match_array([geozone_1, geozone_2])
end
it 'sets allowed elements for scoped models' do
public_comment = create(:comment, commentable: create(:proposal))
restricted_comment = create(:comment, commentable: create(:proposal, :hidden))
expect(comments_resolver.allowed_elements).to match_array([public_comment])
end
end
describe '#call' do
it 'resolves simple associations' do
geozone = create(:geozone)
proposal = create(:proposal, geozone: geozone)
result = geozone_resolver.call(proposal, nil, nil)
expect(result).to eq(geozone)
end
it 'blocks forbidden elements when resolving simple associations' do
skip 'None of the current models allows this spec to be executed'
end
it 'resolves paginated associations' do
proposal = create(:proposal)
comment_1 = create(:comment, commentable: proposal)
comment_2 = create(:comment, commentable: proposal)
comment_3 = create(:comment, commentable: create(:proposal))
result = comments_resolver.call(proposal, nil, nil)
expect(result).to match_array([comment_1, comment_2])
end
it 'blocks forbidden elements when resolving paginated associations' do
proposal = create(:proposal, :hidden)
comment = create(:comment, commentable: proposal)
result = comments_resolver.call(proposal, nil, nil)
expect(result).to be_empty
end
end
end

View File

@@ -1,28 +0,0 @@
require 'rails_helper'
describe GraphQL::RootCollectionResolver do
let(:geozones_resolver) { GraphQL::RootCollectionResolver.new(Geozone) }
let(:comments_resolver) { GraphQL::RootCollectionResolver.new(Comment) }
describe '#call' do
it 'returns the whole colleciton for unscoped models' do
geozone_1 = create(:geozone)
geozone_2 = create(:geozone)
result = geozones_resolver.call(nil, nil, nil)
expect(result).to match_array([geozone_1, geozone_2])
end
it 'blocks forbidden elements for scoped models' do
proposal = create(:proposal, :hidden)
comment_1 = create(:comment)
comment_2 = create(:comment, commentable: proposal)
result = comments_resolver.call(nil, nil, nil)
expect(result).to match_array([comment_1])
end
end
end

View File

@@ -1,44 +0,0 @@
require 'rails_helper'
describe GraphQL::RootElementResolver do
let(:comment_resolver) { GraphQL::RootElementResolver.new(Comment) }
let(:geozone_resolver) { GraphQL::RootElementResolver.new(Geozone) }
describe '#call' do
it 'resolves simple elements' do
comment = create(:comment)
result = comment_resolver.call(nil, {'id' => comment.id}, nil)
expect(result).to eq(comment)
end
it 'returns nil when requested element is forbidden' do
proposal = create(:proposal, :hidden)
comment = create(:comment, commentable: proposal)
result = comment_resolver.call(nil, {'id' => comment.id}, nil)
expect(result).to be_nil
end
it 'returns nil when requested element does not exist' do
result = comment_resolver.call(nil, {'id' => 1}, nil)
expect(result).to be_nil
end
it 'uses the public_for_api scope when available' do
geozone = create(:geozone)
comment = create(:comment, commentable: create(:proposal, :hidden))
geozone_result = geozone_resolver.call(nil, {'id' => geozone.id}, nil)
comment_result = comment_resolver.call(nil, {'id' => comment.id}, nil)
expect(geozone_result).to eq(geozone)
expect(comment_result).to be_nil
end
end
end