Wrote specs for GraphQL root resolvers

This commit is contained in:
Alberto Miedes Garcés
2017-01-08 11:37:15 +01:00
parent 0b302c2afc
commit ed6ba384dd
3 changed files with 64 additions and 2 deletions

View File

@@ -8,9 +8,9 @@ module GraphQL
def call(object, arguments, context)
if target_model.respond_to?(:public_for_api)
target_model.public_for_api.find(arguments["id"])
target_model.public_for_api.find_by(id: arguments["id"])
else
target_model.find(arguments["id"])
target_model.find_by(id: arguments["id"])
end
end
end

View File

@@ -0,0 +1,27 @@
require 'rails_helper'
describe GraphQL::RootCollectionResolver do
let(:comments_resolver) { GraphQL::RootCollectionResolver.new(Comment) }
describe '#call' do
it 'resolves collections' do
comment_1 = create(:comment)
comment_2 = create(:comment)
result = comments_resolver.call(nil, nil, nil)
expect(result).to match_array([comment_1, comment_2])
end
it 'blocks collection forbidden elements' 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

@@ -0,0 +1,35 @@
require 'rails_helper'
describe GraphQL::RootElementResolver do
let(:comment_resolver) { GraphQL::RootElementResolver.new(Comment) }
describe '#call' do
it 'resolves simple elements' do
comment = create(:comment)
arguments = { 'id' => comment.id }
result = comment_resolver.call(nil, arguments, 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)
arguments = { 'id' => comment.id }
result = comment_resolver.call(nil, arguments, nil)
expect(result).to be_nil
end
it 'returns nil when requested element does not exist' do
arguments = { 'id' => 1 }
result = comment_resolver.call(nil, arguments, nil)
expect(result).to be_nil
end
end
end