File tree Expand file tree Collapse file tree 2 files changed +51
-1
lines changed
main/java/graphql/execution
test/groovy/graphql/execution Expand file tree Collapse file tree 2 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -103,13 +103,18 @@ private CompletableFuture<ExecutionResult> executeSubscriptionEvent(ExecutionCon
103103 }
104104
105105 private ExecutionResult wrapWithRootFieldName (ExecutionStrategyParameters parameters , ExecutionResult executionResult ) {
106- String rootFieldName = parameters . field (). get ( 0 ). getName ( );
106+ String rootFieldName = getRootFieldName ( parameters );
107107 return new ExecutionResultImpl (
108108 singletonMap (rootFieldName , executionResult .getData ()),
109109 executionResult .getErrors ()
110110 );
111111 }
112112
113+ private String getRootFieldName (ExecutionStrategyParameters parameters ) {
114+ Field rootField = parameters .field ().get (0 );
115+ return rootField .getAlias () != null ? rootField .getAlias () : rootField .getName ();
116+ }
117+
113118 private ExecutionStrategyParameters firstFieldOfSubscriptionSelection (ExecutionStrategyParameters parameters ) {
114119 Map <String , List <Field >> fields = parameters .fields ();
115120 List <String > fieldNames = new ArrayList <>(fields .keySet ());
Original file line number Diff line number Diff line change @@ -98,6 +98,51 @@ class SubscriptionExecutionStrategyTest extends Specification {
9898
9999 }
100100
101+ @Unroll
102+ def " subscription alias is correctly used in response messages using '#why' implementation" () {
103+
104+ given :
105+ Publisher<Object > publisher = eventStreamPublisher
106+
107+ DataFetcher newMessageDF = new DataFetcher () {
108+ @Override
109+ Object get (DataFetchingEnvironment environment ) {
110+ assert environment. getArgument(" roomId" ) == 123
111+ return publisher
112+ }
113+ }
114+
115+ GraphQL graphQL = buildSubscriptionQL(newMessageDF)
116+
117+ def executionInput = ExecutionInput . newExecutionInput(). query("""
118+ subscription NewMessages {
119+ newsFeed: newMessage(roomId: 123) {
120+ sender
121+ text
122+ }
123+ }
124+ """ ). build()
125+
126+ def executionResult = graphQL. execute(executionInput)
127+
128+ when :
129+ Publisher<ExecutionResult > msgStream = executionResult. getData()
130+ def capturingSubscriber = new CapturingSubscriber<ExecutionResult > ()
131+ msgStream. subscribe(capturingSubscriber)
132+
133+ then :
134+ Awaitility . await(). untilTrue(capturingSubscriber. isDone())
135+
136+ def messages = capturingSubscriber. events
137+ messages. size() == 1
138+ messages[0 ]. data == [" newsFeed" : [sender : " sender0" , text : " text0" ]]
139+
140+ where :
141+ why | eventStreamPublisher
142+ ' reactive streams stream' | new ReactiveStreamsMessagePublisher (1 )
143+ ' rxjava stream' | new RxJavaMessagePublisher (1 )
144+ }
145+
101146
102147 @Unroll
103148 def " multiple subscribers can get messages on a subscription query using '#why' implementation " () {
You can’t perform that action at this time.
0 commit comments