Skip to content

Commit 232447b

Browse files
author
Marc-Andre Giroux
committed
fix warden, sort by name
1 parent 1dd0adf commit 232447b

File tree

3 files changed

+48
-95
lines changed

3 files changed

+48
-95
lines changed

lib/graphql/language/document_from_schema_definition.rb

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@ def initialize(
1717
)
1818
@schema = schema
1919
@always_include_schema = always_include_schema
20-
21-
filter = GraphQL::Language::DocumentFromSchemaDefinition::Filter.new(
22-
only,
23-
except,
24-
include_introspection_types: include_introspection_types,
25-
include_built_ins: include_built_ins,
26-
)
20+
@include_introspection_types = include_introspection_types
21+
@include_built_ins = include_built_ins
2722

2823
@warden = GraphQL::Schema::Warden.new(
29-
filter,
24+
GraphQL::Filter.new(only: only, except: except),
3025
schema: @schema,
31-
context: @context
26+
context: context,
3227
)
3328
end
3429

@@ -59,7 +54,7 @@ def build_schema_node
5954
def build_object_type_node(object_type)
6055
GraphQL::Language::Nodes::ObjectTypeDefinition.new(
6156
name: object_type.name,
62-
interfaces: warden.interfaces(object_type).map { |iface| build_type_name_node(iface) },
57+
interfaces: warden.interfaces(object_type).sort_by(&:name).map { |iface| build_type_name_node(iface) },
6358
fields: build_field_nodes(warden.fields(object_type)),
6459
description: object_type.description,
6560
)
@@ -78,7 +73,7 @@ def build_union_type_node(union_type)
7873
GraphQL::Language::Nodes::UnionTypeDefinition.new(
7974
name: union_type.name,
8075
description: union_type.description,
81-
types: warden.possible_types(union_type).map { |type| build_type_name_node(type) }
76+
types: warden.possible_types(union_type).sort_by(&:name).map { |type| build_type_name_node(type) }
8277
)
8378
end
8479

@@ -93,7 +88,7 @@ def build_interface_type_node(interface_type)
9388
def build_enum_type_node(enum_type)
9489
GraphQL::Language::Nodes::EnumTypeDefinition.new(
9590
name: enum_type.name,
96-
values: warden.enum_values(enum_type).map do |enum_value|
91+
values: warden.enum_values(enum_type).sort_by(&:name).map do |enum_value|
9792
build_enum_value_node(enum_value)
9893
end,
9994
description: enum_type.description,
@@ -175,11 +170,19 @@ def build_type_definition_node(type)
175170
end
176171

177172
def build_argument_nodes(arguments)
178-
arguments.map { |arg| build_argument_node(arg) }
173+
arguments
174+
.map { |arg| build_argument_node(arg) }
175+
.sort_by(&:name)
179176
end
180177

181178
def build_directive_nodes(directives)
182-
directives.map { |directive| build_directive_node(directive) }
179+
if !include_built_ins
180+
directives = directives.reject { |directive| built_in?(directive) }
181+
end
182+
183+
directives
184+
.map { |directive| build_directive_node(directive) }
185+
.sort_by(&:name)
183186
end
184187

185188
def build_definition_nodes
@@ -190,61 +193,42 @@ def build_definition_nodes
190193
end
191194

192195
def build_type_definition_nodes(types)
193-
types.map { |type| build_type_definition_node(type) }
194-
end
195-
196-
def build_field_nodes(fields)
197-
fields.map { |field| build_field_node(field) }
198-
end
199-
200-
class Filter
201-
def initialize(only, except, include_introspection_types:, include_built_ins:)
202-
@only = only
203-
@except = except
204-
@include_introspection_types = include_introspection_types
205-
@include_built_ins = include_built_ins
196+
if !include_introspection_types
197+
types = types.reject { |type| introspection?(type) }
206198
end
207199

208-
def call(member, context)
209-
if !include_introspection_types && introspection?(member)
210-
return false
211-
end
212-
213-
if !include_built_ins && built_in?(member)
214-
return false
215-
end
216-
217-
if only
218-
!only.call(member, context)
219-
elsif except
220-
except.call(member, context)
221-
else
222-
true
223-
end
200+
if !include_built_ins
201+
types = types.reject { |type| built_in?(type) }
224202
end
225203

226-
private
204+
types
205+
.map { |type| build_type_definition_node(type) }
206+
.sort_by(&:name)
207+
end
227208

228-
attr_reader :include_introspection_types, :include_built_ins,
229-
:only, :except
209+
def build_field_nodes(fields)
210+
fields
211+
.map { |field| build_field_node(field) }
212+
.sort_by(&:name)
213+
end
230214

231-
def introspection?(member)
232-
member.is_a?(BaseType) && member.introspection?
233-
end
215+
private
234216

235-
def built_in?(member)
236-
(member.is_a?(GraphQL::ScalarType) && member.default_scalar?) ||
237-
(member.is_a?(GraphQL::Directive) && member.default_directive?)
238-
end
217+
def introspection?(member)
218+
member.is_a?(BaseType) && member.introspection?
239219
end
240220

241-
private
221+
def built_in?(member)
222+
(member.is_a?(GraphQL::ScalarType) && member.default_scalar?) ||
223+
(member.is_a?(GraphQL::Directive) && member.default_directive?)
224+
end
242225

243226
def include_schema_node?
244-
@always_include_schema || !schema.respects_root_name_conventions?
227+
always_include_schema || !schema.respects_root_name_conventions?
245228
end
246229

247-
attr_reader :schema, :warden
230+
attr_reader :schema, :warden, :always_include_schema,
231+
:include_introspection_types, :include_built_ins
248232
end
249233
end
250234
end

lib/graphql/language/printer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def print_node(node, indent: "")
319319
print_input_object_type_definition(node)
320320
when Nodes::DirectiveDefinition
321321
print_directive_definition(node)
322-
when FalseClass, Float, Integer, NilClass, String, TrueClass
322+
when FalseClass, Float, Integer, NilClass, String, TrueClass, Symbol
323323
GraphQL::Language.serialize(node)
324324
when Array
325325
"[#{node.map { |v| print_node(v) }.join(", ")}]".dup

lib/graphql/schema/printer.rb

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Printer
4545
# @param except [<#call(member, ctx)>]
4646
# @param introspection [Boolean] Should include the introspection types in the string?
4747
def initialize(schema, context: nil, only: nil, except: nil, introspection: false)
48-
@document_generator = GraphQL::Language::DocumentFromSchemaDefinition.new(
48+
@document_from_schema = GraphQL::Language::DocumentFromSchemaDefinition.new(
4949
schema,
5050
context: context,
5151
only: only,
@@ -54,6 +54,8 @@ def initialize(schema, context: nil, only: nil, except: nil, introspection: fals
5454
include_built_ins: false,
5555
)
5656

57+
@document = @document_from_schema.document
58+
5759
@schema = schema
5860
end
5961

@@ -78,50 +80,17 @@ def self.print_schema(schema, **args)
7880

7981
# Return a GraphQL schema string for the defined types in the schema
8082
def print_schema
81-
document = @document_generator.document
82-
document.to_query_string
83+
@document.to_query_string
8384
end
8485

8586
def print_type(type)
86-
TypeKindPrinters::STRATEGIES.fetch(type.kind).print(warden, type)
87+
binding.pry
88+
node = @document_from_schema.build_object_type_node(type)
89+
node.to_query_string
8790
end
8891

8992
private
9093

91-
# By default, these are included in a schema printout
92-
IS_USER_DEFINED_MEMBER = ->(member) {
93-
case member
94-
when GraphQL::BaseType
95-
!member.introspection?
96-
when GraphQL::Directive
97-
!member.default_directive?
98-
else
99-
true
100-
end
101-
}
102-
103-
private_constant :IS_USER_DEFINED_MEMBER
104-
105-
def build_blacklist(only, except, introspection:)
106-
if introspection
107-
if only
108-
->(m, ctx) { !only.call(m, ctx) }
109-
elsif except
110-
except
111-
else
112-
->(m, ctx) { false }
113-
end
114-
else
115-
if only
116-
->(m, ctx) { !(IS_USER_DEFINED_MEMBER.call(m) && only.call(m, ctx)) }
117-
elsif except
118-
->(m, ctx) { !IS_USER_DEFINED_MEMBER.call(m) || except.call(m, ctx) }
119-
else
120-
->(m, ctx) { !IS_USER_DEFINED_MEMBER.call(m) }
121-
end
122-
end
123-
end
124-
12594
def print_schema_definition
12695
if (schema.query.nil? || schema.query.name == 'Query') &&
12796
(schema.mutation.nil? || schema.mutation.name == 'Mutation') &&

0 commit comments

Comments
 (0)