diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java index 130c689f..0698e48d 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -132,7 +132,7 @@ abstract class AbstractGraphQlSourceBuilder> } private GraphQLSchema applyTypeVisitors(GraphQLSchema schema) { - GraphQLTypeVisitor visitor = ContextDataFetcherDecorator.createVisitor(this.subscriptionExceptionResolvers); + GraphQLTypeVisitor visitor = ContextDataFetcherDecorator.createVisitor(schema, this.subscriptionExceptionResolvers); List visitors = new ArrayList<>(this.typeVisitors); visitors.add(visitor); diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java index aacb18ce..65151064 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/ContextDataFetcherDecorator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,8 @@ import graphql.schema.DataFetchingEnvironment; import graphql.schema.GraphQLCodeRegistry; import graphql.schema.GraphQLFieldDefinition; import graphql.schema.GraphQLFieldsContainer; +import graphql.schema.GraphQLObjectType; +import graphql.schema.GraphQLSchema; import graphql.schema.GraphQLSchemaElement; import graphql.schema.GraphQLTypeVisitor; import graphql.schema.GraphQLTypeVisitorStub; @@ -97,9 +99,13 @@ final class ContextDataFetcherDecorator implements DataFetcher { * Static factory method to create {@link GraphQLTypeVisitor} that wraps * data fetchers with the {@link ContextDataFetcherDecorator}. */ - static GraphQLTypeVisitor createVisitor(List resolvers) { + static GraphQLTypeVisitor createVisitor( + GraphQLSchema schema, List resolvers) { - SubscriptionExceptionResolver compositeResolver = new CompositeSubscriptionExceptionResolver(resolvers); + GraphQLObjectType subscriptionType = schema.getSubscriptionType(); + String subscriptionTypeName = (subscriptionType != null ? subscriptionType.getName() : null); + + SubscriptionExceptionResolver exceptionResolver = new CompositeSubscriptionExceptionResolver(resolvers); return new GraphQLTypeVisitorStub() { @Override @@ -111,8 +117,8 @@ final class ContextDataFetcherDecorator implements DataFetcher { DataFetcher dataFetcher = codeRegistry.getDataFetcher(parent, fieldDefinition); if (applyDecorator(dataFetcher)) { - boolean handlesSubscription = parent.getName().equals("Subscription"); - dataFetcher = new ContextDataFetcherDecorator(dataFetcher, handlesSubscription, compositeResolver); + boolean handlesSubscription = parent.getName().equals(subscriptionTypeName); + dataFetcher = new ContextDataFetcherDecorator(dataFetcher, handlesSubscription, exceptionResolver); codeRegistry.dataFetcher(parent, fieldDefinition, dataFetcher); }