@@ -11,8 +11,12 @@ module Language
11
11
# @param include_introspection_types [Boolean] Wether or not to print introspection types
12
12
# @param include_introspection_types [Boolean] Wether or not to print built in types and directives
13
13
class DocumentFromSchemaDefinition
14
- def initialize ( schema , context : nil , only : [ ] , except : [ ] , include_introspection_types : false , include_built_ins : false )
14
+ def initialize (
15
+ schema , context : nil , only : nil , except : nil , include_introspection_types : false ,
16
+ include_built_ins : false , always_include_schema : false
17
+ )
15
18
@schema = schema
19
+ @always_include_schema = always_include_schema
16
20
17
21
filter = GraphQL ::Language ::DocumentFromSchemaDefinition ::Filter . new (
18
22
only ,
@@ -36,16 +40,18 @@ def document
36
40
37
41
protected
38
42
39
- def build_schema_node ( schema )
40
- schema_node = GraphQL ::Language ::Nodes ::SchemaDefinition . new (
41
- query : schema . query . name
42
- )
43
+ def build_schema_node
44
+ schema_node = GraphQL ::Language ::Nodes ::SchemaDefinition . new
45
+
46
+ if schema . query && warden . get_type ( schema . query . name )
47
+ schema_node . query = schema . query . name
48
+ end
43
49
44
- if schema . mutation
50
+ if schema . mutation && warden . get_type ( schema . mutation . name )
45
51
schema_node . mutation = schema . mutation . name
46
52
end
47
53
48
- if schema . subscription
54
+ if schema . subscription && warden . get_type ( schema . subscription . name )
49
55
schema_node . subscription = schema . subscription . name
50
56
end
51
57
@@ -55,16 +61,16 @@ def build_schema_node(schema)
55
61
def build_object_type_node ( object_type )
56
62
GraphQL ::Language ::Nodes ::ObjectTypeDefinition . new (
57
63
name : object_type . name ,
58
- interfaces : object_type . interfaces . map { |iface | build_type_name_node ( iface ) } ,
59
- fields : build_field_nodes ( object_type . fields . values ) ,
64
+ interfaces : warden . interfaces ( object_type ) . map { |iface | build_type_name_node ( iface ) } ,
65
+ fields : build_field_nodes ( warden . fields ( object_type ) ) ,
60
66
description : object_type . description ,
61
67
)
62
68
end
63
69
64
70
def build_field_node ( field )
65
71
GraphQL ::Language ::Nodes ::FieldDefinition . new (
66
72
name : field . name ,
67
- arguments : build_argument_nodes ( field . arguments . values ) ,
73
+ arguments : build_argument_nodes ( warden . arguments ( field ) ) ,
68
74
type : build_type_name_node ( field . type ) ,
69
75
description : field . description ,
70
76
)
@@ -74,22 +80,22 @@ def build_union_type_node(union_type)
74
80
GraphQL ::Language ::Nodes ::UnionTypeDefinition . new (
75
81
name : union_type . name ,
76
82
description : union_type . description ,
77
- types : union_type . possible_types . map { |type | build_type_name_node ( type ) }
83
+ types : warden . possible_types ( union_type ) . map { |type | build_type_name_node ( type ) }
78
84
)
79
85
end
80
86
81
87
def build_interface_type_node ( interface_type )
82
88
GraphQL ::Language ::Nodes ::InterfaceTypeDefinition . new (
83
89
name : interface_type . name ,
84
90
description : interface_type . description ,
85
- fields : build_field_nodes ( interface_type . fields . values )
91
+ fields : build_field_nodes ( warden . fields ( interface_type ) )
86
92
)
87
93
end
88
94
89
95
def build_enum_type_node ( enum_type )
90
96
GraphQL ::Language ::Nodes ::EnumTypeDefinition . new (
91
97
name : enum_type . name ,
92
- values : enum_type . values . values . map do |enum_value |
98
+ values : warden . enum_values ( enum_type ) . map do |enum_value |
93
99
build_enum_value_node ( enum_value )
94
100
end ,
95
101
description : enum_type . description ,
@@ -122,15 +128,15 @@ def build_argument_node(argument)
122
128
def build_input_object_node ( input_object )
123
129
GraphQL ::Language ::Nodes ::InputObjectTypeDefinition . new (
124
130
name : input_object . name ,
125
- fields : build_argument_nodes ( input_object . arguments . values ) ,
131
+ fields : build_argument_nodes ( warden . arguments ( input_object ) ) ,
126
132
description : input_object . description ,
127
133
)
128
134
end
129
135
130
136
def build_directive_node ( directive )
131
137
GraphQL ::Language ::Nodes ::DirectiveDefinition . new (
132
138
name : directive . name ,
133
- arguments : build_argument_nodes ( directive . arguments . values ) ,
139
+ arguments : build_argument_nodes ( warden . arguments ( directive ) ) ,
134
140
locations : directive . locations . map ( &:to_s ) ,
135
141
description : directive . description ,
136
142
)
@@ -180,8 +186,8 @@ def build_directive_nodes(directives)
180
186
181
187
def build_definition_nodes
182
188
definitions = build_type_definition_nodes ( warden . types )
183
- definitions += build_directive_nodes ( warden . directives . values )
184
- definitions << build_schema_node ( schema )
189
+ definitions += build_directive_nodes ( warden . directives )
190
+ definitions << build_schema_node if include_schema_node?
185
191
definitions
186
192
end
187
193
@@ -221,7 +227,10 @@ def call(member, context)
221
227
222
228
private
223
229
224
- def instrospection? ( member )
230
+ attr_reader :include_introspection_types , :include_built_ins ,
231
+ :only , :except
232
+
233
+ def introspection? ( member )
225
234
member . is_a? ( BaseType ) && member . introspection?
226
235
end
227
236
@@ -233,7 +242,11 @@ def built_in?(member)
233
242
234
243
private
235
244
236
- attr_reader :schema , :types
245
+ def include_schema_node?
246
+ @always_include_schema || !schema . respects_root_name_conventions?
247
+ end
248
+
249
+ attr_reader :schema , :warden
237
250
end
238
251
end
239
252
end
0 commit comments