Skip to content

Commit dcfb97d

Browse files
author
Marc-Andre Giroux
committed
add some warden tests for schema to ast
1 parent 33db157 commit dcfb97d

File tree

2 files changed

+668
-313
lines changed

2 files changed

+668
-313
lines changed

lib/graphql/language/document_from_schema_definition.rb

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ module Language
1111
# @param include_introspection_types [Boolean] Wether or not to print introspection types
1212
# @param include_introspection_types [Boolean] Wether or not to print built in types and directives
1313
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+
)
1518
@schema = schema
19+
@always_include_schema = always_include_schema
1620

1721
filter = GraphQL::Language::DocumentFromSchemaDefinition::Filter.new(
1822
only,
@@ -36,16 +40,18 @@ def document
3640

3741
protected
3842

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
4349

44-
if schema.mutation
50+
if schema.mutation && warden.get_type(schema.mutation.name)
4551
schema_node.mutation = schema.mutation.name
4652
end
4753

48-
if schema.subscription
54+
if schema.subscription && warden.get_type(schema.subscription.name)
4955
schema_node.subscription = schema.subscription.name
5056
end
5157

@@ -55,16 +61,16 @@ def build_schema_node(schema)
5561
def build_object_type_node(object_type)
5662
GraphQL::Language::Nodes::ObjectTypeDefinition.new(
5763
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)),
6066
description: object_type.description,
6167
)
6268
end
6369

6470
def build_field_node(field)
6571
GraphQL::Language::Nodes::FieldDefinition.new(
6672
name: field.name,
67-
arguments: build_argument_nodes(field.arguments.values),
73+
arguments: build_argument_nodes(warden.arguments(field)),
6874
type: build_type_name_node(field.type),
6975
description: field.description,
7076
)
@@ -74,22 +80,22 @@ def build_union_type_node(union_type)
7480
GraphQL::Language::Nodes::UnionTypeDefinition.new(
7581
name: union_type.name,
7682
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) }
7884
)
7985
end
8086

8187
def build_interface_type_node(interface_type)
8288
GraphQL::Language::Nodes::InterfaceTypeDefinition.new(
8389
name: interface_type.name,
8490
description: interface_type.description,
85-
fields: build_field_nodes(interface_type.fields.values)
91+
fields: build_field_nodes(warden.fields(interface_type))
8692
)
8793
end
8894

8995
def build_enum_type_node(enum_type)
9096
GraphQL::Language::Nodes::EnumTypeDefinition.new(
9197
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|
9399
build_enum_value_node(enum_value)
94100
end,
95101
description: enum_type.description,
@@ -122,15 +128,15 @@ def build_argument_node(argument)
122128
def build_input_object_node(input_object)
123129
GraphQL::Language::Nodes::InputObjectTypeDefinition.new(
124130
name: input_object.name,
125-
fields: build_argument_nodes(input_object.arguments.values),
131+
fields: build_argument_nodes(warden.arguments(input_object)),
126132
description: input_object.description,
127133
)
128134
end
129135

130136
def build_directive_node(directive)
131137
GraphQL::Language::Nodes::DirectiveDefinition.new(
132138
name: directive.name,
133-
arguments: build_argument_nodes(directive.arguments.values),
139+
arguments: build_argument_nodes(warden.arguments(directive)),
134140
locations: directive.locations.map(&:to_s),
135141
description: directive.description,
136142
)
@@ -180,8 +186,8 @@ def build_directive_nodes(directives)
180186

181187
def build_definition_nodes
182188
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?
185191
definitions
186192
end
187193

@@ -221,7 +227,10 @@ def call(member, context)
221227

222228
private
223229

224-
def instrospection?(member)
230+
attr_reader :include_introspection_types, :include_built_ins,
231+
:only, :except
232+
233+
def introspection?(member)
225234
member.is_a?(BaseType) && member.introspection?
226235
end
227236

@@ -233,7 +242,11 @@ def built_in?(member)
233242

234243
private
235244

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
237250
end
238251
end
239252
end

0 commit comments

Comments
 (0)