Commit 9f1550bd authored by Alex Kalderimis's avatar Alex Kalderimis

Move resolver factories out to their own helper

This moves the methods that create simple resolvers into their own
helper so they can be imported separately, reducing the size of
graphql_helpers.
parent 3d587989
......@@ -4,6 +4,7 @@ require 'spec_helper'
RSpec.describe 'Gitlab::Graphql::Authorize' do
include GraphqlHelpers
include Graphql::ResolverFactories
let_it_be(:user) { create(:user) }
let(:permission_single) { :foo }
......@@ -13,7 +14,7 @@ RSpec.describe 'Gitlab::Graphql::Authorize' do
let(:result) do
schema = empty_schema
schema.use(Gitlab::Graphql::Authorize)
execute_query(query_type, schema)
execute_query(query_type, schema: schema)
end
subject { result.dig('data', 'item') }
......@@ -59,7 +60,7 @@ RSpec.describe 'Gitlab::Graphql::Authorize' do
describe 'with a single permission' do
let(:query_type) do
query_factory do |query|
query.field :item, type, null: true, resolver: simple_resolver(test_object), authorize: permission_single
query.field :item, type, null: true, resolver: new_resolver(test_object), authorize: permission_single
end
end
......@@ -70,7 +71,7 @@ RSpec.describe 'Gitlab::Graphql::Authorize' do
let(:query_type) do
permissions = permission_collection
query_factory do |qt|
qt.field :item, type, null: true, resolver: simple_resolver(test_object) do
qt.field :item, type, null: true, resolver: new_resolver(test_object) do
authorize permissions
end
end
......@@ -83,7 +84,7 @@ RSpec.describe 'Gitlab::Graphql::Authorize' do
describe 'Field authorizations when field is a built in type' do
let(:query_type) do
query_factory do |query|
query.field :item, type, null: true, resolver: simple_resolver(test_object)
query.field :item, type, null: true, resolver: new_resolver(test_object)
end
end
......@@ -136,7 +137,7 @@ RSpec.describe 'Gitlab::Graphql::Authorize' do
describe 'Type authorizations' do
let(:query_type) do
query_factory do |query|
query.field :item, type, null: true, resolver: simple_resolver(test_object)
query.field :item, type, null: true, resolver: new_resolver(test_object)
end
end
......@@ -173,7 +174,7 @@ RSpec.describe 'Gitlab::Graphql::Authorize' do
let(:query_type) do
query_factory do |query|
query.field :item, type, null: true, resolver: simple_resolver(test_object), authorize: permission_2
query.field :item, type, null: true, resolver: new_resolver(test_object), authorize: permission_2
end
end
......@@ -192,7 +193,7 @@ RSpec.describe 'Gitlab::Graphql::Authorize' do
let(:query_type) do
query_factory do |query|
query.field :item, type.connection_type, null: true, resolver: simple_resolver([test_object, second_test_object])
query.field :item, type.connection_type, null: true, resolver: new_resolver([test_object, second_test_object])
end
end
......@@ -212,7 +213,7 @@ RSpec.describe 'Gitlab::Graphql::Authorize' do
describe 'limiting connections with multiple objects' do
let(:query_type) do
query_factory do |query|
query.field :item, type.connection_type, null: true, resolver: simple_resolver([test_object, second_test_object])
query.field :item, type.connection_type, null: true, resolver: new_resolver([test_object, second_test_object])
end
end
......@@ -236,7 +237,7 @@ RSpec.describe 'Gitlab::Graphql::Authorize' do
let(:query_type) do
query_factory do |query|
query.field :item, [type], null: true, resolver: simple_resolver([test_object])
query.field :item, [type], null: true, resolver: new_resolver([test_object])
end
end
......@@ -264,13 +265,13 @@ RSpec.describe 'Gitlab::Graphql::Authorize' do
type_factory do |type|
type.graphql_name 'FakeProjectType'
type.field :test_issues, issue_type.connection_type, null: false,
resolver: simple_resolver(Issue.where(project: [visible_project, other_project]).order(id: :asc))
resolver: new_resolver(Issue.where(project: [visible_project, other_project]).order(id: :asc))
end
end
let(:query_type) do
query_factory do |query|
query.field :test_project, project_type, null: false, resolver: simple_resolver(visible_project)
query.field :test_project, project_type, null: false, resolver: new_resolver(visible_project)
end
end
......
......@@ -4,6 +4,7 @@ require 'spec_helper'
RSpec.describe 'Graphql Field feature flags' do
include GraphqlHelpers
include Graphql::ResolverFactories
let_it_be(:user) { create(:user) }
......@@ -23,7 +24,7 @@ RSpec.describe 'Graphql Field feature flags' do
let(:query_type) do
query_factory do |query|
query.field :item, type, null: true, feature_flag: feature_flag, resolver: simple_resolver(test_object)
query.field :item, type, null: true, feature_flag: feature_flag, resolver: new_resolver(test_object)
end
end
......
# frozen_string_literal: true
module Graphql
module ResolverFactories
def new_resolver(resolved_value = 'Resolved value', method: :resolve)
case method
when :resolve
simple_resolver(resolved_value)
when :find_object
find_object_resolver(resolved_value)
else
raise "Cannot build a resolver for #{method}"
end
end
private
def simple_resolver(resolved_value = 'Resolved value')
Class.new(Resolvers::BaseResolver) do
define_method :resolve do |**_args|
resolved_value
end
end
end
def find_object_resolver(resolved_value = 'Found object')
Class.new(Resolvers::BaseResolver) do
include ::Gitlab::Graphql::Authorize::AuthorizeResource
def resolve(**args)
authorized_find!(**args)
end
define_method :find_object do |**_args|
resolved_value
end
end
end
end
end
......@@ -259,39 +259,6 @@ module GraphqlHelpers
::Gitlab::Utils::MergeHash.merge(Array.wrap(variables).map(&:to_h)).to_json
end
def new_resolver(resolved_value = 'Resolved value', method: :resolve)
case resolved_value
when :resolve
simple_resolver(resolved_value)
when :find_object
find_object_resolver(resolved_value)
else
raise "Cannot build a resolver for #{method}"
end
end
def simple_resolver(resolved_value = 'Resolved value')
Class.new(Resolvers::BaseResolver) do
define_method :resolve do |**_args|
resolved_value
end
end
end
def find_object_resolver(resolved_value = 'Found object')
Class.new(Resolvers::BaseResolver) do
include ::Gitlab::Graphql::Authorize::AuthorizeResource
def resolve(**args)
authorized_find!(**args)
end
define_method :find_object do |**_args|
resolved_value
end
end
end
# Recursively convert a Hash with Ruby-style keys to GraphQL fieldname-style keys
#
# prepare_input_for_mutation({ 'my_key' => 1 })
......@@ -660,10 +627,12 @@ module GraphqlHelpers
end
end
def execute_query(query_type, schema = empty_schema)
# assumes query_string to be let-bound in the current context
def execute_query(query_type, schema: empty_schema, graphql: query_string)
schema.query(query_type)
schema.execute(
query_string,
graphql,
context: { current_user: user },
variables: {}
)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment