Skip to content

Commit 07b00fa

Browse files
committed
Add fix and tests
1 parent efe661a commit 07b00fa

3 files changed

Lines changed: 63 additions & 10 deletions

File tree

src/main/java/graphql/language/SDLExtensionDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import graphql.PublicApi;
55

66
/**
7-
* An marker interface for Schema Definition Language (SDL) extension definitions.
7+
* A marker interface for Schema Definition Language (SDL) extension definitions.
88
*/
99
@PublicApi
1010
public interface SDLExtensionDefinition {

src/main/java/graphql/schema/idl/SchemaGeneratorHelper.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -909,10 +909,13 @@ void buildOperations(BuildContext buildCtx, GraphQLSchema.Builder schemaBuilder)
909909

910910
Optional<OperationTypeDefinition> mutationOperation = getOperationNamed("mutation", operationTypeDefs);
911911
if (!mutationOperation.isPresent()) {
912-
Optional<TypeDefinition> mutationTypeDef = typeRegistry.getType("Mutation");
913-
if (mutationTypeDef.isPresent()) {
914-
mutation = buildOutputType(buildCtx, TypeName.newTypeName().name(mutationTypeDef.get().getName()).build());
915-
schemaBuilder.mutation(mutation);
912+
if (!typeRegistry.schemaDefinition().isPresent()) {
913+
// If no schema definition, then there is no schema keyword. Default to using type called Mutation
914+
Optional<TypeDefinition> mutationTypeDef = typeRegistry.getType("Mutation");
915+
if (mutationTypeDef.isPresent()) {
916+
mutation = buildOutputType(buildCtx, TypeName.newTypeName().name(mutationTypeDef.get().getName()).build());
917+
schemaBuilder.mutation(mutation);
918+
}
916919
}
917920
} else {
918921
mutation = buildOperation(buildCtx, mutationOperation.get());
@@ -921,10 +924,13 @@ void buildOperations(BuildContext buildCtx, GraphQLSchema.Builder schemaBuilder)
921924

922925
Optional<OperationTypeDefinition> subscriptionOperation = getOperationNamed("subscription", operationTypeDefs);
923926
if (!subscriptionOperation.isPresent()) {
924-
Optional<TypeDefinition> subscriptionTypeDef = typeRegistry.getType("Subscription");
925-
if (subscriptionTypeDef.isPresent()) {
926-
subscription = buildOutputType(buildCtx, TypeName.newTypeName().name(subscriptionTypeDef.get().getName()).build());
927-
schemaBuilder.subscription(subscription);
927+
if (!typeRegistry.schemaDefinition().isPresent()) {
928+
// If no schema definition, then there is no schema keyword. Default to using type called Subscription
929+
Optional<TypeDefinition> subscriptionTypeDef = typeRegistry.getType("Subscription");
930+
if (subscriptionTypeDef.isPresent()) {
931+
subscription = buildOutputType(buildCtx, TypeName.newTypeName().name(subscriptionTypeDef.get().getName()).build());
932+
schemaBuilder.subscription(subscription);
933+
}
928934
}
929935
} else {
930936
subscription = buildOperation(buildCtx, subscriptionOperation.get());

src/test/groovy/graphql/schema/idl/SchemaParserTest.groovy

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class SchemaParserTest extends Specification {
363363
364364
}
365365
366-
def "correctly parses schema keyword block"() {
366+
def "correctly parses schema keyword block, include Query, does not include Mutation type"() {
367367
// From RFC to clarify spec https://github.com/graphql/graphql-spec/pull/987
368368
when:
369369
def graphQL = TestUtil.graphQL("""
@@ -389,4 +389,51 @@ class SchemaParserTest extends Specification {
389389
graphQL.graphQLSchema.queryType != null
390390
graphQL.graphQLSchema.mutationType == null
391391
}
392+
393+
def "correctly parses schema keyword block, include Query, does not include Subscription type"() {
394+
// From RFC to clarify spec https://github.com/graphql/graphql-spec/pull/987
395+
when:
396+
def graphQL = TestUtil.graphQL("""
397+
schema {
398+
query: Query
399+
}
400+
type Query {
401+
viruses: [Virus!]
402+
}
403+
type Virus {
404+
name: String!
405+
}
406+
type Subscription {
407+
newspaper: String!
408+
}
409+
""").build()
410+
411+
then:
412+
graphQL.graphQLSchema.definition.operationTypeDefinitions.size() == 1
413+
graphQL.graphQLSchema.definition.operationTypeDefinitions.first().name == "query"
414+
graphQL.graphQLSchema.queryType != null
415+
graphQL.graphQLSchema.subscriptionType == null
416+
}
417+
418+
def "correctly parses schema that does not contain the schema keyword, includes Query and Mutation types"() {
419+
when:
420+
def schema = """
421+
type Mutation {
422+
name: String!
423+
geneSequence: String!
424+
}
425+
type Query {
426+
viruses: [Virus!]
427+
}
428+
type Virus {
429+
name: String!
430+
}
431+
"""
432+
def graphQL = TestUtil.graphQL(schema).build()
433+
434+
then:
435+
graphQL.graphQLSchema.definition == null // No SchemaDefinition
436+
graphQL.graphQLSchema.queryType != null
437+
graphQL.graphQLSchema.mutationType != null
438+
}
392439
}

0 commit comments

Comments
 (0)