Skip to content

Defer support #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Deferred results should always be deferred and be sent second
  • Loading branch information
JacobMountain committed Aug 7, 2019
commit afc4c49a19f3dd6979a04cb61ba78bf8426f1729
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ private static class StaticDataPublisher<T> extends SingleSubscriberPublisher<T>
StaticDataPublisher(T data) {
super();
super.offer(data);
super.noMoreData();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1054,9 +1054,39 @@ class AbstractGraphQLHttpServletSpec extends Specification {

def "defer query over HTTP POST"() {
setup:
servlet = TestUtils.createDefaultServlet()
request.setContent('{"query": "subscription Subscription($arg: String!) { echo(arg: $arg) }", "operationName": "Subscription", "variables": {"arg": "test"}}'.bytes)
request.setAsyncSupported(true)

when:
servlet.doPost(request, response)
then:
response.getStatus() == STATUS_OK
response.getContentType() == CONTENT_TYPE_SERVER_SENT_EVENTS

when:
subscriptionLatch.await(1, TimeUnit.SECONDS)
then:
getSubscriptionResponseContent()[0].data.echo == "First\n\ntest"
getSubscriptionResponseContent()[1].data.echo == "Second\n\ntest"
}

def "deferred query that takes longer than initial results, should still be sent second"() {
setup:
servlet = TestUtils.createDefaultServlet({ env ->
if (env.getField().name == "a") {
Thread.sleep(1000)
}
env.arguments.arg
})
request.setContent(mapper.writeValueAsBytes([
query: 'query { echo(arg:"test") @defer }'
query: '''
{
object {
a(arg: "Hello")
b(arg: "World") @defer
}
}
'''
]))
request.setAsyncSupported(true)

Expand All @@ -1066,15 +1096,15 @@ class AbstractGraphQLHttpServletSpec extends Specification {
then:
response.getStatus() == STATUS_OK
response.getContentType() == CONTENT_TYPE_SERVER_SENT_EVENTS
getSubscriptionResponseContent()[0].data.echo == null
getSubscriptionResponseContent()[0].data.object.a == "Hello" // a has a Thread.sleep

when:
subscriptionLatch.await(1, TimeUnit.SECONDS)

then:
def content = getSubscriptionResponseContent()
content[1].data == "test"
content[1].path == ["echo"]
content[1].data == "World"
content[1].path == ["object", "b"]
}

def "errors before graphql schema execution return internal server error"() {
Expand Down
26 changes: 26 additions & 0 deletions src/test/groovy/graphql/servlet/TestUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ class TestUtils {
}
field.dataFetcher(queryDataFetcher)
}
.field { GraphQLFieldDefinition.Builder field ->
field.name("object")
field.type(
GraphQLObjectType.newObject()
.name("NestedObject")
.field { nested ->
nested.name("a")
nested.type(Scalars.GraphQLString)
nested.argument { argument ->
argument.name("arg")
argument.type(Scalars.GraphQLString)
}
nested.dataFetcher(queryDataFetcher)
}
.field { nested ->
nested.name("b")
nested.type(Scalars.GraphQLString)
nested.argument { argument ->
argument.name("arg")
argument.type(Scalars.GraphQLString)
}
nested.dataFetcher(queryDataFetcher)
}
)
field.dataFetcher(new StaticDataFetcher([:]))
}
.field { GraphQLFieldDefinition.Builder field ->
field.name("returnsNullIncorrectly")
field.type(new GraphQLNonNull(Scalars.GraphQLString))
Expand Down