Skip to content

Commit 3666112

Browse files
committed
Support custom "subscription" type name
Closes gh-590
1 parent 9066c40 commit 3666112

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

Lines changed: 2 additions & 2 deletions
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.
@@ -113,7 +113,7 @@ public GraphQlSource build() {
113113
protected abstract GraphQLSchema initGraphQlSchema();
114114

115115
private GraphQLSchema applyTypeVisitors(GraphQLSchema schema) {
116-
GraphQLTypeVisitor visitor = ContextDataFetcherDecorator.createVisitor(this.subscriptionExceptionResolvers);
116+
GraphQLTypeVisitor visitor = ContextDataFetcherDecorator.createVisitor(schema, this.subscriptionExceptionResolvers);
117117
List<GraphQLTypeVisitor> visitors = new ArrayList<>(this.typeVisitors);
118118
visitors.add(visitor);
119119

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

Lines changed: 11 additions & 5 deletions
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;
@@ -103,9 +105,13 @@ public Object get(DataFetchingEnvironment environment) throws Exception {
103105
* Static factory method to create {@link GraphQLTypeVisitor} that wraps
104106
* data fetchers with the {@link ContextDataFetcherDecorator}.
105107
*/
106-
static GraphQLTypeVisitor createVisitor(List<SubscriptionExceptionResolver> resolvers) {
108+
static GraphQLTypeVisitor createVisitor(
109+
GraphQLSchema schema, List<SubscriptionExceptionResolver> resolvers) {
107110

108-
SubscriptionExceptionResolver compositeResolver = new CompositeSubscriptionExceptionResolver(resolvers);
111+
GraphQLObjectType subscriptionType = schema.getSubscriptionType();
112+
String subscriptionTypeName = (subscriptionType != null ? subscriptionType.getName() : null);
113+
114+
SubscriptionExceptionResolver exceptionResolver = new CompositeSubscriptionExceptionResolver(resolvers);
109115

110116
return new GraphQLTypeVisitorStub() {
111117
@Override
@@ -117,8 +123,8 @@ public TraversalControl visitGraphQLFieldDefinition(
117123
DataFetcher<?> dataFetcher = codeRegistry.getDataFetcher(parent, fieldDefinition);
118124

119125
if (applyDecorator(dataFetcher)) {
120-
boolean handlesSubscription = parent.getName().equals("Subscription");
121-
dataFetcher = new ContextDataFetcherDecorator(dataFetcher, handlesSubscription, compositeResolver);
126+
boolean handlesSubscription = parent.getName().equals(subscriptionTypeName);
127+
dataFetcher = new ContextDataFetcherDecorator(dataFetcher, handlesSubscription, exceptionResolver);
122128
codeRegistry.dataFetcher(parent, fieldDefinition, dataFetcher);
123129
}
124130

0 commit comments

Comments
 (0)