Skip to content

Commit

Permalink
Support custom "subscription" type name
Browse files Browse the repository at this point in the history
Closes gh-590
  • Loading branch information
rstoyanchev committed Mar 8, 2023
1 parent 9066c40 commit 3666112
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -113,7 +113,7 @@ public GraphQlSource build() {
protected abstract GraphQLSchema initGraphQlSchema();

private GraphQLSchema applyTypeVisitors(GraphQLSchema schema) {
GraphQLTypeVisitor visitor = ContextDataFetcherDecorator.createVisitor(this.subscriptionExceptionResolvers);
GraphQLTypeVisitor visitor = ContextDataFetcherDecorator.createVisitor(schema, this.subscriptionExceptionResolvers);
List<GraphQLTypeVisitor> visitors = new ArrayList<>(this.typeVisitors);
visitors.add(visitor);

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,6 +24,8 @@
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;
Expand Down Expand Up @@ -103,9 +105,13 @@ public Object get(DataFetchingEnvironment environment) throws Exception {
* Static factory method to create {@link GraphQLTypeVisitor} that wraps
* data fetchers with the {@link ContextDataFetcherDecorator}.
*/
static GraphQLTypeVisitor createVisitor(List<SubscriptionExceptionResolver> resolvers) {
static GraphQLTypeVisitor createVisitor(
GraphQLSchema schema, List<SubscriptionExceptionResolver> 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
Expand All @@ -117,8 +123,8 @@ public TraversalControl visitGraphQLFieldDefinition(
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);
}

Expand Down

0 comments on commit 3666112

Please sign in to comment.