graphql 0.12.1 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/lib/graphql.rb +31 -41
  3. data/lib/graphql/argument.rb +23 -21
  4. data/lib/graphql/base_type.rb +5 -8
  5. data/lib/graphql/define/assign_argument.rb +5 -2
  6. data/lib/graphql/define/type_definer.rb +2 -1
  7. data/lib/graphql/directive.rb +34 -36
  8. data/lib/graphql/directive/include_directive.rb +3 -7
  9. data/lib/graphql/directive/skip_directive.rb +3 -7
  10. data/lib/graphql/enum_type.rb +78 -76
  11. data/lib/graphql/execution_error.rb +1 -3
  12. data/lib/graphql/field.rb +99 -95
  13. data/lib/graphql/input_object_type.rb +49 -47
  14. data/lib/graphql/interface_type.rb +31 -34
  15. data/lib/graphql/introspection.rb +19 -18
  16. data/lib/graphql/introspection/directive_location_enum.rb +8 -0
  17. data/lib/graphql/introspection/directive_type.rb +1 -3
  18. data/lib/graphql/introspection/field_type.rb +1 -1
  19. data/lib/graphql/introspection/fields_field.rb +1 -1
  20. data/lib/graphql/introspection/introspection_query.rb +1 -3
  21. data/lib/graphql/introspection/possible_types_field.rb +7 -1
  22. data/lib/graphql/introspection/schema_field.rb +13 -9
  23. data/lib/graphql/introspection/type_by_name_field.rb +13 -17
  24. data/lib/graphql/introspection/typename_field.rb +12 -8
  25. data/lib/graphql/language.rb +5 -9
  26. data/lib/graphql/language/lexer.rb +668 -0
  27. data/lib/graphql/language/lexer.rl +149 -0
  28. data/lib/graphql/language/parser.rb +842 -116
  29. data/lib/graphql/language/parser.y +264 -0
  30. data/lib/graphql/language/token.rb +21 -0
  31. data/lib/graphql/list_type.rb +33 -31
  32. data/lib/graphql/non_null_type.rb +33 -31
  33. data/lib/graphql/object_type.rb +52 -55
  34. data/lib/graphql/query.rb +83 -80
  35. data/lib/graphql/query/context.rb +5 -1
  36. data/lib/graphql/query/directive_resolution.rb +16 -0
  37. data/lib/graphql/query/executor.rb +3 -3
  38. data/lib/graphql/query/input_validation_result.rb +17 -15
  39. data/lib/graphql/query/serial_execution.rb +5 -5
  40. data/lib/graphql/query/serial_execution/execution_context.rb +4 -3
  41. data/lib/graphql/query/serial_execution/selection_resolution.rb +19 -21
  42. data/lib/graphql/query/serial_execution/value_resolution.rb +1 -1
  43. data/lib/graphql/query/type_resolver.rb +22 -18
  44. data/lib/graphql/query/variable_validation_error.rb +14 -12
  45. data/lib/graphql/schema.rb +87 -77
  46. data/lib/graphql/schema/each_item_validator.rb +16 -12
  47. data/lib/graphql/schema/field_validator.rb +14 -10
  48. data/lib/graphql/schema/implementation_validator.rb +26 -22
  49. data/lib/graphql/schema/middleware_chain.rb +2 -1
  50. data/lib/graphql/schema/possible_types.rb +34 -0
  51. data/lib/graphql/schema/printer.rb +122 -120
  52. data/lib/graphql/schema/type_expression.rb +1 -0
  53. data/lib/graphql/schema/type_map.rb +3 -10
  54. data/lib/graphql/schema/type_reducer.rb +65 -81
  55. data/lib/graphql/schema/type_validator.rb +45 -41
  56. data/lib/graphql/static_validation.rb +7 -9
  57. data/lib/graphql/static_validation/all_rules.rb +29 -24
  58. data/lib/graphql/static_validation/arguments_validator.rb +39 -35
  59. data/lib/graphql/static_validation/literal_validator.rb +44 -40
  60. data/lib/graphql/static_validation/message.rb +30 -26
  61. data/lib/graphql/static_validation/rules/argument_literals_are_compatible.rb +15 -11
  62. data/lib/graphql/static_validation/rules/arguments_are_defined.rb +14 -10
  63. data/lib/graphql/static_validation/rules/directives_are_defined.rb +16 -12
  64. data/lib/graphql/static_validation/rules/directives_are_in_valid_locations.rb +59 -0
  65. data/lib/graphql/static_validation/rules/fields_are_defined_on_type.rb +25 -21
  66. data/lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb +28 -24
  67. data/lib/graphql/static_validation/rules/fields_will_merge.rb +84 -80
  68. data/lib/graphql/static_validation/rules/fragment_spreads_are_possible.rb +49 -43
  69. data/lib/graphql/static_validation/rules/fragment_types_exist.rb +22 -17
  70. data/lib/graphql/static_validation/rules/fragments_are_finite.rb +19 -15
  71. data/lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb +25 -20
  72. data/lib/graphql/static_validation/rules/fragments_are_used.rb +36 -23
  73. data/lib/graphql/static_validation/rules/required_arguments_are_present.rb +29 -25
  74. data/lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb +21 -17
  75. data/lib/graphql/static_validation/rules/variable_usages_are_allowed.rb +79 -70
  76. data/lib/graphql/static_validation/rules/variables_are_input_types.rb +24 -20
  77. data/lib/graphql/static_validation/rules/variables_are_used_and_defined.rb +122 -119
  78. data/lib/graphql/static_validation/type_stack.rb +138 -129
  79. data/lib/graphql/static_validation/validator.rb +29 -25
  80. data/lib/graphql/type_kinds.rb +42 -40
  81. data/lib/graphql/union_type.rb +22 -16
  82. data/lib/graphql/version.rb +1 -1
  83. data/readme.md +12 -27
  84. data/spec/graphql/base_type_spec.rb +3 -3
  85. data/spec/graphql/directive_spec.rb +10 -18
  86. data/spec/graphql/enum_type_spec.rb +7 -7
  87. data/spec/graphql/execution_error_spec.rb +1 -1
  88. data/spec/graphql/field_spec.rb +14 -13
  89. data/spec/graphql/id_type_spec.rb +6 -6
  90. data/spec/graphql/input_object_type_spec.rb +39 -39
  91. data/spec/graphql/interface_type_spec.rb +16 -32
  92. data/spec/graphql/introspection/directive_type_spec.rb +5 -9
  93. data/spec/graphql/introspection/input_value_type_spec.rb +10 -4
  94. data/spec/graphql/introspection/introspection_query_spec.rb +2 -2
  95. data/spec/graphql/introspection/schema_type_spec.rb +2 -2
  96. data/spec/graphql/introspection/type_type_spec.rb +34 -6
  97. data/spec/graphql/language/parser_spec.rb +299 -105
  98. data/spec/graphql/language/visitor_spec.rb +4 -4
  99. data/spec/graphql/list_type_spec.rb +11 -11
  100. data/spec/graphql/object_type_spec.rb +10 -10
  101. data/spec/graphql/query/arguments_spec.rb +7 -7
  102. data/spec/graphql/query/context_spec.rb +11 -3
  103. data/spec/graphql/query/executor_spec.rb +26 -19
  104. data/spec/graphql/query/serial_execution/execution_context_spec.rb +6 -6
  105. data/spec/graphql/query/serial_execution/value_resolution_spec.rb +2 -2
  106. data/spec/graphql/query/type_resolver_spec.rb +3 -3
  107. data/spec/graphql/query_spec.rb +6 -38
  108. data/spec/graphql/scalar_type_spec.rb +28 -19
  109. data/spec/graphql/schema/field_validator_spec.rb +1 -1
  110. data/spec/graphql/schema/middleware_chain_spec.rb +12 -1
  111. data/spec/graphql/schema/printer_spec.rb +12 -4
  112. data/spec/graphql/schema/rescue_middleware_spec.rb +1 -1
  113. data/spec/graphql/schema/type_expression_spec.rb +2 -2
  114. data/spec/graphql/schema/type_reducer_spec.rb +21 -36
  115. data/spec/graphql/schema/type_validator_spec.rb +9 -9
  116. data/spec/graphql/schema_spec.rb +1 -1
  117. data/spec/graphql/static_validation/rules/argument_literals_are_compatible_spec.rb +4 -4
  118. data/spec/graphql/static_validation/rules/arguments_are_defined_spec.rb +4 -4
  119. data/spec/graphql/static_validation/rules/directives_are_defined_spec.rb +5 -5
  120. data/spec/graphql/static_validation/rules/directives_are_in_valid_locations_spec.rb +39 -0
  121. data/spec/graphql/static_validation/rules/fields_are_defined_on_type_spec.rb +5 -5
  122. data/spec/graphql/static_validation/rules/fields_have_appropriate_selections_spec.rb +4 -4
  123. data/spec/graphql/static_validation/rules/fields_will_merge_spec.rb +2 -2
  124. data/spec/graphql/static_validation/rules/fragment_spreads_are_possible_spec.rb +1 -1
  125. data/spec/graphql/static_validation/rules/fragment_types_exist_spec.rb +2 -2
  126. data/spec/graphql/static_validation/rules/fragments_are_finite_spec.rb +2 -2
  127. data/spec/graphql/static_validation/rules/fragments_are_on_composite_types_spec.rb +2 -2
  128. data/spec/graphql/static_validation/rules/fragments_are_used_spec.rb +3 -3
  129. data/spec/graphql/static_validation/rules/required_arguments_are_present_spec.rb +3 -3
  130. data/spec/graphql/static_validation/rules/variable_default_values_are_correctly_typed_spec.rb +5 -5
  131. data/spec/graphql/static_validation/rules/variable_usages_are_allowed_spec.rb +3 -1
  132. data/spec/graphql/static_validation/rules/variables_are_input_types_spec.rb +4 -4
  133. data/spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb +3 -3
  134. data/spec/graphql/static_validation/type_stack_spec.rb +3 -2
  135. data/spec/graphql/static_validation/validator_spec.rb +26 -6
  136. data/spec/graphql/union_type_spec.rb +5 -4
  137. data/spec/spec_helper.rb +2 -5
  138. data/spec/support/dairy_app.rb +30 -9
  139. data/spec/support/dairy_data.rb +1 -1
  140. data/spec/support/star_wars_data.rb +26 -26
  141. data/spec/support/star_wars_schema.rb +1 -1
  142. metadata +40 -21
  143. data/lib/graphql/language/transform.rb +0 -113
  144. data/lib/graphql/query/directive_chain.rb +0 -44
  145. data/lib/graphql/repl.rb +0 -27
  146. data/spec/graphql/language/transform_spec.rb +0 -156
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6682396f1618ee3660ec3120bf597c109d0e2752
4
- data.tar.gz: 93a3813ff37f3eb9f4e447eda9aabd3db2688de3
3
+ metadata.gz: e39609ec3ac4ee1756f3dcbd618785468f05d895
4
+ data.tar.gz: 96aac564a69c22795a299ea4430b1bb95542a788
5
5
  SHA512:
6
- metadata.gz: 09eb390035a47146f403a393c1695d7d6355a7d7573c70a277547113f2abad887cded1bb275fa7865c57df5631119487fdd3e50ddf859e705960fc0f67c8fe6e
7
- data.tar.gz: d4bef1a775db4a7bd2d58a60c283bce67942ab41bfb551d3d1a7f8080ecdcfc2959d7c0f51872cb80016e77cca68bd04fbede2479217c4b994e6919fb68f86e8
6
+ metadata.gz: 4ad5ecccde050131fe70cb6a33d6fdc1acb574a8e13e63af82da209313388f53fc042627f0ca32c00999e35fb227b0e1fd7068f69d7209ed5c2b20beb45a19cf
7
+ data.tar.gz: 8cecd7002d9067f04e5ac6a7381aab405d6e2acd9df5baf45976b994e49615e1fe9d94585df62e9728c7d8c8788ac7b408a40273308221f56e11af315816d74c
@@ -1,5 +1,4 @@
1
1
  require "json"
2
- require "parslet"
3
2
  require "set"
4
3
  require "singleton"
5
4
  require "forwardable"
@@ -22,57 +21,48 @@ module GraphQL
22
21
  # @param [String] a GraphQL query string
23
22
  # @return [GraphQL::Language::Nodes::Document]
24
23
  def self.parse(query_string)
25
- parse_with_parslet(query_string)
24
+ parse_with_racc(query_string)
26
25
  end
27
26
 
28
- def self.parse_with_parslet(string)
29
- tree = GraphQL::PARSER.parse(string)
30
- document = GraphQL::TRANSFORM.apply(tree)
31
- if !document.is_a?(GraphQL::Language::Nodes::Document)
32
- raise("Parse failed! Sorry, somehow we failed to turn this string into a document. Please report this bug!")
33
- end
34
- document
35
- rescue Parslet::ParseFailed => error
36
- line, col = error.cause.source.line_and_column(error.cause.pos)
37
- raise GraphQL::ParseError.new(error.message, line, col, string)
27
+ def self.parse_with_racc(string)
28
+ GraphQL::Language::Parser.parse(string)
38
29
  end
39
30
  end
40
31
 
41
32
  # Order matters for these:
42
33
 
43
- require 'graphql/define'
44
- require 'graphql/base_type'
45
- require 'graphql/object_type'
34
+ require "graphql/define"
35
+ require "graphql/base_type"
36
+ require "graphql/object_type"
46
37
 
47
- require 'graphql/enum_type'
48
- require 'graphql/input_object_type'
49
- require 'graphql/interface_type'
50
- require 'graphql/list_type'
51
- require 'graphql/non_null_type'
52
- require 'graphql/union_type'
38
+ require "graphql/enum_type"
39
+ require "graphql/input_object_type"
40
+ require "graphql/interface_type"
41
+ require "graphql/list_type"
42
+ require "graphql/non_null_type"
43
+ require "graphql/union_type"
53
44
 
54
- require 'graphql/argument'
55
- require 'graphql/field'
56
- require 'graphql/type_kinds'
45
+ require "graphql/argument"
46
+ require "graphql/field"
47
+ require "graphql/type_kinds"
57
48
 
58
- require 'graphql/scalar_type'
59
- require 'graphql/boolean_type'
60
- require 'graphql/float_type'
61
- require 'graphql/id_type'
62
- require 'graphql/int_type'
63
- require 'graphql/string_type'
49
+ require "graphql/scalar_type"
50
+ require "graphql/boolean_type"
51
+ require "graphql/float_type"
52
+ require "graphql/id_type"
53
+ require "graphql/int_type"
54
+ require "graphql/string_type"
55
+ require "graphql/directive"
64
56
 
65
- require 'graphql/introspection'
66
- require 'graphql/language'
67
- require 'graphql/directive'
68
- require 'graphql/schema'
69
- require 'graphql/schema/printer'
57
+ require "graphql/introspection"
58
+ require "graphql/language"
59
+ require "graphql/schema"
60
+ require "graphql/schema/printer"
70
61
 
71
62
  # Order does not matter for these:
72
63
 
73
- require 'graphql/execution_error'
74
- require 'graphql/invalid_null_error'
75
- require 'graphql/query'
76
- require 'graphql/repl'
77
- require 'graphql/static_validation'
78
- require 'graphql/version'
64
+ require "graphql/execution_error"
65
+ require "graphql/invalid_null_error"
66
+ require "graphql/query"
67
+ require "graphql/static_validation"
68
+ require "graphql/version"
@@ -1,23 +1,25 @@
1
- # Used for defined arguments ({Field}, {InputObjectType})
2
- #
3
- # {#name} must be a String.
4
- #
5
- # @example defining an argument for a field
6
- # GraphQL::Field.define do
7
- # # ...
8
- # argument :favoriteFood, types.String, "Favorite thing to eat", default_value: "pizza"
9
- # end
10
- #
11
- # @example defining an input field for an {InputObjectType}
12
- # GraphQL::InputObjectType.define do
13
- # input_field :newName, !types.String
14
- # end
15
- #
16
- class GraphQL::Argument
17
- include GraphQL::Define::InstanceDefinable
18
- accepts_definitions :name, :type, :description, :default_value
19
- attr_accessor :type, :description, :default_value
1
+ module GraphQL
2
+ # Used for defined arguments ({Field}, {InputObjectType})
3
+ #
4
+ # {#name} must be a String.
5
+ #
6
+ # @example defining an argument for a field
7
+ # GraphQL::Field.define do
8
+ # # ...
9
+ # argument :favoriteFood, types.String, "Favorite thing to eat", default_value: "pizza"
10
+ # end
11
+ #
12
+ # @example defining an input field for an {InputObjectType}
13
+ # GraphQL::InputObjectType.define do
14
+ # input_field :newName, !types.String
15
+ # end
16
+ #
17
+ class Argument
18
+ include GraphQL::Define::InstanceDefinable
19
+ accepts_definitions :name, :type, :description, :default_value
20
+ attr_accessor :type, :description, :default_value
20
21
 
21
- # @return [String] The name of this argument on its {GraphQL::Field} or {GraphQL::InputObjectType}
22
- attr_accessor :name
22
+ # @return [String] The name of this argument on its {GraphQL::Field} or {GraphQL::InputObjectType}
23
+ attr_accessor :name
24
+ end
23
25
  end
@@ -50,25 +50,22 @@ module GraphQL
50
50
  # Maybe you'll need to override this in your own interfaces!
51
51
  #
52
52
  # @param object [Object] the object which needs a type to expose it
53
+ # @param ctx [GraphQL::Query::Context]
53
54
  # @return [GraphQL::ObjectType] the type which should expose `object`
54
- def resolve_type(object)
55
- instance_exec(object, &(@resolve_type_proc || DEFAULT_RESOLVE_TYPE))
55
+ def resolve_type(object, ctx)
56
+ instance_exec(object, ctx, &(@resolve_type_proc || DEFAULT_RESOLVE_TYPE))
56
57
  end
57
58
 
58
59
  # The default implementation of {#resolve_type} gets `object.class.name`
59
60
  # and finds a type with the same name
60
- DEFAULT_RESOLVE_TYPE = -> (object) {
61
+ DEFAULT_RESOLVE_TYPE = -> (object, ctx) {
61
62
  type_name = object.class.name
62
- possible_types.find {|t| t.name == type_name}
63
+ ctx.schema.possible_types(self).find {|t| t.name == type_name}
63
64
  }
64
65
 
65
66
  def resolve_type=(new_proc)
66
67
  @resolve_type_proc = new_proc || DEFAULT_RESOLVE_TYPE
67
68
  end
68
-
69
- def include?(type)
70
- possible_types.include?(type)
71
- end
72
69
  end
73
70
 
74
71
  # Print the human-readable name of this type using the query-string naming pattern
@@ -10,8 +10,11 @@ module GraphQL
10
10
  end
11
11
  argument.name = name.to_s
12
12
  argument.type = type
13
- argument.description = description
14
- argument.default_value = default_value
13
+
14
+ description && argument.description = description
15
+ # check nil because false is a valid value
16
+ default_value != nil && argument.default_value = default_value
17
+
15
18
  target.arguments[name.to_s] = argument
16
19
  end
17
20
  end
@@ -5,12 +5,13 @@ module GraphQL
5
5
  # Passed into initialization blocks, eg {ObjectType#initialize}, {Field#initialize}
6
6
  class TypeDefiner
7
7
  include Singleton
8
-
8
+ # rubocop:disable Style/MethodName
9
9
  def Int; GraphQL::INT_TYPE; end
10
10
  def String; GraphQL::STRING_TYPE; end
11
11
  def Float; GraphQL::FLOAT_TYPE; end
12
12
  def Boolean; GraphQL::BOOLEAN_TYPE; end
13
13
  def ID; GraphQL::ID_TYPE; end
14
+ # rubocop:enable Style/MethodName
14
15
 
15
16
  # Make a {ListType} which wraps the input type
16
17
  #
@@ -1,39 +1,37 @@
1
- # This implementation of `Directive` is ... not robust.
2
- # It seems like this area of the spec is still getting worked out, so
3
- # {Directive} & {DirectiveChain} implement `@skip` and `@include` with
4
- # minimal impact on query execution.
5
- class GraphQL::Directive
6
- include GraphQL::Define::InstanceDefinable
7
- accepts_definitions :on, :name, :description, :resolve, argument: GraphQL::Define::AssignArgument
8
-
9
- attr_accessor :on, :arguments, :name, :description
10
-
11
- LOCATIONS = [
12
- ON_OPERATION = :on_operation?,
13
- ON_FRAGMENT = :on_fragment?,
14
- ON_FIELD = :on_field?,
15
- ]
16
-
17
- LOCATIONS.each do |location|
18
- define_method(location) { self.on.include?(location) }
19
- end
20
-
21
- def initialize
22
- @arguments = {}
23
- end
24
-
25
- def resolve(arguments, proc)
26
- @resolve_proc.call(arguments, proc)
27
- end
28
-
29
- def resolve=(resolve_proc)
30
- @resolve_proc = resolve_proc
31
- end
32
-
33
- def to_s
34
- "<GraphQL::Directive #{name}>"
1
+ module GraphQL
2
+ class Directive
3
+ include GraphQL::Define::InstanceDefinable
4
+ accepts_definitions :locations, :name, :description, :include_proc, argument: GraphQL::Define::AssignArgument
5
+
6
+ attr_accessor :locations, :arguments, :name, :description
7
+
8
+ LOCATIONS = [
9
+ QUERY = :QUERY,
10
+ MUTATION = :MUTATION,
11
+ SUBSCRIPTION = :SUBSCRIPTION,
12
+ FIELD = :FIELD,
13
+ FRAGMENT_DEFINITION = :FRAGMENT_DEFINITION,
14
+ FRAGMENT_SPREAD = :FRAGMENT_SPREAD,
15
+ INLINE_FRAGMENT = :INLINE_FRAGMENT,
16
+ ]
17
+
18
+ def initialize
19
+ @arguments = {}
20
+ end
21
+
22
+ def include?(arguments)
23
+ @include_proc.call(arguments)
24
+ end
25
+
26
+ def include_proc=(include_proc)
27
+ @include_proc = include_proc
28
+ end
29
+
30
+ def to_s
31
+ "<GraphQL::Directive #{name}>"
32
+ end
35
33
  end
36
34
  end
37
35
 
38
- require 'graphql/directive/include_directive'
39
- require 'graphql/directive/skip_directive'
36
+ require "graphql/directive/include_directive"
37
+ require "graphql/directive/skip_directive"
@@ -1,14 +1,10 @@
1
1
  GraphQL::Directive::IncludeDirective = GraphQL::Directive.define do
2
2
  name "include"
3
3
  description "Include this part of the query if `if` is true"
4
- on([GraphQL::Directive::ON_FIELD, GraphQL::Directive::ON_FRAGMENT])
4
+ locations([GraphQL::Directive::FIELD, GraphQL::Directive::FRAGMENT_SPREAD, GraphQL::Directive::INLINE_FRAGMENT])
5
5
  argument :if, !GraphQL::BOOLEAN_TYPE
6
6
 
7
- resolve -> (arguments, proc) {
8
- if arguments["if"]
9
- proc.call
10
- else
11
- nil
12
- end
7
+ include_proc -> (arguments) {
8
+ arguments["if"]
13
9
  }
14
10
  end
@@ -1,15 +1,11 @@
1
1
  GraphQL::Directive::SkipDirective = GraphQL::Directive.define do
2
2
  name "skip"
3
3
  description "Ignore this part of the query if `if` is true"
4
- on([GraphQL::Directive::ON_FIELD, GraphQL::Directive::ON_FRAGMENT])
4
+ locations([GraphQL::Directive::FIELD, GraphQL::Directive::FRAGMENT_SPREAD, GraphQL::Directive::INLINE_FRAGMENT])
5
5
 
6
6
  argument :if, !GraphQL::BOOLEAN_TYPE
7
7
 
8
- resolve -> (arguments, proc) {
9
- if !arguments["if"]
10
- proc.call
11
- else
12
- nil
13
- end
8
+ include_proc -> (arguments) {
9
+ !arguments["if"]
14
10
  }
15
11
  end
@@ -1,92 +1,94 @@
1
- # A finite set of possible values, represented in query strings with
2
- # SCREAMING_CASE_NAMES
3
- #
4
- # @example An enum of programming languages
5
- #
6
- # LanguageEnum = GraphQL::EnumType.define do
7
- # name "Languages"
8
- # description "Programming languages for Web projects"
9
- # value("PYTHON", "A dynamic, function-oriented language")
10
- # value("RUBY", "A very dynamic language aimed at programmer happiness")
11
- # value("JAVASCRIPT", "Accidental lingua franca of the web")
12
- # end
13
- class GraphQL::EnumType < GraphQL::BaseType
14
- accepts_definitions value: GraphQL::Define::AssignEnumValue
1
+ module GraphQL
2
+ # A finite set of possible values, represented in query strings with
3
+ # SCREAMING_CASE_NAMES
4
+ #
5
+ # @example An enum of programming languages
6
+ #
7
+ # LanguageEnum = GraphQL::EnumType.define do
8
+ # name "Languages"
9
+ # description "Programming languages for Web projects"
10
+ # value("PYTHON", "A dynamic, function-oriented language")
11
+ # value("RUBY", "A very dynamic language aimed at programmer happiness")
12
+ # value("JAVASCRIPT", "Accidental lingua franca of the web")
13
+ # end
14
+ class EnumType < GraphQL::BaseType
15
+ accepts_definitions value: GraphQL::Define::AssignEnumValue
15
16
 
16
- def initialize
17
- @values_by_name = {}
18
- @values_by_value = {}
19
- end
17
+ def initialize
18
+ @values_by_name = {}
19
+ @values_by_value = {}
20
+ end
20
21
 
21
- def values=(values)
22
- @values_by_name = {}
23
- @values_by_value = {}
24
- values.each { |enum_value| add_value(enum_value) }
25
- end
22
+ def values=(values)
23
+ @values_by_name = {}
24
+ @values_by_value = {}
25
+ values.each { |enum_value| add_value(enum_value) }
26
+ end
26
27
 
27
- def add_value(enum_value)
28
- @values_by_name[enum_value.name] = enum_value
29
- @values_by_value[enum_value.value] = enum_value
30
- end
28
+ def add_value(enum_value)
29
+ @values_by_name[enum_value.name] = enum_value
30
+ @values_by_value[enum_value.value] = enum_value
31
+ end
31
32
 
32
- def values
33
- @values_by_name
34
- end
33
+ def values
34
+ @values_by_name
35
+ end
35
36
 
36
- # Define a value within this enum
37
- # @deprecated use {.define} API instead
38
- # @param name [String] the string representation of this value
39
- # @param description [String]
40
- # @param deprecation_reason [String] if provided, `deprecated?` will be true
41
- # @param value [Object] the underlying value for this enum value
42
- def value(name, description=nil, deprecation_reason: nil, value: name)
43
- values[name] = EnumValue.new(name: name, description: description, deprecation_reason: deprecation_reason, value: value)
44
- end
37
+ # Define a value within this enum
38
+ # @deprecated use {.define} API instead
39
+ # @param name [String] the string representation of this value
40
+ # @param description [String]
41
+ # @param deprecation_reason [String] if provided, `deprecated?` will be true
42
+ # @param value [Object] the underlying value for this enum value
43
+ def value(name, description=nil, deprecation_reason: nil, value: name)
44
+ values[name] = EnumValue.new(name: name, description: description, deprecation_reason: deprecation_reason, value: value)
45
+ end
45
46
 
46
- def kind
47
- GraphQL::TypeKinds::ENUM
48
- end
47
+ def kind
48
+ GraphQL::TypeKinds::ENUM
49
+ end
49
50
 
50
- def validate_non_null_input(value_name)
51
- result = GraphQL::Query::InputValidationResult.new
51
+ def validate_non_null_input(value_name)
52
+ result = GraphQL::Query::InputValidationResult.new
52
53
 
53
- if !@values_by_name.key?(value_name)
54
- result.add_problem("Expected #{JSON.dump(value_name)} to be one of: #{@values_by_name.keys.join(', ')}")
55
- end
54
+ if !@values_by_name.key?(value_name)
55
+ result.add_problem("Expected #{JSON.dump(value_name)} to be one of: #{@values_by_name.keys.join(', ')}")
56
+ end
56
57
 
57
- result
58
- end
58
+ result
59
+ end
59
60
 
60
- # Get the underlying value for this enum value
61
- #
62
- # @example get episode value from Enum
63
- # episode = EpisodeEnum.coerce("NEWHOPE")
64
- # episode # => 6
65
- #
66
- # @param value_name [String] the string representation of this enum value
67
- # @return [Object] the underlying value for this enum value
68
- def coerce_non_null_input(value_name)
69
- @values_by_name.fetch(value_name).value
70
- end
61
+ # Get the underlying value for this enum value
62
+ #
63
+ # @example get episode value from Enum
64
+ # episode = EpisodeEnum.coerce("NEWHOPE")
65
+ # episode # => 6
66
+ #
67
+ # @param value_name [String] the string representation of this enum value
68
+ # @return [Object] the underlying value for this enum value
69
+ def coerce_non_null_input(value_name)
70
+ @values_by_name.fetch(value_name).value
71
+ end
71
72
 
72
- def coerce_result(value)
73
- @values_by_value.fetch(value).name
74
- end
73
+ def coerce_result(value)
74
+ @values_by_value.fetch(value).name
75
+ end
75
76
 
76
- def to_s
77
- name
78
- end
77
+ def to_s
78
+ name
79
+ end
79
80
 
80
- # A value within an {EnumType}
81
- #
82
- # Created with {EnumType#value}
83
- class EnumValue
84
- attr_reader :name, :description, :deprecation_reason, :value
85
- def initialize(name:, description:, deprecation_reason:, value:)
86
- @name = name
87
- @description = description
88
- @deprecation_reason = deprecation_reason
89
- @value = value
81
+ # A value within an {EnumType}
82
+ #
83
+ # Created with {EnumType#value}
84
+ class EnumValue
85
+ attr_reader :name, :description, :deprecation_reason, :value
86
+ def initialize(name:, description:, deprecation_reason:, value:)
87
+ @name = name
88
+ @description = description
89
+ @deprecation_reason = deprecation_reason
90
+ @value = value
91
+ end
90
92
  end
91
93
  end
92
94
  end