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.
- checksums.yaml +4 -4
- data/lib/graphql.rb +31 -41
- data/lib/graphql/argument.rb +23 -21
- data/lib/graphql/base_type.rb +5 -8
- data/lib/graphql/define/assign_argument.rb +5 -2
- data/lib/graphql/define/type_definer.rb +2 -1
- data/lib/graphql/directive.rb +34 -36
- data/lib/graphql/directive/include_directive.rb +3 -7
- data/lib/graphql/directive/skip_directive.rb +3 -7
- data/lib/graphql/enum_type.rb +78 -76
- data/lib/graphql/execution_error.rb +1 -3
- data/lib/graphql/field.rb +99 -95
- data/lib/graphql/input_object_type.rb +49 -47
- data/lib/graphql/interface_type.rb +31 -34
- data/lib/graphql/introspection.rb +19 -18
- data/lib/graphql/introspection/directive_location_enum.rb +8 -0
- data/lib/graphql/introspection/directive_type.rb +1 -3
- data/lib/graphql/introspection/field_type.rb +1 -1
- data/lib/graphql/introspection/fields_field.rb +1 -1
- data/lib/graphql/introspection/introspection_query.rb +1 -3
- data/lib/graphql/introspection/possible_types_field.rb +7 -1
- data/lib/graphql/introspection/schema_field.rb +13 -9
- data/lib/graphql/introspection/type_by_name_field.rb +13 -17
- data/lib/graphql/introspection/typename_field.rb +12 -8
- data/lib/graphql/language.rb +5 -9
- data/lib/graphql/language/lexer.rb +668 -0
- data/lib/graphql/language/lexer.rl +149 -0
- data/lib/graphql/language/parser.rb +842 -116
- data/lib/graphql/language/parser.y +264 -0
- data/lib/graphql/language/token.rb +21 -0
- data/lib/graphql/list_type.rb +33 -31
- data/lib/graphql/non_null_type.rb +33 -31
- data/lib/graphql/object_type.rb +52 -55
- data/lib/graphql/query.rb +83 -80
- data/lib/graphql/query/context.rb +5 -1
- data/lib/graphql/query/directive_resolution.rb +16 -0
- data/lib/graphql/query/executor.rb +3 -3
- data/lib/graphql/query/input_validation_result.rb +17 -15
- data/lib/graphql/query/serial_execution.rb +5 -5
- data/lib/graphql/query/serial_execution/execution_context.rb +4 -3
- data/lib/graphql/query/serial_execution/selection_resolution.rb +19 -21
- data/lib/graphql/query/serial_execution/value_resolution.rb +1 -1
- data/lib/graphql/query/type_resolver.rb +22 -18
- data/lib/graphql/query/variable_validation_error.rb +14 -12
- data/lib/graphql/schema.rb +87 -77
- data/lib/graphql/schema/each_item_validator.rb +16 -12
- data/lib/graphql/schema/field_validator.rb +14 -10
- data/lib/graphql/schema/implementation_validator.rb +26 -22
- data/lib/graphql/schema/middleware_chain.rb +2 -1
- data/lib/graphql/schema/possible_types.rb +34 -0
- data/lib/graphql/schema/printer.rb +122 -120
- data/lib/graphql/schema/type_expression.rb +1 -0
- data/lib/graphql/schema/type_map.rb +3 -10
- data/lib/graphql/schema/type_reducer.rb +65 -81
- data/lib/graphql/schema/type_validator.rb +45 -41
- data/lib/graphql/static_validation.rb +7 -9
- data/lib/graphql/static_validation/all_rules.rb +29 -24
- data/lib/graphql/static_validation/arguments_validator.rb +39 -35
- data/lib/graphql/static_validation/literal_validator.rb +44 -40
- data/lib/graphql/static_validation/message.rb +30 -26
- data/lib/graphql/static_validation/rules/argument_literals_are_compatible.rb +15 -11
- data/lib/graphql/static_validation/rules/arguments_are_defined.rb +14 -10
- data/lib/graphql/static_validation/rules/directives_are_defined.rb +16 -12
- data/lib/graphql/static_validation/rules/directives_are_in_valid_locations.rb +59 -0
- data/lib/graphql/static_validation/rules/fields_are_defined_on_type.rb +25 -21
- data/lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb +28 -24
- data/lib/graphql/static_validation/rules/fields_will_merge.rb +84 -80
- data/lib/graphql/static_validation/rules/fragment_spreads_are_possible.rb +49 -43
- data/lib/graphql/static_validation/rules/fragment_types_exist.rb +22 -17
- data/lib/graphql/static_validation/rules/fragments_are_finite.rb +19 -15
- data/lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb +25 -20
- data/lib/graphql/static_validation/rules/fragments_are_used.rb +36 -23
- data/lib/graphql/static_validation/rules/required_arguments_are_present.rb +29 -25
- data/lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb +21 -17
- data/lib/graphql/static_validation/rules/variable_usages_are_allowed.rb +79 -70
- data/lib/graphql/static_validation/rules/variables_are_input_types.rb +24 -20
- data/lib/graphql/static_validation/rules/variables_are_used_and_defined.rb +122 -119
- data/lib/graphql/static_validation/type_stack.rb +138 -129
- data/lib/graphql/static_validation/validator.rb +29 -25
- data/lib/graphql/type_kinds.rb +42 -40
- data/lib/graphql/union_type.rb +22 -16
- data/lib/graphql/version.rb +1 -1
- data/readme.md +12 -27
- data/spec/graphql/base_type_spec.rb +3 -3
- data/spec/graphql/directive_spec.rb +10 -18
- data/spec/graphql/enum_type_spec.rb +7 -7
- data/spec/graphql/execution_error_spec.rb +1 -1
- data/spec/graphql/field_spec.rb +14 -13
- data/spec/graphql/id_type_spec.rb +6 -6
- data/spec/graphql/input_object_type_spec.rb +39 -39
- data/spec/graphql/interface_type_spec.rb +16 -32
- data/spec/graphql/introspection/directive_type_spec.rb +5 -9
- data/spec/graphql/introspection/input_value_type_spec.rb +10 -4
- data/spec/graphql/introspection/introspection_query_spec.rb +2 -2
- data/spec/graphql/introspection/schema_type_spec.rb +2 -2
- data/spec/graphql/introspection/type_type_spec.rb +34 -6
- data/spec/graphql/language/parser_spec.rb +299 -105
- data/spec/graphql/language/visitor_spec.rb +4 -4
- data/spec/graphql/list_type_spec.rb +11 -11
- data/spec/graphql/object_type_spec.rb +10 -10
- data/spec/graphql/query/arguments_spec.rb +7 -7
- data/spec/graphql/query/context_spec.rb +11 -3
- data/spec/graphql/query/executor_spec.rb +26 -19
- data/spec/graphql/query/serial_execution/execution_context_spec.rb +6 -6
- data/spec/graphql/query/serial_execution/value_resolution_spec.rb +2 -2
- data/spec/graphql/query/type_resolver_spec.rb +3 -3
- data/spec/graphql/query_spec.rb +6 -38
- data/spec/graphql/scalar_type_spec.rb +28 -19
- data/spec/graphql/schema/field_validator_spec.rb +1 -1
- data/spec/graphql/schema/middleware_chain_spec.rb +12 -1
- data/spec/graphql/schema/printer_spec.rb +12 -4
- data/spec/graphql/schema/rescue_middleware_spec.rb +1 -1
- data/spec/graphql/schema/type_expression_spec.rb +2 -2
- data/spec/graphql/schema/type_reducer_spec.rb +21 -36
- data/spec/graphql/schema/type_validator_spec.rb +9 -9
- data/spec/graphql/schema_spec.rb +1 -1
- data/spec/graphql/static_validation/rules/argument_literals_are_compatible_spec.rb +4 -4
- data/spec/graphql/static_validation/rules/arguments_are_defined_spec.rb +4 -4
- data/spec/graphql/static_validation/rules/directives_are_defined_spec.rb +5 -5
- data/spec/graphql/static_validation/rules/directives_are_in_valid_locations_spec.rb +39 -0
- data/spec/graphql/static_validation/rules/fields_are_defined_on_type_spec.rb +5 -5
- data/spec/graphql/static_validation/rules/fields_have_appropriate_selections_spec.rb +4 -4
- data/spec/graphql/static_validation/rules/fields_will_merge_spec.rb +2 -2
- data/spec/graphql/static_validation/rules/fragment_spreads_are_possible_spec.rb +1 -1
- data/spec/graphql/static_validation/rules/fragment_types_exist_spec.rb +2 -2
- data/spec/graphql/static_validation/rules/fragments_are_finite_spec.rb +2 -2
- data/spec/graphql/static_validation/rules/fragments_are_on_composite_types_spec.rb +2 -2
- data/spec/graphql/static_validation/rules/fragments_are_used_spec.rb +3 -3
- data/spec/graphql/static_validation/rules/required_arguments_are_present_spec.rb +3 -3
- data/spec/graphql/static_validation/rules/variable_default_values_are_correctly_typed_spec.rb +5 -5
- data/spec/graphql/static_validation/rules/variable_usages_are_allowed_spec.rb +3 -1
- data/spec/graphql/static_validation/rules/variables_are_input_types_spec.rb +4 -4
- data/spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb +3 -3
- data/spec/graphql/static_validation/type_stack_spec.rb +3 -2
- data/spec/graphql/static_validation/validator_spec.rb +26 -6
- data/spec/graphql/union_type_spec.rb +5 -4
- data/spec/spec_helper.rb +2 -5
- data/spec/support/dairy_app.rb +30 -9
- data/spec/support/dairy_data.rb +1 -1
- data/spec/support/star_wars_data.rb +26 -26
- data/spec/support/star_wars_schema.rb +1 -1
- metadata +40 -21
- data/lib/graphql/language/transform.rb +0 -113
- data/lib/graphql/query/directive_chain.rb +0 -44
- data/lib/graphql/repl.rb +0 -27
- data/spec/graphql/language/transform_spec.rb +0 -156
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e39609ec3ac4ee1756f3dcbd618785468f05d895
|
4
|
+
data.tar.gz: 96aac564a69c22795a299ea4430b1bb95542a788
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ad5ecccde050131fe70cb6a33d6fdc1acb574a8e13e63af82da209313388f53fc042627f0ca32c00999e35fb227b0e1fd7068f69d7209ed5c2b20beb45a19cf
|
7
|
+
data.tar.gz: 8cecd7002d9067f04e5ac6a7381aab405d6e2acd9df5baf45976b994e49615e1fe9d94585df62e9728c7d8c8788ac7b408a40273308221f56e11af315816d74c
|
data/lib/graphql.rb
CHANGED
@@ -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
|
-
|
24
|
+
parse_with_racc(query_string)
|
26
25
|
end
|
27
26
|
|
28
|
-
def self.
|
29
|
-
|
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
|
44
|
-
require
|
45
|
-
require
|
34
|
+
require "graphql/define"
|
35
|
+
require "graphql/base_type"
|
36
|
+
require "graphql/object_type"
|
46
37
|
|
47
|
-
require
|
48
|
-
require
|
49
|
-
require
|
50
|
-
require
|
51
|
-
require
|
52
|
-
require
|
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
|
55
|
-
require
|
56
|
-
require
|
45
|
+
require "graphql/argument"
|
46
|
+
require "graphql/field"
|
47
|
+
require "graphql/type_kinds"
|
57
48
|
|
58
|
-
require
|
59
|
-
require
|
60
|
-
require
|
61
|
-
require
|
62
|
-
require
|
63
|
-
require
|
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
|
66
|
-
require
|
67
|
-
require
|
68
|
-
require
|
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
|
74
|
-
require
|
75
|
-
require
|
76
|
-
require
|
77
|
-
require
|
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"
|
data/lib/graphql/argument.rb
CHANGED
@@ -1,23 +1,25 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
22
|
-
|
22
|
+
# @return [String] The name of this argument on its {GraphQL::Field} or {GraphQL::InputObjectType}
|
23
|
+
attr_accessor :name
|
24
|
+
end
|
23
25
|
end
|
data/lib/graphql/base_type.rb
CHANGED
@@ -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
|
-
|
14
|
-
argument.
|
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
|
#
|
data/lib/graphql/directive.rb
CHANGED
@@ -1,39 +1,37 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
39
|
-
require
|
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
|
-
|
4
|
+
locations([GraphQL::Directive::FIELD, GraphQL::Directive::FRAGMENT_SPREAD, GraphQL::Directive::INLINE_FRAGMENT])
|
5
5
|
argument :if, !GraphQL::BOOLEAN_TYPE
|
6
6
|
|
7
|
-
|
8
|
-
|
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
|
-
|
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
|
-
|
9
|
-
|
10
|
-
proc.call
|
11
|
-
else
|
12
|
-
nil
|
13
|
-
end
|
8
|
+
include_proc -> (arguments) {
|
9
|
+
!arguments["if"]
|
14
10
|
}
|
15
11
|
end
|
data/lib/graphql/enum_type.rb
CHANGED
@@ -1,92 +1,94 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# value("
|
11
|
-
# value("
|
12
|
-
#
|
13
|
-
|
14
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
def initialize
|
18
|
+
@values_by_name = {}
|
19
|
+
@values_by_value = {}
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
33
|
+
def values
|
34
|
+
@values_by_name
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
47
|
-
|
48
|
-
|
47
|
+
def kind
|
48
|
+
GraphQL::TypeKinds::ENUM
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
51
|
+
def validate_non_null_input(value_name)
|
52
|
+
result = GraphQL::Query::InputValidationResult.new
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
58
|
-
|
58
|
+
result
|
59
|
+
end
|
59
60
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
73
|
+
def coerce_result(value)
|
74
|
+
@values_by_value.fetch(value).name
|
75
|
+
end
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
77
|
+
def to_s
|
78
|
+
name
|
79
|
+
end
|
79
80
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
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
|