Skip to content

Commit 87de45e

Browse files
committed
Apply camelization when initializing a field; use proper types for list and non-null; and type introspection
1 parent 3296d93 commit 87de45e

File tree

12 files changed

+131
-69
lines changed

12 files changed

+131
-69
lines changed

lib/graphql/deprecated_dsl.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ module DeprecatedDSL
2424

2525
def self.activate
2626
TYPE_CLASSES.each { |c| c.extend(Methods) }
27-
GraphQL::Schema::Member::ListTypeProxy.include(Methods)
28-
GraphQL::Schema::Member::NonNullTypeProxy.include(Methods)
27+
GraphQL::Schema::List.include(Methods)
28+
GraphQL::Schema::NonNull.include(Methods)
2929
end
3030
module Methods
3131
def !

lib/graphql/schema.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222

2323
require "graphql/schema/member"
24+
require "graphql/schema/list"
25+
require "graphql/schema/non_null"
2426
require "graphql/schema/argument"
2527
require "graphql/schema/enum_value"
2628
require "graphql/schema/enum"

lib/graphql/schema/field.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def initialize(name, return_type_expr = nil, desc = nil, owner: nil, null: nil,
7070
if return_type_expr.is_a?(GraphQL::Field)
7171
raise ArgumentError, "A GraphQL::Field was passed as the second argument, use the `field:` keyword for this instead."
7272
end
73-
@name = name.to_s
73+
@name = camelize ? Member::BuildType.camelize(name.to_s) : name.to_s
7474
if description && desc
7575
raise ArgumentError, "Provide description as a positional argument or `description:` keyword, but not both (#{desc.inspect}, #{description.inspect})"
7676
end
@@ -99,7 +99,6 @@ def initialize(name, return_type_expr = nil, desc = nil, owner: nil, null: nil,
9999
@max_page_size = max_page_size
100100
@introspection = introspection
101101
@extras = extras
102-
@camelize = camelize
103102
@mutation = mutation
104103
@mutation_class = mutation_class
105104
# Override the default from HasArguments
@@ -157,7 +156,7 @@ def to_graphql
157156
GraphQL::Field.new
158157
end
159158

160-
field_defn.name = @camelize ? Member::BuildType.camelize(name) : name
159+
field_defn.name = @name
161160
if @return_type_expr
162161
field_defn.type = -> { type }
163162
end

lib/graphql/schema/interface.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module DefinitionMethods
77
include GraphQL::Schema::Member::CachedGraphQLDefinition
88
include GraphQL::Relay::TypeExtensions
99
include GraphQL::Schema::Member::BaseDSLMethods
10+
include GraphQL::Schema::Member::TypeSystemHelpers
1011
include GraphQL::Schema::Member::HasFields
1112

1213
# Methods defined in this block will be:

lib/graphql/schema/list.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+
# Represents a list type in the schema.
6+
# Wraps a {Schema::Member} as a list type.
7+
# @see {Schema::Member::TypeSystemHelpers#to_list_type}
8+
class List
9+
include GraphQL::Schema::Member::CachedGraphQLDefinition
10+
include GraphQL::Schema::Member::TypeSystemHelpers
11+
12+
# @return [Class, Module] The inner type of this list, the type of which one or more objects may be present.
13+
attr_reader :of_type
14+
15+
def initialize(of_type)
16+
@of_type = of_type
17+
end
18+
19+
def to_graphql
20+
@of_type.graphql_definition.to_list_type
21+
end
22+
23+
end
24+
end
25+
end

lib/graphql/schema/member.rb

Lines changed: 2 additions & 2 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/type_system_helpers'
67
require "graphql/relay/type_extensions"
78

89
module GraphQL
@@ -16,12 +17,11 @@ class Member
1617
extend CachedGraphQLDefinition
1718
extend GraphQL::Relay::TypeExtensions
1819
extend BaseDSLMethods
20+
extend TypeSystemHelpers
1921
end
2022
end
2123
end
2224

23-
require 'graphql/schema/member/list_type_proxy'
24-
require 'graphql/schema/member/non_null_type_proxy'
2525
require 'graphql/schema/member/has_arguments'
2626
require 'graphql/schema/member/has_fields'
2727
require 'graphql/schema/member/instrumentation'

lib/graphql/schema/member/base_dsl_methods.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@ def to_graphql
6767
raise NotImplementedError
6868
end
6969

70-
# @return [ListTypeProxy] Make a list-type representation of this type
71-
def to_list_type
72-
ListTypeProxy.new(self)
73-
end
74-
75-
# @return [NonNullTypeProxy] Make a non-null-type representation of this type
76-
def to_non_null_type
77-
NonNullTypeProxy.new(self)
78-
end
79-
8070
def overridden_graphql_name
8171
@graphql_name || find_inherited_method(:overridden_graphql_name, nil)
8272
end

lib/graphql/schema/member/list_type_proxy.rb

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/graphql/schema/member/non_null_type_proxy.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
module GraphQL
4+
class Schema
5+
class Member
6+
module TypeSystemHelpers
7+
# @return [Schema::NonNull] Make a non-null-type representation of this type
8+
def to_non_null_type
9+
GraphQL::Schema::NonNull.new(self)
10+
end
11+
12+
# @return [Schema::List] Make a list-type representation of this type
13+
def to_list_type
14+
GraphQL::Schema::List.new(self)
15+
end
16+
17+
# @return [Boolean] true if this is a non-nullable type. A nullable list of non-nullables is considered nullable.
18+
def non_null?
19+
false
20+
end
21+
22+
# @return [Boolean] true if this is a list type. A non-nullable list is considered a list.
23+
def list?
24+
false
25+
end
26+
end
27+
end
28+
end
29+
end

lib/graphql/schema/non_null.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
module GraphQL
4+
class Schema
5+
# Wraps a {Schema::Member} when it is required.
6+
# @see {Schema::Member::TypeSystemHelpers#to_non_null_type}
7+
class NonNull
8+
include GraphQL::Schema::Member::CachedGraphQLDefinition
9+
include GraphQL::Schema::Member::TypeSystemHelpers
10+
attr_reader :of_type
11+
def initialize(of_type)
12+
@of_type = of_type
13+
end
14+
15+
def to_graphql
16+
@of_type.graphql_definition.to_non_null_type
17+
end
18+
19+
# @return [true]
20+
def non_null?
21+
true
22+
end
23+
24+
# @return [Boolean] True if this type wraps a list type
25+
def list?
26+
@of_type.list?
27+
end
28+
end
29+
end
30+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
require "spec_helper"
3+
4+
describe GraphQL::Schema::Member::TypeSystemHelpers do
5+
let(:object) {
6+
Class.new(GraphQL::Schema::Object) do
7+
graphql_name "Thing"
8+
9+
field :int, Integer, null: true
10+
field :int2, Integer, null: false
11+
field :int_list, [Integer], null: true
12+
field :int_list2, [Integer], null: false
13+
end
14+
}
15+
16+
let(:int_field) { object.fields["int"] }
17+
let(:int2_field) { object.fields["int2"] }
18+
let(:int_list_field) { object.fields["intList"] }
19+
let(:int_list2_field) { object.fields["intList2"] }
20+
21+
describe "#list?" do
22+
it "is true for lists, including non-null lists, otherwise false" do
23+
assert int_list_field.type.list?
24+
assert int_list2_field.type.list?
25+
refute int_field.type.list?
26+
refute int2_field.type.list?
27+
end
28+
end
29+
30+
describe "#non_null?" do
31+
it "is true for required types" do
32+
assert int2_field.type.non_null?
33+
assert int_list2_field.type.non_null?
34+
refute int_field.type.non_null?
35+
refute int_list_field.type.non_null?
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)