Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ namespace :benchmark do
GraphQLBenchmark.benchmark_lazy_execution
end

desc "Benchmark introspection"
task :introspection do
prepare_benchmark
GraphQLBenchmark.benchmark_introspection
end

desc "Memory profile"
task :memory do
prepare_benchmark
Expand Down
22 changes: 22 additions & 0 deletions benchmark/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ def benchmark_lazy_execution
end
end

def benchmark_introspection
document = GraphQL.parse(GraphQL::Introspection.query)

Benchmark.ips do |x|
x.report("graphql-ruby: introspection") do
GRAPHQL_GEM_SCHEMA.execute(document: document)
end

x.report("graphql-cardinal introspection") do
GraphQL::Cardinal::Executor.new(
SCHEMA,
BREADTH_RESOLVERS,
document,
{},
tracers: [CARDINAL_TRACER],
).perform
end

x.compare!
end
end

def memory_profile
default_data_sizes = "10, 1000"
sizes = ENV.fetch("SIZES", default_data_sizes).split(",").map(&:to_i)
Expand Down
1 change: 1 addition & 0 deletions lib/graphql/cardinal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ module Cardinal
require_relative "cardinal/loader"
require_relative "cardinal/tracer"
require_relative "cardinal/field_resolvers"
require_relative "cardinal/introspection"
require_relative "cardinal/executor"
require_relative "cardinal/version"
8 changes: 3 additions & 5 deletions lib/graphql/cardinal/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(schema, resolvers, document, root_object, variables: {}, context:
@root_object = root_object
@tracers = tracers
@variables = variables
@context = context
@context = @query.context
@data = {}
@errors = []
@exec_queue = []
Expand Down Expand Up @@ -211,10 +211,8 @@ def resolve_execution_field(exec_field, resolved_sources)
# DANGER: HOT PATH!
parent_responses[i][field_key] = if val.nil? || val.is_a?(StandardError)
build_missing_value(exec_field, field_type, val)
elsif return_type.kind.scalar?
coerce_scalar_value(return_type, val)
elsif return_type.kind.enum?
coerce_enum_value(return_type, val)
elsif return_type.kind.leaf?
coerce_leaf_value(exec_field, field_type, val)
else
val
end
Expand Down
39 changes: 19 additions & 20 deletions lib/graphql/cardinal/executor/hot_paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module GraphQL::Cardinal
class Executor
module HotPaths
INCORRECT_LIST_VALUE = "Incorrect result for list field. Expected Array, got ".freeze

# DANGER: HOT PATH!
# Overhead added here scales dramatically...
def build_composite_response(exec_field, current_type, source, next_sources, next_responses)
Expand All @@ -13,7 +15,7 @@ def build_composite_response(exec_field, current_type, source, next_sources, nex
build_missing_value(exec_field, current_type, source)
elsif current_type.list?
unless source.is_a?(Array)
report_exception("Incorrect result for list field. Expected Array, got #{source.class}", field: exec_field)
report_exception("#{INCORRECT_LIST_VALUE}#{source.class}", field: exec_field)
return build_missing_value(exec_field, current_type, nil)
end

Expand Down Expand Up @@ -47,28 +49,25 @@ def build_missing_value(exec_field, current_type, val)

# DANGER: HOT PATH!
# Overhead added here scales dramatically...
def coerce_scalar_value(type, value)
case type.graphql_name
when "String"
value.is_a?(String) ? value : value.to_s
when "ID"
value.is_a?(String) || value.is_a?(Numeric) ? value : value.to_s
when "Int"
value.is_a?(Integer) ? value : Integer(value)
when "Float"
value.is_a?(Float) ? value : Float(value)
when "Boolean"
value == TrueClass || value == FalseClass ? value : !!value
def coerce_leaf_value(exec_field, current_type, val)
if current_type.list?
unless val.is_a?(Array)
report_exception("#{INCORRECT_LIST_VALUE}#{val.class}", field: exec_field)
return build_missing_value(exec_field, current_type, nil)
end

current_type = current_type.of_type while current_type.non_null?

val.each { coerce_leaf_value(exec_field, current_type.of_type, _1) }
else
value
begin
current_type.unwrap.coerce_result(val, @context)
rescue StandardError => e
report_exception("Coercion error", field: exec_field)
build_missing_value(exec_field, current_type, val)
end
end
end

# DANGER: HOT PATH!
# Overhead added here scales dramatically...
def coerce_enum_value(type, value)
value
end
end
end
end
12 changes: 12 additions & 0 deletions lib/graphql/cardinal/field_resolvers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ def resolve(objects, _args, _ctx, _scope)
end
end

class MethodResolver < FieldResolver
def initialize(name)
@name = name
end

def resolve(objects, _args, _ctx, _scope)
map_sources(objects) do |obj|
obj.public_send(@name)
end
end
end

class TypenameResolver < FieldResolver
def resolve(objects, _args, _ctx, scope)
typename = scope.parent_type.graphql_name.freeze
Expand Down
201 changes: 201 additions & 0 deletions lib/graphql/cardinal/introspection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# frozen_string_literal: true

module GraphQL
module Cardinal
module Introspection
module Schema
class EndpointResolver < FieldResolver
def resolve(objects, _args, ctx, _exec_field)
Array.new(objects.length, ctx.query.schema)
end
end

class TypesResolver < FieldResolver
def resolve(objects, _args, ctx, _exec_field)
types = ctx.query.types.all_types
Array.new(objects.length, types)
end
end

class DirectivesResolver < FieldResolver
def resolve(objects, _args, ctx, _exec_field)
directives = ctx.query.types.directives
Array.new(objects.length, directives)
end
end
end

module Type
class EndpointResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
type = ctx.query.get_type(args["name"])
Array.new(objects.length, type)
end
end

class TypeKindResolver < FieldResolver
def resolve(objects, _args, ctx, _exec_field)
map_sources(objects) do |type|
type.kind.name
end
end
end

class EnumValuesResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
map_sources(objects) do |type|
if type.kind.enum?
enum_values = ctx.query.types.enum_values(type)
enum_values = enum_values.reject(&:deprecation_reason) unless args["includeDeprecated"]
enum_values
end
end
end
end

class FieldsResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
map_sources(objects) do |type|
if type.kind.fields?
fields = ctx.query.types.fields(type)
fields = fields.reject(&:deprecation_reason) unless args["includeDeprecated"]
fields
end
end
end
end

class InputFieldsResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
map_sources(objects) do |type|
if type.kind.input_object?
fields = ctx.query.types.arguments(type)
fields = fields.reject(&:deprecation_reason) unless args["includeDeprecated"]
fields
end
end
end
end

class InterfacesResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
map_sources(objects) do |type|
ctx.query.types.interfaces(type) if type.kind.fields?
end
end
end

class PossibleTypesResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
map_sources(objects) do |type|
ctx.query.possible_types(type) if type.kind.abstract?
end
end
end

class OfTypeResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
map_sources(objects) do |type|
type.of_type if type.kind.wraps?
end
end
end

class SpecifiedByUrlResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
map_sources(objects) do |type|
type.specified_by_url if type.kind.scalar?
end
end
end
end

class ArgumentsResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
map_sources(objects) do |owner|
owner_args = ctx.query.types.arguments(owner)
owner_args = owner_args.reject(&:deprecation_reason) unless args["includeDeprecated"]
owner_args
end
end
end

class ArgumentDefaultValueResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
builder = nil
printer = nil
map_sources(objects) do |arg|
next nil unless arg.default_value?

builder ||= GraphQL::Language::DocumentFromSchemaDefinition.new(ctx.query.schema, context: ctx)
printer ||= GraphQL::Language::Printer.new
printer.print(builder.build_default_value(arg.default_value, arg.type))
end
end
end

class IsDeprecatedResolver < FieldResolver
def resolve(objects, args, ctx, _exec_field)
map_sources(objects) { !!_1.deprecation_reason }
end
end

ENTRYPOINT_RESOLVERS = {
"__schema" => Schema::EndpointResolver.new,
"__type" => Type::EndpointResolver.new,
}.freeze

TYPE_RESOLVERS = {
"__Schema" => {
"description" => MethodResolver.new(:description),
"directives" => Schema::DirectivesResolver.new,
"mutationType" => MethodResolver.new(:mutation),
"queryType" => MethodResolver.new(:query),
"subscriptionType" => MethodResolver.new(:subscription),
"types" => Schema::TypesResolver.new,
},
"__Type" => {
"description" => MethodResolver.new(:description),
"enumValues" => Type::EnumValuesResolver.new,
"fields" => Type::FieldsResolver.new,
"inputFields" => Type::InputFieldsResolver.new,
"interfaces" => Type::InterfacesResolver.new,
"kind" => Type::TypeKindResolver.new,
"name" => MethodResolver.new(:graphql_name),
"ofType" => Type::OfTypeResolver.new,
"possibleTypes" => Type::PossibleTypesResolver.new,
"specifiedByURL" => Type::SpecifiedByUrlResolver.new,
},
"__Field" => {
"args" => ArgumentsResolver.new,
"deprecationReason" => MethodResolver.new(:deprecation_reason),
"description" => MethodResolver.new(:description),
"isDeprecated" => IsDeprecatedResolver.new,
"name" => MethodResolver.new(:graphql_name),
"type" => MethodResolver.new(:type),
},
"__InputValue" => {
"defaultValue" => ArgumentDefaultValueResolver.new,
"deprecationReason" => MethodResolver.new(:deprecation_reason),
"description" => MethodResolver.new(:description),
"isDeprecated" => IsDeprecatedResolver.new,
"name" => MethodResolver.new(:graphql_name),
"type" => MethodResolver.new(:type),
},
"__EnumValue" => {
"deprecationReason" => MethodResolver.new(:deprecation_reason),
"description" => MethodResolver.new(:description),
"isDeprecated" => IsDeprecatedResolver.new,
"name" => MethodResolver.new(:graphql_name),
},
"__Directive" => {
"args" => ArgumentsResolver.new,
"description" => MethodResolver.new(:description),
"isRepeatable" => MethodResolver.new(:repeatable?),
"locations" => MethodResolver.new(:locations),
"name" => MethodResolver.new(:graphql_name),
}
}.freeze
end
end
end
2 changes: 2 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def perform(keys)
end

BREADTH_RESOLVERS = {
**GraphQL::Cardinal::Introspection::TYPE_RESOLVERS,
"Node" => {
"id" => GraphQL::Cardinal::HashKeyResolver.new("id"),
"__type__" => ->(obj, ctx) { ctx[:query].get_type(obj["__typename__"]) },
Expand Down Expand Up @@ -130,6 +131,7 @@ def perform(keys)
"value" => GraphQL::Cardinal::HashKeyResolver.new("value"),
},
"Query" => {
**GraphQL::Cardinal::Introspection::ENTRYPOINT_RESOLVERS,
"products" => GraphQL::Cardinal::HashKeyResolver.new("products"),
"nodes" => GraphQL::Cardinal::HashKeyResolver.new("nodes"),
"node" => GraphQL::Cardinal::HashKeyResolver.new("node"),
Expand Down
Loading