Skip to content

Instantly share code, notes, and snippets.

@RStankov
Created July 28, 2020 15:41
Show Gist options
  • Save RStankov/9de68e4138758fe2042f705764e3c58c to your computer and use it in GitHub Desktop.
Save RStankov/9de68e4138758fe2042f705764e3c58c to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module SpecSupport
module GraphHelper
def execute_mutation(current_user: nil, context: {}, **inputs)
context = Graph::Context.new(
query: OpenStruct.new(schema: StacksSchema),
values: context.merge(current_user: current_user),
object: nil,
)
described_class.new(object: nil, context: context, field: nil).resolve(inputs)
end
def execute_mutation!(inputs)
result = execute_mutation(inputs)
expect_no_errors(result)
result
end
def execute_resolver(current_user: nil, context: {}, object: nil, **inputs)
resolver = described_class.new(
object: object,
field: nil,
context: Graph::Context.new(
query: OpenStruct.new(schema: StacksSchema),
values: { current_user: current_user, session: {} }.merge(context),
object: object,
),
)
if inputs.empty?
resolver.resolve
else
resolver.resolve(inputs)
end
end
def execute_search_resolver(current_user: nil, context: {}, object: nil, **inputs)
described_class.call(
object,
inputs,
Graph::Context.new(
query: OpenStruct.new(schema: StacksSchema),
values: { current_user: current_user, session: {} }.merge(context),
object: object,
),
)
end
def execute_batch_resolver(*args)
GraphQL::Batch.batch do
execute_resolver(*args)
end
end
def expect_node(result, value = :dont_care)
expect(result[:errors]).to eq []
if value == :dont_care
expect(result[:node]).not_to eq nil
else
expect(result[:node]).to eq value
end
yield result[:node] if block_given?
end
def expect_no_errors(result)
expect(result[:errors]).to eq []
end
def expect_error(result, name, value = :dont_care)
expect(result[:errors]).not_to eq []
expect(result[:node]).to eq(nil), 'node should be blank'
error = result[:errors].find { |info| info.field.to_s == name.to_s }
expect(error).to be_present, "error message for #{name} is not present"
expect(error.message).to eq value unless value == :dont_care
end
def expect_record_not_found_error(result)
expect_error result, :base, :record_not_found
end
def expect_access_denied_error(result)
expect_error result, :base, :access_denied
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment