Skip to content

Commit 8981e77

Browse files
author
Robert Mosolgo
authored
Merge pull request rmosolgo#1446 from rmosolgo/type-cleanup
Improve 1.8 type inspection
2 parents d256dd2 + 6c1c881 commit 8981e77

22 files changed

+242
-79
lines changed

.rubocop.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ AllCops:
1010

1111
# def ...
1212
# end
13-
Lint/DefEndAlignment:
13+
Layout/DefEndAlignment:
1414
EnforcedStyleAlignWith: def
1515

1616
# value = if
1717
# # ...
1818
# end
19-
Lint/EndAlignment:
19+
Layout/EndAlignment:
2020
EnforcedStyleAlignWith: variable
2121

2222
Lint/UselessAssignment:

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/argument.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Argument
77

88
NO_DEFAULT = :__no_default__
99

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

1213
# @return [GraphQL::Schema::Field, Class] The field or input object this argument belongs to
@@ -20,12 +21,11 @@ class Argument
2021
# @param default_value [Object]
2122
# @param camelize [Boolean] if true, the name will be camelized when building the schema
2223
def initialize(arg_name, type_expr, desc = nil, required:, description: nil, default_value: NO_DEFAULT, camelize: true, owner:, &definition_block)
23-
@name = arg_name.to_s
24+
@name = camelize ? Member::BuildType.camelize(arg_name.to_s) : arg_name.to_s
2425
@type_expr = type_expr
2526
@description = desc || description
2627
@null = !required
2728
@default_value = default_value
28-
@camelize = camelize
2929
@owner = owner
3030

3131
if definition_block
@@ -43,7 +43,7 @@ def description(text = nil)
4343

4444
def to_graphql
4545
argument = GraphQL::Argument.new
46-
argument.name = @camelize ? Member::BuildType.camelize(@name) : @name
46+
argument.name = @name
4747
argument.type = -> { type }
4848
argument.description = @description
4949
argument.metadata[:type_class] = self

lib/graphql/schema/enum.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def enum_value_class(new_enum_value_class = nil)
6868
@enum_value_class || (superclass <= GraphQL::Schema::Enum ? superclass.enum_value_class : nil)
6969
end
7070

71+
def kind
72+
GraphQL::TypeKinds::ENUM
73+
end
74+
7175
private
7276

7377
def own_values

lib/graphql/schema/field.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Field
77
include GraphQL::Schema::Member::AcceptsDefinition
88
include GraphQL::Schema::Member::HasArguments
99

10-
# @return [String]
10+
# @return [String] the GraphQL name for this field, camelized unless `camelize: false` is provided
1111
attr_reader :name
1212

1313
# @return [String]
@@ -67,7 +67,10 @@ def initialize(name, return_type_expr = nil, desc = nil, owner: nil, null: nil,
6767
if (field || function || resolve || resolve) && extras.any?
6868
raise ArgumentError, "keyword `extras:` may only be used with method-based resolve, please remove `field:`, `function:`, `resolve:`, or `mutation:`"
6969
end
70-
@name = name.to_s
70+
if return_type_expr.is_a?(GraphQL::Field)
71+
raise ArgumentError, "A GraphQL::Field was passed as the second argument, use the `field:` keyword for this instead."
72+
end
73+
@name = camelize ? Member::BuildType.camelize(name.to_s) : name.to_s
7174
if description && desc
7275
raise ArgumentError, "Provide description as a positional argument or `description:` keyword, but not both (#{desc.inspect}, #{description.inspect})"
7376
end
@@ -96,7 +99,6 @@ def initialize(name, return_type_expr = nil, desc = nil, owner: nil, null: nil,
9699
@max_page_size = max_page_size
97100
@introspection = introspection
98101
@extras = extras
99-
@camelize = camelize
100102
@mutation = mutation
101103
@mutation_class = mutation_class
102104
# Override the default from HasArguments
@@ -154,7 +156,7 @@ def to_graphql
154156
GraphQL::Field.new
155157
end
156158

157-
field_defn.name = @camelize ? Member::BuildType.camelize(name) : name
159+
field_defn.name = @name
158160
if @return_type_expr
159161
field_defn.type = -> { type }
160162
end

lib/graphql/schema/input_object.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def to_graphql
7070
type_defn.arguments_class = self
7171
type_defn
7272
end
73+
74+
def kind
75+
GraphQL::TypeKinds::INPUT_OBJECT
76+
end
7377
end
7478
end
7579
end

lib/graphql/schema/interface.rb

Lines changed: 5 additions & 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:
@@ -66,6 +67,10 @@ def to_graphql
6667
type_defn
6768
end
6869

70+
def kind
71+
GraphQL::TypeKinds::INTERFACE
72+
end
73+
6974
protected
7075

7176
def own_interfaces

lib/graphql/schema/list.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
def kind
24+
GraphQL::TypeKinds::LIST
25+
end
26+
end
27+
end
28+
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/build_type.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def parse_type(type_expr, null:)
4242
if maybe_type.respond_to?(:graphql_definition)
4343
maybe_type
4444
else
45-
raise "Unexpected class/module found for GraphQL type: #{type_expr} (must be type definition class/module)"
45+
raise ArgumentError, "Unexpected class/module found for GraphQL type: #{type_expr} (must be type definition class/module)"
4646
end
4747
end
4848
end
@@ -72,6 +72,8 @@ def parse_type(type_expr, null:)
7272
# Eg `String` => GraphQL::STRING_TYPE
7373
parse_type(type_expr.name, null: true)
7474
end
75+
when false
76+
raise ArgumentError, "Received `false` instead of a type, maybe a `!` should be replaced with `null: true` (for fields) or `required: true` (for arguments)"
7577
end
7678

7779
if return_type.nil?

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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
27+
# @return [GraphQL::TypeKinds::TypeKind]
28+
def kind
29+
raise NotImplementedError
30+
end
31+
end
32+
end
33+
end
34+
end

lib/graphql/schema/non_null.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
29+
def kind
30+
GraphQL::TypeKinds::NON_NULL
31+
end
32+
end
33+
end
34+
end

lib/graphql/schema/object.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def to_graphql
7171

7272
obj_type
7373
end
74+
75+
def kind
76+
GraphQL::TypeKinds::OBJECT
77+
end
7478
end
7579
end
7680
end

lib/graphql/schema/scalar.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def to_graphql
2525
type_defn.metadata[:type_class] = self
2626
type_defn
2727
end
28+
29+
def kind
30+
GraphQL::TypeKinds::SCALAR
31+
end
2832
end
2933
end
3034
end

lib/graphql/schema/union.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def to_graphql
2626
type_defn.metadata[:type_class] = self
2727
type_defn
2828
end
29+
30+
def kind
31+
GraphQL::TypeKinds::UNION
32+
end
2933
end
3034
end
3135
end

0 commit comments

Comments
 (0)