graphql 1.11.5 → 1.12.2
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.
Potentially problematic release.
This version of graphql might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/lib/generators/graphql/install_generator.rb +5 -5
- data/lib/generators/graphql/object_generator.rb +2 -0
- data/lib/generators/graphql/relay_generator.rb +63 -0
- data/lib/generators/graphql/templates/base_connection.erb +8 -0
- data/lib/generators/graphql/templates/base_edge.erb +8 -0
- data/lib/generators/graphql/templates/node_type.erb +9 -0
- data/lib/generators/graphql/templates/object.erb +1 -1
- data/lib/generators/graphql/templates/query_type.erb +1 -3
- data/lib/generators/graphql/templates/schema.erb +8 -35
- data/lib/graphql.rb +39 -4
- data/lib/graphql/analysis/analyze_query.rb +7 -0
- data/lib/graphql/analysis/ast.rb +11 -2
- data/lib/graphql/analysis/ast/visitor.rb +9 -1
- data/lib/graphql/backtrace.rb +28 -19
- data/lib/graphql/backtrace/legacy_tracer.rb +56 -0
- data/lib/graphql/backtrace/table.rb +22 -2
- data/lib/graphql/backtrace/tracer.rb +40 -9
- data/lib/graphql/backwards_compatibility.rb +2 -1
- data/lib/graphql/base_type.rb +1 -1
- data/lib/graphql/compatibility/execution_specification.rb +1 -0
- data/lib/graphql/compatibility/lazy_execution_specification.rb +2 -0
- data/lib/graphql/compatibility/query_parser_specification.rb +2 -0
- data/lib/graphql/compatibility/schema_parser_specification.rb +2 -0
- data/lib/graphql/dataloader.rb +198 -0
- data/lib/graphql/dataloader/null_dataloader.rb +21 -0
- data/lib/graphql/dataloader/request.rb +24 -0
- data/lib/graphql/dataloader/request_all.rb +22 -0
- data/lib/graphql/dataloader/source.rb +93 -0
- data/lib/graphql/define/assign_global_id_field.rb +2 -2
- data/lib/graphql/define/instance_definable.rb +32 -2
- data/lib/graphql/define/type_definer.rb +5 -5
- data/lib/graphql/deprecated_dsl.rb +7 -2
- data/lib/graphql/deprecation.rb +13 -0
- data/lib/graphql/enum_type.rb +2 -0
- data/lib/graphql/execution/errors.rb +4 -0
- data/lib/graphql/execution/execute.rb +7 -0
- data/lib/graphql/execution/interpreter.rb +10 -6
- data/lib/graphql/execution/interpreter/arguments.rb +57 -5
- data/lib/graphql/execution/interpreter/arguments_cache.rb +8 -0
- data/lib/graphql/execution/interpreter/handles_raw_value.rb +0 -7
- data/lib/graphql/execution/interpreter/runtime.rb +219 -117
- data/lib/graphql/execution/multiplex.rb +20 -6
- data/lib/graphql/function.rb +4 -0
- data/lib/graphql/input_object_type.rb +2 -0
- data/lib/graphql/integer_decoding_error.rb +17 -0
- data/lib/graphql/interface_type.rb +3 -1
- data/lib/graphql/internal_representation/document.rb +2 -2
- data/lib/graphql/internal_representation/rewrite.rb +1 -1
- data/lib/graphql/invalid_null_error.rb +1 -1
- data/lib/graphql/language/document_from_schema_definition.rb +50 -23
- data/lib/graphql/object_type.rb +2 -0
- data/lib/graphql/pagination/connection.rb +5 -1
- data/lib/graphql/pagination/connections.rb +15 -19
- data/lib/graphql/query.rb +6 -1
- data/lib/graphql/query/arguments.rb +1 -1
- data/lib/graphql/query/context.rb +8 -1
- data/lib/graphql/query/serial_execution.rb +1 -0
- data/lib/graphql/query/validation_pipeline.rb +1 -1
- data/lib/graphql/relay/array_connection.rb +2 -2
- data/lib/graphql/relay/base_connection.rb +7 -0
- data/lib/graphql/relay/connection_instrumentation.rb +4 -4
- data/lib/graphql/relay/connection_type.rb +1 -1
- data/lib/graphql/relay/mutation.rb +1 -0
- data/lib/graphql/relay/node.rb +3 -0
- data/lib/graphql/relay/range_add.rb +14 -5
- data/lib/graphql/relay/type_extensions.rb +2 -0
- data/lib/graphql/scalar_type.rb +2 -0
- data/lib/graphql/schema.rb +80 -29
- data/lib/graphql/schema/argument.rb +25 -7
- data/lib/graphql/schema/build_from_definition.rb +150 -58
- data/lib/graphql/schema/default_type_error.rb +2 -0
- data/lib/graphql/schema/directive.rb +76 -0
- data/lib/graphql/schema/directive/flagged.rb +57 -0
- data/lib/graphql/schema/enum.rb +3 -0
- data/lib/graphql/schema/enum_value.rb +12 -6
- data/lib/graphql/schema/field.rb +52 -23
- data/lib/graphql/schema/field/connection_extension.rb +10 -8
- data/lib/graphql/schema/field/scope_extension.rb +1 -1
- data/lib/graphql/schema/input_object.rb +33 -22
- data/lib/graphql/schema/interface.rb +1 -0
- data/lib/graphql/schema/member.rb +4 -0
- data/lib/graphql/schema/member/base_dsl_methods.rb +1 -0
- data/lib/graphql/schema/member/build_type.rb +3 -3
- data/lib/graphql/schema/member/has_arguments.rb +67 -50
- data/lib/graphql/schema/member/has_deprecation_reason.rb +25 -0
- data/lib/graphql/schema/member/has_directives.rb +98 -0
- data/lib/graphql/schema/member/has_fields.rb +2 -2
- data/lib/graphql/schema/member/has_validators.rb +31 -0
- data/lib/graphql/schema/member/type_system_helpers.rb +1 -1
- data/lib/graphql/schema/middleware_chain.rb +1 -1
- data/lib/graphql/schema/object.rb +11 -0
- data/lib/graphql/schema/printer.rb +5 -4
- data/lib/graphql/schema/relay_classic_mutation.rb +1 -1
- data/lib/graphql/schema/resolver.rb +7 -0
- data/lib/graphql/schema/resolver/has_payload_type.rb +2 -0
- data/lib/graphql/schema/subscription.rb +19 -1
- data/lib/graphql/schema/timeout_middleware.rb +3 -1
- data/lib/graphql/schema/unique_within_type.rb +1 -2
- data/lib/graphql/schema/validation.rb +4 -2
- data/lib/graphql/schema/validator.rb +163 -0
- data/lib/graphql/schema/validator/exclusion_validator.rb +31 -0
- data/lib/graphql/schema/validator/format_validator.rb +49 -0
- data/lib/graphql/schema/validator/inclusion_validator.rb +33 -0
- data/lib/graphql/schema/validator/length_validator.rb +57 -0
- data/lib/graphql/schema/validator/numericality_validator.rb +71 -0
- data/lib/graphql/schema/validator/required_validator.rb +68 -0
- data/lib/graphql/static_validation.rb +1 -0
- data/lib/graphql/static_validation/all_rules.rb +1 -0
- data/lib/graphql/static_validation/rules/fields_will_merge.rb +25 -17
- data/lib/graphql/static_validation/rules/input_object_names_are_unique.rb +30 -0
- data/lib/graphql/static_validation/rules/input_object_names_are_unique_error.rb +30 -0
- data/lib/graphql/static_validation/validation_timeout_error.rb +25 -0
- data/lib/graphql/static_validation/validator.rb +32 -9
- data/lib/graphql/subscriptions.rb +17 -20
- data/lib/graphql/subscriptions/subscription_root.rb +1 -1
- data/lib/graphql/tracing.rb +2 -2
- data/lib/graphql/tracing/appoptics_tracing.rb +3 -1
- data/lib/graphql/tracing/platform_tracing.rb +4 -2
- data/lib/graphql/tracing/prometheus_tracing/graphql_collector.rb +4 -1
- data/lib/graphql/tracing/skylight_tracing.rb +1 -1
- data/lib/graphql/types/int.rb +9 -2
- data/lib/graphql/types/relay.rb +11 -3
- data/lib/graphql/types/relay/base_connection.rb +2 -91
- data/lib/graphql/types/relay/base_edge.rb +2 -34
- data/lib/graphql/types/relay/connection_behaviors.rb +123 -0
- data/lib/graphql/types/relay/default_relay.rb +27 -0
- data/lib/graphql/types/relay/edge_behaviors.rb +42 -0
- data/lib/graphql/types/relay/has_node_field.rb +41 -0
- data/lib/graphql/types/relay/has_nodes_field.rb +41 -0
- data/lib/graphql/types/relay/node.rb +2 -4
- data/lib/graphql/types/relay/node_behaviors.rb +15 -0
- data/lib/graphql/types/relay/node_field.rb +1 -19
- data/lib/graphql/types/relay/nodes_field.rb +1 -19
- data/lib/graphql/types/relay/page_info.rb +2 -14
- data/lib/graphql/types/relay/page_info_behaviors.rb +25 -0
- data/lib/graphql/types/string.rb +7 -1
- data/lib/graphql/unauthorized_error.rb +1 -1
- data/lib/graphql/union_type.rb +2 -0
- data/lib/graphql/upgrader/member.rb +1 -0
- data/lib/graphql/upgrader/schema.rb +1 -0
- data/lib/graphql/version.rb +1 -1
- data/readme.md +1 -1
- metadata +50 -6
- data/lib/graphql/types/relay/base_field.rb +0 -22
- data/lib/graphql/types/relay/base_interface.rb +0 -29
- data/lib/graphql/types/relay/base_object.rb +0 -26
@@ -10,6 +10,10 @@ module GraphQL
|
|
10
10
|
include GraphQL::Schema::Member::AcceptsDefinition
|
11
11
|
include GraphQL::Schema::Member::HasPath
|
12
12
|
include GraphQL::Schema::Member::HasAstNode
|
13
|
+
include GraphQL::Schema::Member::HasDirectives
|
14
|
+
include GraphQL::Schema::Member::HasDeprecationReason
|
15
|
+
include GraphQL::Schema::Member::HasValidators
|
16
|
+
include GraphQL::Schema::FindInheritedValue::EmptyObjects
|
13
17
|
|
14
18
|
NO_DEFAULT = :__no_default__
|
15
19
|
|
@@ -45,8 +49,10 @@ module GraphQL
|
|
45
49
|
# @param camelize [Boolean] if true, the name will be camelized when building the schema
|
46
50
|
# @param from_resolver [Boolean] if true, a Resolver class defined this argument
|
47
51
|
# @param method_access [Boolean] If false, don't build method access on legacy {Query::Arguments} instances.
|
52
|
+
# @param directives [Hash{Class => Hash}]
|
48
53
|
# @param deprecation_reason [String]
|
49
|
-
|
54
|
+
# @param validates [Hash, nil] Options for building validators, if any should be applied
|
55
|
+
def initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, method_access: true, owner:, validates: nil, directives: nil, deprecation_reason: nil, &definition_block)
|
50
56
|
arg_name ||= name
|
51
57
|
@name = -(camelize ? Member::BuildType.camelize(arg_name.to_s) : arg_name.to_s)
|
52
58
|
@type_expr = type_expr || type
|
@@ -63,6 +69,14 @@ module GraphQL
|
|
63
69
|
@method_access = method_access
|
64
70
|
self.deprecation_reason = deprecation_reason
|
65
71
|
|
72
|
+
if directives
|
73
|
+
directives.each do |dir_class, dir_options|
|
74
|
+
directive(dir_class, **dir_options)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
self.validates(validates)
|
79
|
+
|
66
80
|
if definition_block
|
67
81
|
if definition_block.arity == 1
|
68
82
|
instance_exec(self, &definition_block)
|
@@ -94,14 +108,16 @@ module GraphQL
|
|
94
108
|
# @return [String] Deprecation reason for this argument
|
95
109
|
def deprecation_reason(text = nil)
|
96
110
|
if text
|
97
|
-
|
98
|
-
@deprecation_reason = text
|
111
|
+
self.deprecation_reason = text
|
99
112
|
else
|
100
|
-
|
113
|
+
super()
|
101
114
|
end
|
102
115
|
end
|
103
116
|
|
104
|
-
|
117
|
+
def deprecation_reason=(new_reason)
|
118
|
+
validate_deprecated_or_optional(null: @null, deprecation_reason: new_reason)
|
119
|
+
super
|
120
|
+
end
|
105
121
|
|
106
122
|
def visible?(context)
|
107
123
|
true
|
@@ -157,8 +173,8 @@ module GraphQL
|
|
157
173
|
if NO_DEFAULT != @default_value
|
158
174
|
argument.default_value = @default_value
|
159
175
|
end
|
160
|
-
if
|
161
|
-
argument.deprecation_reason =
|
176
|
+
if self.deprecation_reason
|
177
|
+
argument.deprecation_reason = self.deprecation_reason
|
162
178
|
end
|
163
179
|
argument
|
164
180
|
end
|
@@ -199,6 +215,8 @@ module GraphQL
|
|
199
215
|
value = value.prepare
|
200
216
|
end
|
201
217
|
|
218
|
+
Schema::Validator.validate!(validators, obj, context, value)
|
219
|
+
|
202
220
|
if @prepare.nil?
|
203
221
|
value
|
204
222
|
elsif @prepare.is_a?(String) || @prepare.is_a?(Symbol)
|
@@ -3,6 +3,7 @@ require "graphql/schema/build_from_definition/resolve_map"
|
|
3
3
|
|
4
4
|
module GraphQL
|
5
5
|
class Schema
|
6
|
+
# TODO Populate `.directive(...)` from here
|
6
7
|
module BuildFromDefinition
|
7
8
|
if !String.method_defined?(:-@)
|
8
9
|
using GraphQL::StringDedupBackport
|
@@ -18,8 +19,8 @@ module GraphQL
|
|
18
19
|
from_document(parser.parse_file(definition_path), **kwargs)
|
19
20
|
end
|
20
21
|
|
21
|
-
def from_document(document, default_resolve:, using: {}, relay: false
|
22
|
-
Builder.build(document, default_resolve: default_resolve || {}, relay: relay, using: using
|
22
|
+
def from_document(document, default_resolve:, using: {}, relay: false)
|
23
|
+
Builder.build(document, default_resolve: default_resolve || {}, relay: relay, using: using)
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
@@ -27,7 +28,7 @@ module GraphQL
|
|
27
28
|
module Builder
|
28
29
|
extend self
|
29
30
|
|
30
|
-
def build(document, default_resolve:, using: {},
|
31
|
+
def build(document, default_resolve:, using: {}, relay:)
|
31
32
|
raise InvalidDocumentError.new('Must provide a document ast.') if !document || !document.is_a?(GraphQL::Language::Nodes::Document)
|
32
33
|
|
33
34
|
if default_resolve.is_a?(Hash)
|
@@ -41,45 +42,43 @@ module GraphQL
|
|
41
42
|
schema_definition = schema_defns.first
|
42
43
|
types = {}
|
43
44
|
directives = {}
|
44
|
-
type_resolver = ->(
|
45
|
+
type_resolver = build_resolve_type(types, directives, ->(type_name) { types[type_name] ||= Schema::LateBoundType.new(type_name)})
|
46
|
+
# Make a different type resolver because we need to coerce directive arguments
|
47
|
+
# _while_ building the schema.
|
48
|
+
# It will dig for a type if it encounters a custom type. This could be a problem if there are cycles.
|
49
|
+
directive_type_resolver = nil
|
50
|
+
directive_type_resolver = build_resolve_type(GraphQL::Schema::BUILT_IN_TYPES, directives, ->(type_name) {
|
51
|
+
types[type_name] ||= begin
|
52
|
+
defn = document.definitions.find { |d| d.respond_to?(:name) && d.name == type_name }
|
53
|
+
build_definition_from_node(defn, directive_type_resolver, default_resolve)
|
54
|
+
end
|
55
|
+
})
|
45
56
|
|
46
57
|
document.definitions.each do |definition|
|
47
|
-
|
48
|
-
|
49
|
-
nil # already handled
|
50
|
-
when GraphQL::Language::Nodes::EnumTypeDefinition
|
51
|
-
types[definition.name] = build_enum_type(definition, type_resolver)
|
52
|
-
when GraphQL::Language::Nodes::ObjectTypeDefinition
|
53
|
-
types[definition.name] = build_object_type(definition, type_resolver)
|
54
|
-
when GraphQL::Language::Nodes::InterfaceTypeDefinition
|
55
|
-
types[definition.name] = build_interface_type(definition, type_resolver)
|
56
|
-
when GraphQL::Language::Nodes::UnionTypeDefinition
|
57
|
-
types[definition.name] = build_union_type(definition, type_resolver)
|
58
|
-
when GraphQL::Language::Nodes::ScalarTypeDefinition
|
59
|
-
types[definition.name] = build_scalar_type(definition, type_resolver, default_resolve: default_resolve)
|
60
|
-
when GraphQL::Language::Nodes::InputObjectTypeDefinition
|
61
|
-
types[definition.name] = build_input_object_type(definition, type_resolver)
|
62
|
-
when GraphQL::Language::Nodes::DirectiveDefinition
|
63
|
-
directives[definition.name] = build_directive(definition, type_resolver)
|
58
|
+
if definition.is_a?(GraphQL::Language::Nodes::DirectiveDefinition)
|
59
|
+
directives[definition.name] = build_directive(definition, directive_type_resolver)
|
64
60
|
end
|
65
61
|
end
|
66
62
|
|
67
|
-
|
68
|
-
|
69
|
-
#
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
types[
|
63
|
+
directives = GraphQL::Schema.default_directives.merge(directives)
|
64
|
+
|
65
|
+
# In case any directives referenced built-in types for their arguments:
|
66
|
+
replace_late_bound_types_with_built_in(types)
|
67
|
+
|
68
|
+
document.definitions.each do |definition|
|
69
|
+
case definition
|
70
|
+
when GraphQL::Language::Nodes::SchemaDefinition, GraphQL::Language::Nodes::DirectiveDefinition
|
71
|
+
nil # already handled
|
72
|
+
else
|
73
|
+
# It's possible that this was already loaded by the directives
|
74
|
+
prev_type = types[definition.name]
|
75
|
+
if prev_type.nil? || prev_type.is_a?(Schema::LateBoundType)
|
76
|
+
types[definition.name] = build_definition_from_node(definition, type_resolver, default_resolve)
|
77
|
+
end
|
79
78
|
end
|
80
79
|
end
|
81
80
|
|
82
|
-
|
81
|
+
replace_late_bound_types_with_built_in(types)
|
83
82
|
|
84
83
|
if schema_definition
|
85
84
|
if schema_definition.query
|
@@ -133,11 +132,6 @@ module GraphQL
|
|
133
132
|
ast_node(schema_definition)
|
134
133
|
end
|
135
134
|
|
136
|
-
if interpreter
|
137
|
-
use GraphQL::Execution::Interpreter
|
138
|
-
use GraphQL::Analysis::AST
|
139
|
-
end
|
140
|
-
|
141
135
|
using.each do |plugin, options|
|
142
136
|
if options
|
143
137
|
use(plugin, **options)
|
@@ -169,10 +163,85 @@ module GraphQL
|
|
169
163
|
raise(GraphQL::RequiredImplementationMissingError, "Generated Schema cannot use Interface or Union types for execution. Implement resolve_type on your resolver.")
|
170
164
|
}
|
171
165
|
|
166
|
+
def build_definition_from_node(definition, type_resolver, default_resolve)
|
167
|
+
case definition
|
168
|
+
when GraphQL::Language::Nodes::EnumTypeDefinition
|
169
|
+
build_enum_type(definition, type_resolver)
|
170
|
+
when GraphQL::Language::Nodes::ObjectTypeDefinition
|
171
|
+
build_object_type(definition, type_resolver)
|
172
|
+
when GraphQL::Language::Nodes::InterfaceTypeDefinition
|
173
|
+
build_interface_type(definition, type_resolver)
|
174
|
+
when GraphQL::Language::Nodes::UnionTypeDefinition
|
175
|
+
build_union_type(definition, type_resolver)
|
176
|
+
when GraphQL::Language::Nodes::ScalarTypeDefinition
|
177
|
+
build_scalar_type(definition, type_resolver, default_resolve: default_resolve)
|
178
|
+
when GraphQL::Language::Nodes::InputObjectTypeDefinition
|
179
|
+
build_input_object_type(definition, type_resolver)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
# Modify `types`, replacing any late-bound references to built-in types
|
184
|
+
# with their actual definitions.
|
185
|
+
#
|
186
|
+
# (Schema definitions are allowed to reference those built-ins without redefining them.)
|
187
|
+
# @return void
|
188
|
+
def replace_late_bound_types_with_built_in(types)
|
189
|
+
GraphQL::Schema::BUILT_IN_TYPES.each do |scalar_name, built_in_scalar|
|
190
|
+
existing_type = types[scalar_name]
|
191
|
+
if existing_type.is_a?(GraphQL::Schema::LateBoundType)
|
192
|
+
types[scalar_name] = built_in_scalar
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def build_directives(definition, ast_node, type_resolver)
|
198
|
+
dirs = prepare_directives(ast_node, type_resolver)
|
199
|
+
dirs.each do |dir_class, options|
|
200
|
+
definition.directive(dir_class, **options)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def prepare_directives(ast_node, type_resolver)
|
205
|
+
dirs = {}
|
206
|
+
ast_node.directives.each do |dir_node|
|
207
|
+
if dir_node.name == "deprecated"
|
208
|
+
# This is handled using `deprecation_reason`
|
209
|
+
next
|
210
|
+
else
|
211
|
+
dir_class = type_resolver.call(dir_node.name)
|
212
|
+
if dir_class.nil?
|
213
|
+
raise ArgumentError, "No definition for @#{dir_node.name} on #{ast_node.name} at #{ast_node.line}:#{ast_node.col}"
|
214
|
+
end
|
215
|
+
options = args_to_kwargs(dir_class, dir_node)
|
216
|
+
dirs[dir_class] = options
|
217
|
+
end
|
218
|
+
end
|
219
|
+
dirs
|
220
|
+
end
|
221
|
+
|
222
|
+
def args_to_kwargs(arg_owner, node)
|
223
|
+
if node.respond_to?(:arguments)
|
224
|
+
kwargs = {}
|
225
|
+
node.arguments.each do |arg_node|
|
226
|
+
arg_defn = arg_owner.get_argument(arg_node.name)
|
227
|
+
kwargs[arg_defn.keyword] = args_to_kwargs(arg_defn.type.unwrap, arg_node.value)
|
228
|
+
end
|
229
|
+
kwargs
|
230
|
+
elsif node.is_a?(Array)
|
231
|
+
node.map { |n| args_to_kwargs(arg_owner, n) }
|
232
|
+
elsif node.is_a?(Language::Nodes::Enum)
|
233
|
+
node.name
|
234
|
+
else
|
235
|
+
# scalar
|
236
|
+
node
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
172
240
|
def build_enum_type(enum_type_definition, type_resolver)
|
173
241
|
builder = self
|
174
242
|
Class.new(GraphQL::Schema::Enum) do
|
175
243
|
graphql_name(enum_type_definition.name)
|
244
|
+
builder.build_directives(self, enum_type_definition, type_resolver)
|
176
245
|
description(enum_type_definition.description)
|
177
246
|
ast_node(enum_type_definition)
|
178
247
|
enum_type_definition.values.each do |enum_value_definition|
|
@@ -180,6 +249,7 @@ module GraphQL
|
|
180
249
|
value: enum_value_definition.name,
|
181
250
|
deprecation_reason: builder.build_deprecation_reason(enum_value_definition.directives),
|
182
251
|
description: enum_value_definition.description,
|
252
|
+
directives: builder.prepare_directives(enum_value_definition, type_resolver),
|
183
253
|
ast_node: enum_value_definition,
|
184
254
|
)
|
185
255
|
end
|
@@ -197,29 +267,36 @@ module GraphQL
|
|
197
267
|
end
|
198
268
|
|
199
269
|
def build_scalar_type(scalar_type_definition, type_resolver, default_resolve:)
|
270
|
+
builder = self
|
200
271
|
Class.new(GraphQL::Schema::Scalar) do
|
201
272
|
graphql_name(scalar_type_definition.name)
|
202
273
|
description(scalar_type_definition.description)
|
203
274
|
ast_node(scalar_type_definition)
|
275
|
+
builder.build_directives(self, scalar_type_definition, type_resolver)
|
204
276
|
|
205
277
|
if default_resolve.respond_to?(:coerce_input)
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
def self.coerce_result(val, ctx)
|
211
|
-
ctx.schema.definition_default_resolve.coerce_result(self, val, ctx)
|
212
|
-
end
|
278
|
+
# Put these method definitions in another method to avoid retaining `type_resolve`
|
279
|
+
# from this method's bindiing
|
280
|
+
builder.build_scalar_type_coerce_method(self, :coerce_input, default_resolve)
|
281
|
+
builder.build_scalar_type_coerce_method(self, :coerce_result, default_resolve)
|
213
282
|
end
|
214
283
|
end
|
215
284
|
end
|
216
285
|
|
286
|
+
def build_scalar_type_coerce_method(scalar_class, method_name, default_definition_resolve)
|
287
|
+
scalar_class.define_singleton_method(method_name) do |val, ctx|
|
288
|
+
default_definition_resolve.public_send(method_name, self, val, ctx)
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
217
292
|
def build_union_type(union_type_definition, type_resolver)
|
293
|
+
builder = self
|
218
294
|
Class.new(GraphQL::Schema::Union) do
|
219
295
|
graphql_name(union_type_definition.name)
|
220
296
|
description(union_type_definition.description)
|
221
297
|
possible_types(*union_type_definition.types.map { |type_name| type_resolver.call(type_name) })
|
222
298
|
ast_node(union_type_definition)
|
299
|
+
builder.build_directives(self, union_type_definition, type_resolver)
|
223
300
|
end
|
224
301
|
end
|
225
302
|
|
@@ -230,6 +307,7 @@ module GraphQL
|
|
230
307
|
graphql_name(object_type_definition.name)
|
231
308
|
description(object_type_definition.description)
|
232
309
|
ast_node(object_type_definition)
|
310
|
+
builder.build_directives(self, object_type_definition, type_resolver)
|
233
311
|
|
234
312
|
object_type_definition.interfaces.each do |interface_name|
|
235
313
|
interface_defn = type_resolver.call(interface_name)
|
@@ -246,6 +324,7 @@ module GraphQL
|
|
246
324
|
graphql_name(input_object_type_definition.name)
|
247
325
|
description(input_object_type_definition.description)
|
248
326
|
ast_node(input_object_type_definition)
|
327
|
+
builder.build_directives(self, input_object_type_definition, type_resolver)
|
249
328
|
builder.build_arguments(self, input_object_type_definition.fields, type_resolver)
|
250
329
|
end
|
251
330
|
end
|
@@ -286,6 +365,7 @@ module GraphQL
|
|
286
365
|
ast_node: argument_defn,
|
287
366
|
camelize: false,
|
288
367
|
method_access: false,
|
368
|
+
directives: prepare_directives(argument_defn, type_resolver),
|
289
369
|
**default_value_kwargs
|
290
370
|
)
|
291
371
|
end
|
@@ -309,6 +389,7 @@ module GraphQL
|
|
309
389
|
graphql_name(interface_type_definition.name)
|
310
390
|
description(interface_type_definition.description)
|
311
391
|
ast_node(interface_type_definition)
|
392
|
+
builder.build_directives(self, interface_type_definition, type_resolver)
|
312
393
|
|
313
394
|
builder.build_fields(self, interface_type_definition.fields, type_resolver, default_resolve: nil)
|
314
395
|
end
|
@@ -331,6 +412,7 @@ module GraphQL
|
|
331
412
|
ast_node: field_definition,
|
332
413
|
method_conflict_warning: false,
|
333
414
|
camelize: false,
|
415
|
+
directives: prepare_directives(field_definition, type_resolver),
|
334
416
|
resolver_method: resolve_method_name,
|
335
417
|
)
|
336
418
|
|
@@ -349,18 +431,28 @@ module GraphQL
|
|
349
431
|
end
|
350
432
|
end
|
351
433
|
|
352
|
-
def
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
434
|
+
def build_resolve_type(lookup_hash, directives, missing_type_handler)
|
435
|
+
resolve_type_proc = nil
|
436
|
+
resolve_type_proc = ->(ast_node) {
|
437
|
+
case ast_node
|
438
|
+
when GraphQL::Language::Nodes::TypeName
|
439
|
+
type_name = ast_node.name
|
440
|
+
if lookup_hash.key?(type_name)
|
441
|
+
lookup_hash[type_name]
|
442
|
+
else
|
443
|
+
missing_type_handler.call(type_name)
|
444
|
+
end
|
445
|
+
when GraphQL::Language::Nodes::NonNullType
|
446
|
+
resolve_type_proc.call(ast_node.of_type).to_non_null_type
|
447
|
+
when GraphQL::Language::Nodes::ListType
|
448
|
+
resolve_type_proc.call(ast_node.of_type).to_list_type
|
449
|
+
when String
|
450
|
+
directives[ast_node]
|
451
|
+
else
|
452
|
+
raise "Unexpected ast_node: #{ast_node.inspect}"
|
453
|
+
end
|
454
|
+
}
|
455
|
+
resolve_type_proc
|
364
456
|
end
|
365
457
|
|
366
458
|
def resolve_type_name(type)
|
@@ -9,6 +9,13 @@ module GraphQL
|
|
9
9
|
class Directive < GraphQL::Schema::Member
|
10
10
|
extend GraphQL::Schema::Member::HasArguments
|
11
11
|
class << self
|
12
|
+
# Directives aren't types, they don't have kinds.
|
13
|
+
undef_method :kind
|
14
|
+
|
15
|
+
def path
|
16
|
+
"@#{super}"
|
17
|
+
end
|
18
|
+
|
12
19
|
# Return a name based on the class name,
|
13
20
|
# but downcase the first letter.
|
14
21
|
def default_graphql_name
|
@@ -21,6 +28,11 @@ module GraphQL
|
|
21
28
|
|
22
29
|
def locations(*new_locations)
|
23
30
|
if new_locations.any?
|
31
|
+
new_locations.each do |new_loc|
|
32
|
+
if !LOCATIONS.include?(new_loc.to_sym)
|
33
|
+
raise ArgumentError, "#{self} (#{self.graphql_name}) has an invalid directive location: `locations #{new_loc}` "
|
34
|
+
end
|
35
|
+
end
|
24
36
|
@locations = new_locations
|
25
37
|
else
|
26
38
|
@locations ||= (superclass.respond_to?(:locations) ? superclass.locations : [])
|
@@ -87,6 +99,23 @@ module GraphQL
|
|
87
99
|
end
|
88
100
|
end
|
89
101
|
|
102
|
+
# @return [GraphQL::Schema::Field, GraphQL::Schema::Argument, Class, Module]
|
103
|
+
attr_reader :owner
|
104
|
+
|
105
|
+
# @return [GraphQL::Interpreter::Arguments]
|
106
|
+
attr_reader :arguments
|
107
|
+
|
108
|
+
def initialize(owner, **arguments)
|
109
|
+
@owner = owner
|
110
|
+
assert_valid_owner
|
111
|
+
# It's be nice if we had the real context here, but we don't. What we _would_ get is:
|
112
|
+
# - error handling
|
113
|
+
# - lazy resolution
|
114
|
+
# Probably, those won't be needed here, since these are configuration arguments,
|
115
|
+
# not runtime arguments.
|
116
|
+
@arguments = self.class.coerce_arguments(nil, arguments, Query::NullContext)
|
117
|
+
end
|
118
|
+
|
90
119
|
LOCATIONS = [
|
91
120
|
QUERY = :QUERY,
|
92
121
|
MUTATION = :MUTATION,
|
@@ -129,6 +158,53 @@ module GraphQL
|
|
129
158
|
INPUT_OBJECT: 'Location adjacent to an input object type definition.',
|
130
159
|
INPUT_FIELD_DEFINITION: 'Location adjacent to an input object field definition.',
|
131
160
|
}
|
161
|
+
|
162
|
+
private
|
163
|
+
|
164
|
+
def assert_valid_owner
|
165
|
+
case @owner
|
166
|
+
when Class
|
167
|
+
if @owner < GraphQL::Schema::Object
|
168
|
+
assert_has_location(OBJECT)
|
169
|
+
elsif @owner < GraphQL::Schema::Union
|
170
|
+
assert_has_location(UNION)
|
171
|
+
elsif @owner < GraphQL::Schema::Enum
|
172
|
+
assert_has_location(ENUM)
|
173
|
+
elsif @owner < GraphQL::Schema::InputObject
|
174
|
+
assert_has_location(INPUT_OBJECT)
|
175
|
+
elsif @owner < GraphQL::Schema::Scalar
|
176
|
+
assert_has_location(SCALAR)
|
177
|
+
elsif @owner < GraphQL::Schema
|
178
|
+
assert_has_location(SCHEMA)
|
179
|
+
else
|
180
|
+
raise "Unexpected directive owner class: #{@owner}"
|
181
|
+
end
|
182
|
+
when Module
|
183
|
+
assert_has_location(INTERFACE)
|
184
|
+
when GraphQL::Schema::Argument
|
185
|
+
if @owner.owner.is_a?(GraphQL::Schema::Field)
|
186
|
+
assert_has_location(ARGUMENT_DEFINITION)
|
187
|
+
else
|
188
|
+
assert_has_location(INPUT_FIELD_DEFINITION)
|
189
|
+
end
|
190
|
+
when GraphQL::Schema::Field
|
191
|
+
assert_has_location(FIELD_DEFINITION)
|
192
|
+
when GraphQL::Schema::EnumValue
|
193
|
+
assert_has_location(ENUM_VALUE)
|
194
|
+
else
|
195
|
+
raise "Unexpected directive owner: #{@owner.inspect}"
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
def assert_has_location(location)
|
200
|
+
if !self.class.locations.include?(location)
|
201
|
+
raise ArgumentError, <<-MD
|
202
|
+
Directive `@#{self.class.graphql_name}` can't be attached to #{@owner.graphql_name} because #{location} isn't included in its locations (#{self.class.locations.join(", ")}).
|
203
|
+
|
204
|
+
Use `locations(#{location})` to update this directive's definition, or remove it from #{@owner.graphql_name}.
|
205
|
+
MD
|
206
|
+
end
|
207
|
+
end
|
132
208
|
end
|
133
209
|
end
|
134
210
|
end
|