Skip to content

Commit 2c77433

Browse files
committed
Support custom "subscription" type name
Closes gh-590
1 parent 496bc31 commit 2c77433

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -132,7 +132,7 @@ private GraphQLSchema applyTypeVisitorsToTransformSchema(GraphQLSchema schema) {
132132
}
133133

134134
private GraphQLSchema applyTypeVisitors(GraphQLSchema schema) {
135-
GraphQLTypeVisitor visitor = ContextDataFetcherDecorator.createVisitor(this.subscriptionExceptionResolvers);
135+
GraphQLTypeVisitor visitor = ContextDataFetcherDecorator.createVisitor(schema, this.subscriptionExceptionResolvers);
136136
List<GraphQLTypeVisitor> visitors = new ArrayList<>(this.typeVisitors);
137137
visitors.add(visitor);
138138

spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@
2424
import graphql.schema.GraphQLCodeRegistry;
2525
import graphql.schema.GraphQLFieldDefinition;
2626
import graphql.schema.GraphQLFieldsContainer;
27+
import graphql.schema.GraphQLObjectType;
28+
import graphql.schema.GraphQLSchema;
2729
import graphql.schema.GraphQLSchemaElement;
2830
import graphql.schema.GraphQLTypeVisitor;
2931
import graphql.schema.GraphQLTypeVisitorStub;
@@ -97,9 +99,13 @@ public Object get(DataFetchingEnvironment environment) throws Exception {
9799
* Static factory method to create {@link GraphQLTypeVisitor} that wraps
98100
* data fetchers with the {@link ContextDataFetcherDecorator}.
99101
*/
100-
static GraphQLTypeVisitor createVisitor(List<SubscriptionExceptionResolver> resolvers) {
102+
static GraphQLTypeVisitor createVisitor(
103+
GraphQLSchema schema, List<SubscriptionExceptionResolver> resolvers) {
101104

102-
SubscriptionExceptionResolver compositeResolver = new CompositeSubscriptionExceptionResolver(resolvers);
105+
GraphQLObjectType subscriptionType = schema.getSubscriptionType();
106+
String subscriptionTypeName = (subscriptionType != null ? subscriptionType.getName() : null);
107+
108+
SubscriptionExceptionResolver exceptionResolver = new CompositeSubscriptionExceptionResolver(resolvers);
103109

104110
return new GraphQLTypeVisitorStub() {
105111
@Override
@@ -111,8 +117,8 @@ public TraversalControl visitGraphQLFieldDefinition(
111117
DataFetcher<?> dataFetcher = codeRegistry.getDataFetcher(parent, fieldDefinition);
112118

113119
if (applyDecorator(dataFetcher)) {
114-
boolean handlesSubscription = parent.getName().equals("Subscription");
115-
dataFetcher = new ContextDataFetcherDecorator(dataFetcher, handlesSubscription, compositeResolver);
120+
boolean handlesSubscription = parent.getName().equals(subscriptionTypeName);
121+
dataFetcher = new ContextDataFetcherDecorator(dataFetcher, handlesSubscription, exceptionResolver);
116122
codeRegistry.dataFetcher(parent, fieldDefinition, dataFetcher);
117123
}
118124

0 commit comments

Comments
 (0)