Skip to content

Commit 15a1f52

Browse files
committed
Add .path method to schema members for debugging
1 parent 54edbe0 commit 15a1f52

18 files changed

+117
-1
lines changed

lib/graphql/schema/argument.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class Schema
44
class Argument
55
include GraphQL::Schema::Member::CachedGraphQLDefinition
66
include GraphQL::Schema::Member::AcceptsDefinition
7+
include GraphQL::Schema::Member::HasPath
78

89
NO_DEFAULT = :__no_default__
910

lib/graphql/schema/enum_value.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Schema
2727
# end
2828
class EnumValue < GraphQL::Schema::Member
2929
include GraphQL::Schema::Member::AcceptsDefinition
30+
include GraphQL::Schema::Member::HasPath
3031

3132
attr_reader :graphql_name
3233

lib/graphql/schema/field.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Field
66
include GraphQL::Schema::Member::CachedGraphQLDefinition
77
include GraphQL::Schema::Member::AcceptsDefinition
88
include GraphQL::Schema::Member::HasArguments
9+
include GraphQL::Schema::Member::HasPath
910

1011
# @return [String] the GraphQL name for this field, camelized unless `camelize: false` is provided
1112
attr_reader :name

lib/graphql/schema/interface.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module DefinitionMethods
99
include GraphQL::Schema::Member::BaseDSLMethods
1010
include GraphQL::Schema::Member::TypeSystemHelpers
1111
include GraphQL::Schema::Member::HasFields
12+
include GraphQL::Schema::Member::HasPath
1213
include GraphQL::Schema::Member::RelayShortcuts
1314
include GraphQL::Schema::Member::Scoped
1415

lib/graphql/schema/member.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'graphql/schema/member/base_dsl_methods'
44
require 'graphql/schema/member/cached_graphql_definition'
55
require 'graphql/schema/member/graphql_type_names'
6+
require 'graphql/schema/member/has_path'
67
require 'graphql/schema/member/relay_shortcuts'
78
require 'graphql/schema/member/scoped'
89
require 'graphql/schema/member/type_system_helpers'
@@ -22,6 +23,7 @@ class Member
2223
extend TypeSystemHelpers
2324
extend Scoped
2425
extend RelayShortcuts
26+
extend HasPath
2527
end
2628
end
2729
end

lib/graphql/schema/member/base_dsl_methods.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def authorized?(object, context)
108108
end
109109
end
110110

111+
112+
111113
private
112114

113115
def find_inherited_method(method_name, default_value)

lib/graphql/schema/member/has_path.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module GraphQL
4+
class Schema
5+
class Member
6+
module HasPath
7+
# @return [String] A description of this member's place in the GraphQL schema
8+
def path
9+
path_str = if self.respond_to?(:graphql_name)
10+
self.graphql_name
11+
elsif self.class.respond_to?(:graphql_name)
12+
# Instances of resolvers
13+
self.class.graphql_name
14+
end
15+
16+
if self.respond_to?(:owner) && owner.respond_to?(:path)
17+
path_str = "#{owner.path}.#{path_str}"
18+
end
19+
20+
path_str
21+
end
22+
end
23+
end
24+
end
25+
end

lib/graphql/schema/resolver.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Resolver
2323
# Really we only need description from here, but:
2424
extend Schema::Member::BaseDSLMethods
2525
extend GraphQL::Schema::Member::HasArguments
26+
include Schema::Member::HasPath
27+
extend Schema::Member::HasPath
2628

2729
# @param object [Object] the initialize object, pass to {Query.initialize} as `root_value`
2830
# @param context [GraphQL::Query::Context]

spec/graphql/schema/argument_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ class Schema < GraphQL::Schema
2929
end
3030
end
3131

32-
32+
describe "#path" do
33+
it "includes type, field and argument names" do
34+
assert_equal "Query.field.argWithBlock", SchemaArgumentTest::Query.fields["field"].arguments["argWithBlock"].path
35+
end
36+
end
3337

3438
describe "#name" do
3539
it "reflects camelization" do

spec/graphql/schema/enum_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
describe GraphQL::Schema::Enum do
55
let(:enum) { Jazz::Family }
6+
7+
describe ".path" do
8+
it "is the name" do
9+
assert_equal "Family", enum.path
10+
end
11+
end
12+
613
describe "type info" do
714
it "tells about the definition" do
815
assert_equal "Family", enum.graphql_name

spec/graphql/schema/enum_value_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
require "spec_helper"
33

44
describe GraphQL::Schema::EnumValue do
5+
describe "#path" do
6+
it "Has the owner name too" do
7+
enum = Class.new(GraphQL::Schema::Enum) do
8+
graphql_name "Abc"
9+
value(:XYZ)
10+
end
11+
12+
assert_equal "Abc.XYZ", enum.values["XYZ"].path
13+
end
14+
end
15+
516
describe "#deprecation_reason" do
617
it "can be written and read" do
718
enum_value = GraphQL::Schema::EnumValue.new(:x, owner: nil)

spec/graphql/schema/field_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
let(:object_class) { Jazz::Query }
77
let(:field) { object_class.fields["inspectInput"] }
88

9+
describe "path" do
10+
it "is the object/interface and field name" do
11+
assert_equal "Query.inspectInput", field.path
12+
assert_equal "GloballyIdentifiable.id", Jazz::GloballyIdentifiableType.fields["id"].path
13+
end
14+
end
15+
916
it "uses the argument class" do
1017
arg_defn = field.graphql_definition.arguments.values.first
1118
assert_equal :ok, arg_defn.metadata[:custom]

spec/graphql/schema/input_object_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33

44
describe GraphQL::Schema::InputObject do
55
let(:input_object) { Jazz::EnsembleInput }
6+
7+
describe ".path" do
8+
it "is the name" do
9+
assert_equal "EnsembleInput", input_object.path
10+
end
11+
12+
it "is used in argument paths" do
13+
assert_equal "EnsembleInput.name", input_object.arguments["name"].path
14+
end
15+
end
16+
617
describe "type info" do
718
it "has it" do
819
assert_equal "EnsembleInput", input_object.graphql_name

spec/graphql/schema/interface_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
describe GraphQL::Schema::Interface do
55
let(:interface) { Jazz::GloballyIdentifiableType }
66

7+
describe ".path" do
8+
it "is the name" do
9+
assert_equal "GloballyIdentifiable", interface.path
10+
end
11+
end
12+
713
describe "type info" do
814
it "tells its type info" do
915
assert_equal "GloballyIdentifiable", interface.graphql_name

spec/graphql/schema/object_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
assert object_class.respond_to?(:connection_type)
1515
end
1616

17+
describe "path" do
18+
it "is the type name" do
19+
assert_equal "Ensemble", object_class.path
20+
end
21+
end
22+
1723
it "inherits fields and interfaces" do
1824
new_object_class = Class.new(object_class) do
1925
field :newField, String, null: true

spec/graphql/schema/resolver_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,22 @@ def exec_query(*args)
291291
ResolverTest::Schema.execute(*args)
292292
end
293293

294+
describe ".path" do
295+
it "is the name" do
296+
assert_equal "Resolver1", ResolverTest::Resolver1.path
297+
end
298+
299+
it "is used for arguments and fields" do
300+
assert_equal "Resolver1.value", ResolverTest::Resolver1.arguments["value"].path
301+
assert_equal "PrepResolver7.int", ResolverTest::PrepResolver7.fields["int"].path
302+
end
303+
304+
it "works on instances" do
305+
r = ResolverTest::Resolver1.new(object: nil, context: nil)
306+
assert_equal "Resolver1", r.path
307+
end
308+
end
309+
294310
it "gets initialized for each resolution" do
295311
# State isn't shared between calls:
296312
res = exec_query " { r1: resolver1(value: 1) r2: resolver1 }"

spec/graphql/schema/scalar_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
require "spec_helper"
33

44
describe GraphQL::Schema::Scalar do
5+
describe ".path" do
6+
it "is the name" do
7+
assert_equal "String", GraphQL::Types::String.path
8+
end
9+
end
10+
511
describe "in queries" do
612
it "becomes output" do
713
query_str = <<-GRAPHQL

spec/graphql/schema/union_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
describe GraphQL::Schema::Union do
55
let(:union) { Jazz::PerformingAct }
6+
7+
describe ".path" do
8+
it "is the name" do
9+
assert_equal "PerformingAct", union.path
10+
end
11+
end
12+
613
describe "type info" do
714
it "has some" do
815
assert_equal 2, union.possible_types.size

0 commit comments

Comments
 (0)