-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathschema.rb
More file actions
1797 lines (1622 loc) · 70.4 KB
/
schema.rb
File metadata and controls
1797 lines (1622 loc) · 70.4 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require "logger"
require "graphql/schema/addition"
require "graphql/schema/always_visible"
require "graphql/schema/base_64_encoder"
require "graphql/schema/find_inherited_value"
require "graphql/schema/finder"
require "graphql/schema/introspection_system"
require "graphql/schema/late_bound_type"
require "graphql/schema/timeout"
require "graphql/schema/type_expression"
require "graphql/schema/unique_within_type"
require "graphql/schema/warden"
require "graphql/schema/build_from_definition"
require "graphql/schema/validator"
require "graphql/schema/member"
require "graphql/schema/wrapper"
require "graphql/schema/list"
require "graphql/schema/non_null"
require "graphql/schema/argument"
require "graphql/schema/enum_value"
require "graphql/schema/enum"
require "graphql/schema/field_extension"
require "graphql/schema/field"
require "graphql/schema/input_object"
require "graphql/schema/interface"
require "graphql/schema/scalar"
require "graphql/schema/object"
require "graphql/schema/union"
require "graphql/schema/directive"
require "graphql/schema/directive/deprecated"
require "graphql/schema/directive/include"
require "graphql/schema/directive/one_of"
require "graphql/schema/directive/skip"
require "graphql/schema/directive/feature"
require "graphql/schema/directive/flagged"
require "graphql/schema/directive/transform"
require "graphql/schema/directive/specified_by"
require "graphql/schema/type_membership"
require "graphql/schema/resolver"
require "graphql/schema/mutation"
require "graphql/schema/has_single_input_argument"
require "graphql/schema/relay_classic_mutation"
require "graphql/schema/subscription"
require "graphql/schema/visibility"
module GraphQL
# A GraphQL schema which may be queried with {GraphQL::Query}.
#
# The {Schema} contains:
#
# - types for exposing your application
# - query analyzers for assessing incoming queries (including max depth & max complexity restrictions)
# - execution strategies for running incoming queries
#
# Schemas start with root types, {Schema#query}, {Schema#mutation} and {Schema#subscription}.
# The schema will traverse the tree of fields & types, using those as starting points.
# Any undiscoverable types may be provided with the `types` configuration.
#
# Schemas can restrict large incoming queries with `max_depth` and `max_complexity` configurations.
# (These configurations can be overridden by specific calls to {Schema#execute})
#
# @example defining a schema
# class MySchema < GraphQL::Schema
# query QueryType
# # If types are only connected by way of interfaces, they must be added here
# orphan_types ImageType, AudioType
# end
#
class Schema
extend GraphQL::Schema::Member::HasAstNode
extend GraphQL::Schema::FindInheritedValue
extend Autoload
autoload :BUILT_IN_TYPES, "graphql/schema/built_in_types"
class DuplicateNamesError < GraphQL::Error
attr_reader :duplicated_name
def initialize(duplicated_name:, duplicated_definition_1:, duplicated_definition_2:)
@duplicated_name = duplicated_name
super(
"Found two visible definitions for `#{duplicated_name}`: #{duplicated_definition_1}, #{duplicated_definition_2}"
)
end
end
class UnresolvedLateBoundTypeError < GraphQL::Error
attr_reader :type
def initialize(type:)
@type = type
super("Late bound type was never found: #{type.inspect}")
end
end
# Error that is raised when [#Schema#from_definition] is passed an invalid schema definition string.
class InvalidDocumentError < Error; end;
class << self
# Create schema with the result of an introspection query.
# @param introspection_result [Hash] A response from {GraphQL::Introspection::INTROSPECTION_QUERY}
# @return [Class<GraphQL::Schema>] the schema described by `input`
def from_introspection(introspection_result)
GraphQL::Schema::Loader.load(introspection_result)
end
# Create schema from an IDL schema or file containing an IDL definition.
# @param definition_or_path [String] A schema definition string, or a path to a file containing the definition
# @param default_resolve [<#call(type, field, obj, args, ctx)>] A callable for handling field resolution
# @param parser [Object] An object for handling definition string parsing (must respond to `parse`)
# @param using [Hash] Plugins to attach to the created schema with `use(key, value)`
# @return [Class] the schema described by `document`
def from_definition(definition_or_path, default_resolve: nil, parser: GraphQL.default_parser, using: {})
# If the file ends in `.graphql` or `.graphqls`, treat it like a filepath
if definition_or_path.end_with?(".graphql") || definition_or_path.end_with?(".graphqls")
GraphQL::Schema::BuildFromDefinition.from_definition_path(
self,
definition_or_path,
default_resolve: default_resolve,
parser: parser,
using: using,
)
else
GraphQL::Schema::BuildFromDefinition.from_definition(
self,
definition_or_path,
default_resolve: default_resolve,
parser: parser,
using: using,
)
end
end
def deprecated_graphql_definition
graphql_definition(silence_deprecation_warning: true)
end
# @return [GraphQL::Subscriptions]
def subscriptions(inherited: true)
defined?(@subscriptions) ? @subscriptions : (inherited ? find_inherited_value(:subscriptions, nil) : nil)
end
def subscriptions=(new_implementation)
@subscriptions = new_implementation
end
# @param new_mode [Symbol] If configured, this will be used when `context: { trace_mode: ... }` isn't set.
def default_trace_mode(new_mode = nil)
if new_mode
@default_trace_mode = new_mode
elsif defined?(@default_trace_mode)
@default_trace_mode
elsif superclass.respond_to?(:default_trace_mode)
superclass.default_trace_mode
else
:default
end
end
def trace_class(new_class = nil)
if new_class
# If any modules were already added for `:default`,
# re-apply them here
mods = trace_modules_for(:default)
mods.each { |mod| new_class.include(mod) }
new_class.include(DefaultTraceClass)
trace_mode(:default, new_class)
end
trace_class_for(:default, build: true)
end
# @return [Class] Return the trace class to use for this mode, looking one up on the superclass if this Schema doesn't have one defined.
def trace_class_for(mode, build: false)
if (trace_class = own_trace_modes[mode])
trace_class
elsif superclass.respond_to?(:trace_class_for) && (trace_class = superclass.trace_class_for(mode, build: false))
trace_class
elsif build
own_trace_modes[mode] = build_trace_mode(mode)
else
nil
end
end
# Configure `trace_class` to be used whenever `context: { trace_mode: mode_name }` is requested.
# {default_trace_mode} is used when no `trace_mode: ...` is requested.
#
# When a `trace_class` is added this way, it will _not_ receive other modules added with `trace_with(...)`
# unless `trace_mode` is explicitly given. (This class will not receive any default trace modules.)
#
# Subclasses of the schema will use `trace_class` as a base class for this mode and those
# subclass also will _not_ receive default tracing modules.
#
# @param mode_name [Symbol]
# @param trace_class [Class] subclass of GraphQL::Tracing::Trace
# @return void
def trace_mode(mode_name, trace_class)
own_trace_modes[mode_name] = trace_class
nil
end
def own_trace_modes
@own_trace_modes ||= {}
end
def build_trace_mode(mode)
case mode
when :default
# Use the superclass's default mode if it has one, or else start an inheritance chain at the built-in base class.
base_class = (superclass.respond_to?(:trace_class_for) && superclass.trace_class_for(mode, build: true)) || GraphQL::Tracing::Trace
const_set(:DefaultTrace, Class.new(base_class) do
include DefaultTraceClass
end)
else
# First, see if the superclass has a custom-defined class for this.
# Then, if it doesn't, use this class's default trace
base_class = (superclass.respond_to?(:trace_class_for) && superclass.trace_class_for(mode)) || trace_class_for(:default, build: true)
# Prepare the default trace class if it hasn't been initialized yet
base_class ||= (own_trace_modes[:default] = build_trace_mode(:default))
mods = trace_modules_for(mode)
if base_class < DefaultTraceClass
mods = trace_modules_for(:default) + mods
end
# Copy the existing default options into this mode's options
default_options = trace_options_for(:default)
add_trace_options_for(mode, default_options)
Class.new(base_class) do
!mods.empty? && include(*mods)
end
end
end
def own_trace_modules
@own_trace_modules ||= Hash.new { |h, k| h[k] = [] }
end
# @return [Array<Module>] Modules added for tracing in `trace_mode`, including inherited ones
def trace_modules_for(trace_mode)
modules = own_trace_modules[trace_mode]
if superclass.respond_to?(:trace_modules_for)
modules += superclass.trace_modules_for(trace_mode)
end
modules
end
# Returns the JSON response of {Introspection::INTROSPECTION_QUERY}.
# @see {#as_json}
# @return [String]
def to_json(**args)
JSON.pretty_generate(as_json(**args))
end
# Return the Hash response of {Introspection::INTROSPECTION_QUERY}.
# @param context [Hash]
# @param only [<#call(member, ctx)>]
# @param except [<#call(member, ctx)>]
# @param include_deprecated_args [Boolean] If true, deprecated arguments will be included in the JSON response
# @param include_schema_description [Boolean] If true, the schema's description will be queried and included in the response
# @param include_is_repeatable [Boolean] If true, `isRepeatable: true|false` will be included with the schema's directives
# @param include_specified_by_url [Boolean] If true, scalar types' `specifiedByUrl:` will be included in the response
# @param include_is_one_of [Boolean] If true, `isOneOf: true|false` will be included with input objects
# @return [Hash] GraphQL result
def as_json(context: {}, include_deprecated_args: true, include_schema_description: false, include_is_repeatable: false, include_specified_by_url: false, include_is_one_of: false)
introspection_query = Introspection.query(
include_deprecated_args: include_deprecated_args,
include_schema_description: include_schema_description,
include_is_repeatable: include_is_repeatable,
include_is_one_of: include_is_one_of,
include_specified_by_url: include_specified_by_url,
)
execute(introspection_query, context: context).to_h
end
# Return the GraphQL IDL for the schema
# @param context [Hash]
# @return [String]
def to_definition(context: {})
GraphQL::Schema::Printer.print_schema(self, context: context)
end
# Return the GraphQL::Language::Document IDL AST for the schema
# @return [GraphQL::Language::Document]
def to_document
GraphQL::Language::DocumentFromSchemaDefinition.new(self).document
end
# @return [String, nil]
def description(new_description = nil)
if new_description
@description = new_description
elsif defined?(@description)
@description
else
find_inherited_value(:description, nil)
end
end
def find(path)
if !@finder
@find_cache = {}
@finder ||= GraphQL::Schema::Finder.new(self)
end
@find_cache[path] ||= @finder.find(path)
end
def static_validator
GraphQL::StaticValidation::Validator.new(schema: self)
end
# Add `plugin` to this schema
# @param plugin [#use] A Schema plugin
# @return void
def use(plugin, **kwargs)
if !kwargs.empty?
plugin.use(self, **kwargs)
else
plugin.use(self)
end
own_plugins << [plugin, kwargs]
end
def plugins
find_inherited_value(:plugins, EMPTY_ARRAY) + own_plugins
end
# Build a map of `{ name => type }` and return it
# @return [Hash<String => Class>] A dictionary of type classes by their GraphQL name
# @see get_type Which is more efficient for finding _one type_ by name, because it doesn't merge hashes.
def types(context = GraphQL::Query::NullContext.instance)
if use_visibility_profile?
types = Visibility::Profile.from_context(context, self)
return types.all_types_h
end
all_types = non_introspection_types.merge(introspection_system.types)
visible_types = {}
all_types.each do |k, v|
visible_types[k] =if v.is_a?(Array)
visible_t = nil
v.each do |t|
if t.visible?(context)
if visible_t.nil?
visible_t = t
else
raise DuplicateNamesError.new(
duplicated_name: k, duplicated_definition_1: visible_t.inspect, duplicated_definition_2: t.inspect
)
end
end
end
visible_t
else
v
end
end
visible_types
end
# @param type_name [String]
# @param context [GraphQL::Query::Context] Used for filtering definitions at query-time
# @param use_visibility_profile Private, for migration to {Schema::Visibility}
# @return [Module, nil] A type, or nil if there's no type called `type_name`
def get_type(type_name, context = GraphQL::Query::NullContext.instance, use_visibility_profile = use_visibility_profile?)
if use_visibility_profile
return Visibility::Profile.from_context(context, self).type(type_name)
end
local_entry = own_types[type_name]
type_defn = case local_entry
when nil
nil
when Array
if context.respond_to?(:types) && context.types.is_a?(GraphQL::Schema::Visibility::Profile)
local_entry
else
visible_t = nil
warden = Warden.from_context(context)
local_entry.each do |t|
if warden.visible_type?(t, context)
if visible_t.nil?
visible_t = t
else
raise DuplicateNamesError.new(
duplicated_name: type_name, duplicated_definition_1: visible_t.inspect, duplicated_definition_2: t.inspect
)
end
end
end
visible_t
end
when Module
local_entry
else
raise "Invariant: unexpected own_types[#{type_name.inspect}]: #{local_entry.inspect}"
end
type_defn ||
introspection_system.types[type_name] || # todo context-specific introspection?
(superclass.respond_to?(:get_type) ? superclass.get_type(type_name, context, use_visibility_profile) : nil)
end
# @return [Boolean] Does this schema have _any_ definition for a type named `type_name`, regardless of visibility?
def has_defined_type?(type_name)
own_types.key?(type_name) || introspection_system.types.key?(type_name) || (superclass.respond_to?(:has_defined_type?) ? superclass.has_defined_type?(type_name) : false)
end
# @api private
attr_writer :connections
# @return [GraphQL::Pagination::Connections] if installed
def connections
if defined?(@connections)
@connections
else
inherited_connections = find_inherited_value(:connections, nil)
# This schema is part of an inheritance chain which is using new connections,
# make a new instance, so we don't pollute the upstream one.
if inherited_connections
@connections = Pagination::Connections.new(schema: self)
else
nil
end
end
end
# Get or set the root `query { ... }` object for this schema.
#
# @example Using `Types::Query` as the entry-point
# query { Types::Query }
#
# @param new_query_object [Class<GraphQL::Schema::Object>] The root type to use for queries
# @param lazy_load_block If a block is given, then it will be called when GraphQL-Ruby needs the root query type.
# @return [Class<GraphQL::Schema::Object>, nil] The configured query root type, if there is one.
def query(new_query_object = nil, &lazy_load_block)
if new_query_object || block_given?
if @query_object
dup_defn = new_query_object || yield
raise GraphQL::Error, "Second definition of `query(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@query_object.inspect}"
elsif use_visibility_profile?
if block_given?
if visibility.preload?
@query_object = lazy_load_block.call
self.visibility.query_configured(@query_object)
else
@query_object = lazy_load_block
end
else
@query_object = new_query_object
self.visibility.query_configured(@query_object)
end
else
@query_object = new_query_object || lazy_load_block.call
add_type_and_traverse(@query_object, root: true)
end
nil
elsif @query_object.is_a?(Proc)
@query_object = @query_object.call
self.visibility&.query_configured(@query_object)
@query_object
else
@query_object || find_inherited_value(:query)
end
end
# Get or set the root `mutation { ... }` object for this schema.
#
# @example Using `Types::Mutation` as the entry-point
# mutation { Types::Mutation }
#
# @param new_mutation_object [Class<GraphQL::Schema::Object>] The root type to use for mutations
# @param lazy_load_block If a block is given, then it will be called when GraphQL-Ruby needs the root mutation type.
# @return [Class<GraphQL::Schema::Object>, nil] The configured mutation root type, if there is one.
def mutation(new_mutation_object = nil, &lazy_load_block)
if new_mutation_object || block_given?
if @mutation_object
dup_defn = new_mutation_object || yield
raise GraphQL::Error, "Second definition of `mutation(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@mutation_object.inspect}"
elsif use_visibility_profile?
if block_given?
if visibility.preload?
@mutation_object = lazy_load_block.call
self.visibility.mutation_configured(@mutation_object)
else
@mutation_object = lazy_load_block
end
else
@mutation_object = new_mutation_object
self.visibility.mutation_configured(@mutation_object)
end
else
@mutation_object = new_mutation_object || lazy_load_block.call
add_type_and_traverse(@mutation_object, root: true)
end
nil
elsif @mutation_object.is_a?(Proc)
@mutation_object = @mutation_object.call
self.visibility&.mutation_configured(@mutation_object)
@mutation_object
else
@mutation_object || find_inherited_value(:mutation)
end
end
# Get or set the root `subscription { ... }` object for this schema.
#
# @example Using `Types::Subscription` as the entry-point
# subscription { Types::Subscription }
#
# @param new_subscription_object [Class<GraphQL::Schema::Object>] The root type to use for subscriptions
# @param lazy_load_block If a block is given, then it will be called when GraphQL-Ruby needs the root subscription type.
# @return [Class<GraphQL::Schema::Object>, nil] The configured subscription root type, if there is one.
def subscription(new_subscription_object = nil, &lazy_load_block)
if new_subscription_object || block_given?
if @subscription_object
dup_defn = new_subscription_object || yield
raise GraphQL::Error, "Second definition of `subscription(...)` (#{dup_defn.inspect}) is invalid, already configured with #{@subscription_object.inspect}"
elsif use_visibility_profile?
if block_given?
if visibility.preload?
@subscription_object = lazy_load_block.call
visibility.subscription_configured(@subscription_object)
else
@subscription_object = lazy_load_block
end
else
@subscription_object = new_subscription_object
self.visibility.subscription_configured(@subscription_object)
end
add_subscription_extension_if_necessary
else
@subscription_object = new_subscription_object || lazy_load_block.call
add_subscription_extension_if_necessary
add_type_and_traverse(@subscription_object, root: true)
end
nil
elsif @subscription_object.is_a?(Proc)
@subscription_object = @subscription_object.call
add_subscription_extension_if_necessary
self.visibility.subscription_configured(@subscription_object)
@subscription_object
else
@subscription_object || find_inherited_value(:subscription)
end
end
# @api private
def root_type_for_operation(operation)
case operation
when "query"
query
when "mutation"
mutation
when "subscription"
subscription
else
raise ArgumentError, "unknown operation type: #{operation}"
end
end
# @return [Array<Class>] The root types (query, mutation, subscription) defined for this schema
def root_types
if use_visibility_profile?
[query, mutation, subscription].compact
else
@root_types
end
end
# @api private
def warden_class
if defined?(@warden_class)
@warden_class
elsif superclass.respond_to?(:warden_class)
superclass.warden_class
else
GraphQL::Schema::Warden
end
end
# @api private
attr_writer :warden_class
# @api private
def visibility_profile_class
if defined?(@visibility_profile_class)
@visibility_profile_class
elsif superclass.respond_to?(:visibility_profile_class)
superclass.visibility_profile_class
else
GraphQL::Schema::Visibility::Profile
end
end
# @api private
attr_writer :visibility_profile_class, :use_visibility_profile
# @api private
attr_accessor :visibility
# @api private
def use_visibility_profile?
if defined?(@use_visibility_profile)
@use_visibility_profile
elsif superclass.respond_to?(:use_visibility_profile?)
superclass.use_visibility_profile?
else
false
end
end
# @param type [Module] The type definition whose possible types you want to see
# @param context [GraphQL::Query::Context] used for filtering visible possible types at runtime
# @param use_visibility_profile Private, for migration to {Schema::Visibility}
# @return [Hash<String, Module>] All possible types, if no `type` is given.
# @return [Array<Module>] Possible types for `type`, if it's given.
def possible_types(type = nil, context = GraphQL::Query::NullContext.instance, use_visibility_profile = use_visibility_profile?)
if use_visibility_profile
if type
return Visibility::Profile.from_context(context, self).possible_types(type)
else
raise "Schema.possible_types is not implemented for `use_visibility_profile?`"
end
end
if type
# TODO duck-typing `.possible_types` would probably be nicer here
if type.kind.union?
type.possible_types(context: context)
else
stored_possible_types = own_possible_types[type]
visible_possible_types = if stored_possible_types && type.kind.interface?
stored_possible_types.select do |possible_type|
possible_type.interfaces(context).include?(type)
end
else
stored_possible_types
end
visible_possible_types ||
introspection_system.possible_types[type] ||
(
superclass.respond_to?(:possible_types) ?
superclass.possible_types(type, context, use_visibility_profile) :
EMPTY_ARRAY
)
end
else
find_inherited_value(:possible_types, EMPTY_HASH)
.merge(own_possible_types)
.merge(introspection_system.possible_types)
end
end
def union_memberships(type = nil)
if type
own_um = own_union_memberships.fetch(type.graphql_name, EMPTY_ARRAY)
inherited_um = find_inherited_value(:union_memberships, EMPTY_HASH).fetch(type.graphql_name, EMPTY_ARRAY)
own_um + inherited_um
else
joined_um = own_union_memberships.dup
find_inherited_value(:union_memberhips, EMPTY_HASH).each do |k, v|
um = joined_um[k] ||= []
um.concat(v)
end
joined_um
end
end
# @api private
# @see GraphQL::Dataloader
def dataloader_class
@dataloader_class || GraphQL::Dataloader::NullDataloader
end
attr_writer :dataloader_class
def references_to(to_type = nil, from: nil)
if to_type
if from
refs = own_references_to[to_type] ||= []
refs << from
else
get_references_to(to_type) || EMPTY_ARRAY
end
else
# `@own_references_to` can be quite large for big schemas,
# and generally speaking, we won't inherit any values.
# So optimize the most common case -- don't create a duplicate Hash.
inherited_value = find_inherited_value(:references_to, EMPTY_HASH)
if !inherited_value.empty?
inherited_value.merge(own_references_to)
else
own_references_to
end
end
end
def type_from_ast(ast_node, context: self.query_class.new(self, "{ __typename }").context)
GraphQL::Schema::TypeExpression.build_type(context.query.types, ast_node)
end
def get_field(type_or_name, field_name, context = GraphQL::Query::NullContext.instance)
parent_type = case type_or_name
when LateBoundType
get_type(type_or_name.name, context)
when String
get_type(type_or_name, context)
when Module
type_or_name
else
raise GraphQL::InvariantError, "Unexpected field owner for #{field_name.inspect}: #{type_or_name.inspect} (#{type_or_name.class})"
end
if parent_type.kind.fields? && (field = parent_type.get_field(field_name, context))
field
elsif parent_type == query && (entry_point_field = introspection_system.entry_point(name: field_name))
entry_point_field
elsif (dynamic_field = introspection_system.dynamic_field(name: field_name))
dynamic_field
else
nil
end
end
def get_fields(type, context = GraphQL::Query::NullContext.instance)
type.fields(context)
end
# Pass a custom introspection module here to use it for this schema.
# @param new_introspection_namespace [Module] If given, use this module for custom introspection on the schema
# @return [Module, nil] The configured namespace, if there is one
def introspection(new_introspection_namespace = nil)
if new_introspection_namespace
@introspection = new_introspection_namespace
# reset this cached value:
@introspection_system = nil
introspection_system
@introspection
else
@introspection || find_inherited_value(:introspection)
end
end
# @return [Schema::IntrospectionSystem] Based on {introspection}
def introspection_system
if !@introspection_system
@introspection_system = Schema::IntrospectionSystem.new(self)
@introspection_system.resolve_late_bindings
self.visibility&.introspection_system_configured(@introspection_system)
end
@introspection_system
end
def cursor_encoder(new_encoder = nil)
if new_encoder
@cursor_encoder = new_encoder
end
@cursor_encoder || find_inherited_value(:cursor_encoder, Base64Encoder)
end
def default_max_page_size(new_default_max_page_size = nil)
if new_default_max_page_size
@default_max_page_size = new_default_max_page_size
else
@default_max_page_size || find_inherited_value(:default_max_page_size)
end
end
# A limit on the number of tokens to accept on incoming query strings.
# Use this to prevent parsing maliciously-large query strings.
# @return [nil, Integer]
def max_query_string_tokens(new_max_tokens = NOT_CONFIGURED)
if NOT_CONFIGURED.equal?(new_max_tokens)
defined?(@max_query_string_tokens) ? @max_query_string_tokens : find_inherited_value(:max_query_string_tokens)
else
@max_query_string_tokens = new_max_tokens
end
end
def default_page_size(new_default_page_size = nil)
if new_default_page_size
@default_page_size = new_default_page_size
else
@default_page_size || find_inherited_value(:default_page_size)
end
end
def query_execution_strategy(new_query_execution_strategy = nil, deprecation_warning: true)
if deprecation_warning
warn "GraphQL::Schema.query_execution_strategy is deprecated without replacement. Use `GraphQL::Query.new` directly to create and execute a custom query instead."
warn " #{caller(1, 1).first}"
end
if new_query_execution_strategy
@query_execution_strategy = new_query_execution_strategy
else
@query_execution_strategy || (superclass.respond_to?(:query_execution_strategy) ? superclass.query_execution_strategy(deprecation_warning: false) : self.default_execution_strategy)
end
end
def mutation_execution_strategy(new_mutation_execution_strategy = nil, deprecation_warning: true)
if deprecation_warning
warn "GraphQL::Schema.mutation_execution_strategy is deprecated without replacement. Use `GraphQL::Query.new` directly to create and execute a custom query instead."
warn " #{caller(1, 1).first}"
end
if new_mutation_execution_strategy
@mutation_execution_strategy = new_mutation_execution_strategy
else
@mutation_execution_strategy || (superclass.respond_to?(:mutation_execution_strategy) ? superclass.mutation_execution_strategy(deprecation_warning: false) : self.default_execution_strategy)
end
end
def subscription_execution_strategy(new_subscription_execution_strategy = nil, deprecation_warning: true)
if deprecation_warning
warn "GraphQL::Schema.subscription_execution_strategy is deprecated without replacement. Use `GraphQL::Query.new` directly to create and execute a custom query instead."
warn " #{caller(1, 1).first}"
end
if new_subscription_execution_strategy
@subscription_execution_strategy = new_subscription_execution_strategy
else
@subscription_execution_strategy || (superclass.respond_to?(:subscription_execution_strategy) ? superclass.subscription_execution_strategy(deprecation_warning: false) : self.default_execution_strategy)
end
end
attr_writer :validate_timeout
def validate_timeout(new_validate_timeout = nil)
if new_validate_timeout
@validate_timeout = new_validate_timeout
elsif defined?(@validate_timeout)
@validate_timeout
else
find_inherited_value(:validate_timeout)
end
end
# Validate a query string according to this schema.
# @param string_or_document [String, GraphQL::Language::Nodes::Document]
# @return [Array<GraphQL::StaticValidation::Error >]
def validate(string_or_document, rules: nil, context: nil)
doc = if string_or_document.is_a?(String)
GraphQL.parse(string_or_document)
else
string_or_document
end
query = query_class.new(self, document: doc, context: context)
validator_opts = { schema: self }
rules && (validator_opts[:rules] = rules)
validator = GraphQL::StaticValidation::Validator.new(**validator_opts)
res = validator.validate(query, timeout: validate_timeout, max_errors: validate_max_errors)
res[:errors]
end
# @param new_query_class [Class<GraphQL::Query>] A subclass to use when executing queries
def query_class(new_query_class = NOT_CONFIGURED)
if NOT_CONFIGURED.equal?(new_query_class)
@query_class || (superclass.respond_to?(:query_class) ? superclass.query_class : GraphQL::Query)
else
@query_class = new_query_class
end
end
attr_writer :validate_max_errors
def validate_max_errors(new_validate_max_errors = NOT_CONFIGURED)
if NOT_CONFIGURED.equal?(new_validate_max_errors)
defined?(@validate_max_errors) ? @validate_max_errors : find_inherited_value(:validate_max_errors)
else
@validate_max_errors = new_validate_max_errors
end
end
attr_writer :max_complexity
def max_complexity(max_complexity = nil, count_introspection_fields: true)
if max_complexity
@max_complexity = max_complexity
@max_complexity_count_introspection_fields = count_introspection_fields
elsif defined?(@max_complexity)
@max_complexity
else
find_inherited_value(:max_complexity)
end
end
def max_complexity_count_introspection_fields
if defined?(@max_complexity_count_introspection_fields)
@max_complexity_count_introspection_fields
else
find_inherited_value(:max_complexity_count_introspection_fields, true)
end
end
attr_writer :analysis_engine
def analysis_engine
@analysis_engine || find_inherited_value(:analysis_engine, self.default_analysis_engine)
end
def error_bubbling(new_error_bubbling = nil)
if !new_error_bubbling.nil?
warn("error_bubbling(#{new_error_bubbling.inspect}) is deprecated; the default value of `false` will be the only option in GraphQL-Ruby 3.0")
@error_bubbling = new_error_bubbling
else
@error_bubbling.nil? ? find_inherited_value(:error_bubbling) : @error_bubbling
end
end
attr_writer :error_bubbling
attr_writer :max_depth
def max_depth(new_max_depth = nil, count_introspection_fields: true)
if new_max_depth
@max_depth = new_max_depth
@count_introspection_fields = count_introspection_fields
elsif defined?(@max_depth)
@max_depth
else
find_inherited_value(:max_depth)
end
end
def count_introspection_fields
if defined?(@count_introspection_fields)
@count_introspection_fields
else
find_inherited_value(:count_introspection_fields, true)
end
end
def disable_introspection_entry_points
@disable_introspection_entry_points = true
# TODO: this clears the cache made in `def types`. But this is not a great solution.
@introspection_system = nil
end
def disable_schema_introspection_entry_point
@disable_schema_introspection_entry_point = true
# TODO: this clears the cache made in `def types`. But this is not a great solution.
@introspection_system = nil
end
def disable_type_introspection_entry_point
@disable_type_introspection_entry_point = true
# TODO: this clears the cache made in `def types`. But this is not a great solution.
@introspection_system = nil
end
def disable_introspection_entry_points?
if instance_variable_defined?(:@disable_introspection_entry_points)
@disable_introspection_entry_points
else
find_inherited_value(:disable_introspection_entry_points?, false)
end
end
def disable_schema_introspection_entry_point?
if instance_variable_defined?(:@disable_schema_introspection_entry_point)
@disable_schema_introspection_entry_point
else
find_inherited_value(:disable_schema_introspection_entry_point?, false)
end
end
def disable_type_introspection_entry_point?
if instance_variable_defined?(:@disable_type_introspection_entry_point)
@disable_type_introspection_entry_point
else
find_inherited_value(:disable_type_introspection_entry_point?, false)
end
end
# @param new_extra_types [Module] Type definitions to include in printing and introspection, even though they aren't referenced in the schema
# @return [Array<Module>] Type definitions added to this schema
def extra_types(*new_extra_types)
if !new_extra_types.empty?
new_extra_types = new_extra_types.flatten
@own_extra_types ||= []
@own_extra_types.concat(new_extra_types)
end
inherited_et = find_inherited_value(:extra_types, nil)
if inherited_et
if @own_extra_types
inherited_et + @own_extra_types
else
inherited_et
end
else
@own_extra_types || EMPTY_ARRAY
end
end
# Tell the schema about these types so that they can be registered as implementations of interfaces in the schema.
#
# This method must be used when an object type is connected to the schema as an interface implementor but
# not as a return type of a field. In that case, if the object type isn't registered here, GraphQL-Ruby won't be able to find it.
#
# @param new_orphan_types [Array<Class<GraphQL::Schema::Object>>] Object types to register as implementations of interfaces in the schema.
# @return [Array<Class<GraphQL::Schema::Object>>] All previously-registered orphan types for this schema
def orphan_types(*new_orphan_types)
if !new_orphan_types.empty?
new_orphan_types = new_orphan_types.flatten