Skip to content

Commit

Permalink
Make helpers, not assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Dec 16, 2023
1 parent ff306d4 commit ae25d61
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 222 deletions.
3 changes: 1 addition & 2 deletions lib/graphql/testing.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# frozen_string_literal: true
require "graphql/testing/assertions"
require "graphql/testing/matchers"
require "graphql/testing/helpers"
73 changes: 0 additions & 73 deletions lib/graphql/testing/assertions.rb

This file was deleted.

100 changes: 100 additions & 0 deletions lib/graphql/testing/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# frozen_string_literal: true
module GraphQL
module Testing
module Helpers
# @param schema_class [Class<GraphQL::Schema>]
# @return [Module] A helpers module which always uses the given schema
def self.for(schema_class)
Module.new do
include SchemaHelpers
@@schema_class_for_helpers = schema_class
end
end

class Error < GraphQL::Error
end

class TypeNotVisibleError < Error
def initialize(type_name:)
message = "`#{type_name}` should be `visible?` this field resolution and `context`, but it was not"
super(message)
end
end

class FieldNotVisibleError < Error
def initialize(type_name:, field_name:)
message = "`#{type_name}.#{field_name}` should be `visible?` for this resolution, but it was not"
super(message)
end
end


def run_graphql_field(schema, field_path, object, arguments: {}, context: {})
type_name, field_name = field_path.split(".")
dummy_query = GraphQL::Query.new(schema, context: context)
query_context = dummy_query.context
visible_object_type = dummy_query.get_type(type_name) # rubocop:disable Development/ContextIsPassedCop
if visible_object_type
visible_field = dummy_query.get_field(visible_object_type, field_name)
if visible_field
graphql_object = visible_object_type.wrap(object, query_context)
result = nil
field_args = visible_field.coerce_arguments(graphql_object, arguments, query_context)
dummy_query.context.dataloader.run_isolated {
result = visible_field.resolve(graphql_object, field_args.keyword_arguments, query_context)
result = schema.sync_lazy(result)
}
result
else
raise FieldNotVisibleError.new(field_name: field_name, type_name: type_name)
end
else
raise TypeNotVisibleError.new(type_name: type_name)
end
end

def with_resolution_context(schema, type:, object:, context:{})
resolution_context = ResolutionAssertionContext.new(
self,
schema: schema,
type_name: type,
object: object,
context: context
)
yield(resolution_context)
end

class ResolutionAssertionContext
def initialize(test, type_name:, object:, schema:, context:)
@test = test
@type_name = type_name
@object = object
@schema = schema
@context = context
end


def run_graphql_field(field_name, arguments: {})
if @schema
@test.run_graphql_field(@schema, "#{@type_name}.#{field_name}", @object, arguments: arguments, context: @context)
else
@test.run_graphql_field("#{@type_name}.#{field_name}", @object, arguments: arguments, context: @context)
end
end
end

module SchemaHelpers
include Helpers

def run_graphql_field(field_path, object, arguments: {}, context: {})
super(@@schema_class_for_helpers, field_path, object, arguments: arguments, context: context)
end

def with_resolution_context(*args, **kwargs, &block)
# schema will be added later
super(nil, *args, **kwargs, &block)
end
end
end
end
end
7 changes: 0 additions & 7 deletions lib/graphql/testing/matchers.rb

This file was deleted.

140 changes: 0 additions & 140 deletions spec/graphql/testing/assertions_spec.rb

This file was deleted.

Loading

0 comments on commit ae25d61

Please # to comment.