Skip to content

Commit 110813b

Browse files
committed
Test cleanup - no need for its own field
1 parent cf8a3c3 commit 110813b

File tree

3 files changed

+17
-40
lines changed

3 files changed

+17
-40
lines changed

src/main/java/graphql/servlet/AbstractGraphQLHttpServlet.java

+11-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.common.io.ByteStreams;
44
import com.google.common.io.CharStreams;
5-
import graphql.DeferredExecutionResult;
65
import graphql.ExecutionResult;
76
import graphql.GraphQL;
87
import graphql.execution.reactive.SingleSubscriberPublisher;
@@ -379,15 +378,16 @@ private void query(GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper graphQL
379378
AtomicReference<Subscription> subscriptionRef = new AtomicReference<>();
380379
asyncContext.addListener(new SubscriptionAsyncListener(subscriptionRef));
381380
ExecutionResultSubscriber subscriber = new ExecutionResultSubscriber(subscriptionRef, asyncContext, graphQLObjectMapper);
382-
Publisher<ExecutionResult> publisher;
381+
List<Publisher<ExecutionResult>> publishers = new ArrayList<>();
383382
if (result.getData() instanceof Publisher) {
384-
publisher = result.getData();
383+
publishers.add(result.getData());
385384
} else {
386-
publisher = new SingleSubscriberPublisher<>();
387-
((SingleSubscriberPublisher<ExecutionResult>) publisher).offer(result);
388-
publisher = new MultiPublisher<>(publisher, (Publisher<ExecutionResult>) result.getExtensions().get(GraphQL.DEFERRED_RESULTS));
385+
publishers.add(new StaticDataPublisher<>(result));
386+
final Publisher<ExecutionResult> deferredResultsPublisher = (Publisher<ExecutionResult>) result.getExtensions().get(GraphQL.DEFERRED_RESULTS);
387+
publishers.add(deferredResultsPublisher);
389388
}
390-
publisher.subscribe(subscriber);
389+
publishers.forEach(it -> it.subscribe(subscriber));
390+
391391
if (isInAsyncThread) {
392392
// We need to delay the completion of async context until after the subscription has terminated, otherwise the AsyncContext is prematurely closed.
393393
try {
@@ -581,20 +581,11 @@ public void await() throws InterruptedException {
581581
}
582582
}
583583

584-
private static class MultiPublisher<T> implements Publisher<T> {
585-
586-
private List<Publisher<T>> publishers;
587-
588-
@SafeVarargs
589-
MultiPublisher(Publisher<T>... publishers) {
590-
this.publishers = Arrays.asList(publishers);
584+
private static class StaticDataPublisher<T> extends SingleSubscriberPublisher<T> implements Publisher<T> {
585+
StaticDataPublisher(T data) {
586+
super();
587+
super.offer(data);
591588
}
592-
593-
@Override
594-
public void subscribe(Subscriber<? super T> s) {
595-
publishers.forEach(publisher -> publisher.subscribe(s));
596-
}
597-
598589
}
599590

600591
}

src/test/groovy/graphql/servlet/AbstractGraphQLHttpServletSpec.groovy

+6-6
Original file line numberDiff line numberDiff line change
@@ -286,23 +286,23 @@ class AbstractGraphQLHttpServletSpec extends Specification {
286286

287287
def "deferred query over HTTP GET"() {
288288
setup:
289-
request.addParameter('query', 'query { deferred(arg:"test") @defer }')
289+
request.addParameter('query', 'query { echo(arg:"test") @defer }')
290290

291291
when:
292292
servlet.doGet(request, response)
293293

294294
then:
295295
response.getStatus() == STATUS_OK
296296
response.getContentType() == CONTENT_TYPE_SERVER_SENT_EVENTS
297-
getSubscriptionResponseContent()[0].data.deferred == null
297+
getSubscriptionResponseContent()[0].data.echo == null
298298

299299
when:
300300
subscriptionLatch.await(1, TimeUnit.SECONDS)
301301

302302
then:
303303
def content = getSubscriptionResponseContent()
304304
content[1].data == "test"
305-
content[1].path == ["deferred"]
305+
content[1].path == ["echo"]
306306
}
307307

308308
def "Batch Execution Handler allows limiting batches and sending error messages."() {
@@ -1056,7 +1056,7 @@ class AbstractGraphQLHttpServletSpec extends Specification {
10561056
setup:
10571057
servlet = TestUtils.createDefaultServlet()
10581058
request.setContent(mapper.writeValueAsBytes([
1059-
query: 'query { deferred(arg:"test") @defer }'
1059+
query: 'query { echo(arg:"test") @defer }'
10601060
]))
10611061
request.setAsyncSupported(true)
10621062

@@ -1066,15 +1066,15 @@ class AbstractGraphQLHttpServletSpec extends Specification {
10661066
then:
10671067
response.getStatus() == STATUS_OK
10681068
response.getContentType() == CONTENT_TYPE_SERVER_SENT_EVENTS
1069-
getSubscriptionResponseContent()[0].data.deferred == null
1069+
getSubscriptionResponseContent()[0].data.echo == null
10701070

10711071
when:
10721072
subscriptionLatch.await(1, TimeUnit.SECONDS)
10731073

10741074
then:
10751075
def content = getSubscriptionResponseContent()
10761076
content[1].data == "test"
1077-
content[1].path == ["deferred"]
1077+
content[1].path == ["echo"]
10781078
}
10791079

10801080
def "errors before graphql schema execution return internal server error"() {

src/test/groovy/graphql/servlet/TestUtils.groovy

-14
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,6 @@ class TestUtils {
120120
field.type(new GraphQLNonNull(Scalars.GraphQLString))
121121
field.dataFetcher({ env -> null })
122122
}
123-
.field { GraphQLFieldDefinition.Builder field ->
124-
field.name("deferred")
125-
field.type(Scalars.GraphQLString)
126-
field.argument { argument ->
127-
argument.name("arg")
128-
argument.type(Scalars.GraphQLString)
129-
}
130-
field.dataFetcher({ env ->
131-
return CompletableFuture.supplyAsync( {
132-
Thread.sleep(100)
133-
env.arguments.arg
134-
})
135-
})
136-
}
137123
.build()
138124

139125
GraphQLObjectType mutation = GraphQLObjectType.newObject()

0 commit comments

Comments
 (0)