Skip to content

Commit eec0f6f

Browse files
author
Marc-Andre Giroux
committed
rm respects_convention from schema, fix docs
1 parent 2406754 commit eec0f6f

File tree

2 files changed

+15
-35
lines changed

2 files changed

+15
-35
lines changed

lib/graphql/language/document_from_schema_definition.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ module Language
99
# @param context [Hash]
1010
# @param only [<#call(member, ctx)>]
1111
# @param except [<#call(member, ctx)>]
12-
# @param include_introspection_types [Boolean] Wether or not to include introspection types in the AST
13-
# @param include_built_in_scalars [Boolean] Wether or not to include built in scalars in the AST
14-
# @param include_built_in_directives [Boolean] Wether or not to include built in diirectives in the AST
12+
# @param include_introspection_types [Boolean] Whether or not to include introspection types in the AST
13+
# @param include_built_in_scalars [Boolean] Whether or not to include built in scalars in the AST
14+
# @param include_built_in_directives [Boolean] Whether or not to include built in diirectives in the AST
1515
class DocumentFromSchemaDefinition
1616
def initialize(
1717
schema, context: nil, only: nil, except: nil, include_introspection_types: false,
@@ -39,16 +39,12 @@ def document
3939
def build_schema_node
4040
schema_node = GraphQL::Language::Nodes::SchemaDefinition.new
4141

42-
if schema.query && warden.get_type(schema.query.name)
43-
schema_node.query = schema.query.name
44-
end
45-
46-
if schema.mutation && warden.get_type(schema.mutation.name)
47-
schema_node.mutation = schema.mutation.name
48-
end
42+
["query", "mutation", "subscription"].each do |operation_name|
43+
root_type = schema.root_type_for_operation(operation_name)
4944

50-
if schema.subscription && warden.get_type(schema.subscription.name)
51-
schema_node.subscription = schema.subscription.name
45+
if root_type
46+
schema_node.public_send("#{operation_name}=", warden.get_type(root_type.name))
47+
end
5248
end
5349

5450
schema_node
@@ -271,7 +267,13 @@ def build_field_nodes(fields)
271267
private
272268

273269
def include_schema_node?
274-
always_include_schema || !schema.respects_root_name_conventions?
270+
always_include_schema || !schema_respects_root_name_conventions?(schema)
271+
end
272+
273+
def schema_respects_root_name_conventions?(schema)
274+
(schema.query.nil? || schema.query.name == 'Query') &&
275+
(schema.mutation.nil? || schema.mutation.name == 'Mutation') &&
276+
(schema.subscription.nil? || schema.subscription.name == 'Subscription')
275277
end
276278

277279
attr_reader :schema, :warden, :always_include_schema,

spec/graphql/schema_spec.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,6 @@
1717
end
1818
end
1919

20-
describe "#respects_root_name_conventions?" do
21-
it "returns true when schema respects root name conventions" do
22-
schema = GraphQL::Schema.define do
23-
query GraphQL::ObjectType.define { name "Query" }
24-
mutation GraphQL::ObjectType.define { name "Mutation" }
25-
subscription GraphQL::ObjectType.define { name "Subscription" }
26-
end
27-
28-
assert schema.respects_root_name_conventions?
29-
end
30-
31-
it "returns false when schema respects root name conventions" do
32-
schema = GraphQL::Schema.define do
33-
query GraphQL::ObjectType.define { name "Query" }
34-
mutation GraphQL::ObjectType.define { name "MutationType" }
35-
subscription GraphQL::ObjectType.define { name "Subscription" }
36-
end
37-
38-
assert_equal false, schema.respects_root_name_conventions?
39-
end
40-
end
41-
4220
describe "#union_memberships" do
4321
it "returns a list of unions that include the type" do
4422
assert_equal [schema.types["Animal"], schema.types["AnimalAsCow"]], schema.union_memberships(schema.types["Cow"])

0 commit comments

Comments
 (0)