Skip to content

Commit 7de0699

Browse files
committed
#2 - Singleton Responses incorrectly wrapped in a list
1 parent 42f6c01 commit 7de0699

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/main/java/com/arangodb/graphql/ArangoDataFetcher.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,13 @@ private ArangoTraversalQueryResult executeQuery(ArangoTraversalQuery query){
8989
return result;
9090
}
9191

92-
private List<Map> processResult(ArangoTraversalQueryResult result){
92+
private Object processResult(ArangoTraversalQueryResult result){
9393
logger.debug("Converting paths to GraphQL Query Response");
9494
Instant start = Instant.now();
95-
List<Map> convertedResult = result.getResult();
95+
Object convertedResult = result.getResult();
9696
Instant finish = Instant.now();
9797
long duration = Duration.between(start, finish).toMillis();
9898
logger.debug("Response conversion completed in {}ms", duration);
99-
logger.debug("GraphQL Query Response has {} elements", convertedResult.size());
10099
return convertedResult;
101100
}
102101

src/main/java/com/arangodb/graphql/query/result/ArangoTraversalQueryResult.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.arangodb.graphql.context.ArangoGraphQLContext;
2424
import com.arangodb.graphql.query.BaseDocumentPathEntity;
2525
import com.arangodb.graphql.query.result.resolver.ResultResolver;
26+
import graphql.schema.DataFetchingEnvironment;
27+
import graphql.schema.GraphQLTypeUtil;
2628

2729
import java.util.List;
2830
import java.util.Map;
@@ -71,8 +73,18 @@ public int size(){
7173
*
7274
* @return The result in the structure requested by the GraphQL Query
7375
*/
74-
public List<Map> getResult() {
75-
return result;
76+
public Object getResult() {
77+
DataFetchingEnvironment environment = ctx.getEnvironment();
78+
boolean isList = GraphQLTypeUtil.isList(environment.getFieldType());
79+
if(isList){
80+
return result;
81+
}
82+
else{
83+
if(result.size() == 0){
84+
return null;
85+
}
86+
return result.get(0);
87+
}
7688
}
7789

7890
}

src/test/java/com/arangodb/graphql/query/ArangoTraversalQueryExecutorTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.arangodb.ArangoDatabase;
2626
import com.arangodb.graphql.context.ArangoGraphQLContext;
2727
import com.arangodb.graphql.query.result.ArangoTraversalQueryResult;
28+
import graphql.schema.DataFetchingEnvironment;
29+
import graphql.schema.GraphQLOutputType;
2830
import org.junit.Before;
2931
import org.junit.Test;
3032
import org.junit.runner.RunWith;
@@ -35,6 +37,7 @@
3537
import java.util.Collections;
3638

3739
import static org.hamcrest.CoreMatchers.equalTo;
40+
import static org.hamcrest.CoreMatchers.nullValue;
3841
import static org.junit.Assert.*;
3942
import static org.mockito.ArgumentMatchers.any;
4043
import static org.mockito.Mockito.verify;
@@ -58,6 +61,12 @@ public class ArangoTraversalQueryExecutorTest {
5861
@Mock
5962
private ArangoGraphQLContext context;
6063

64+
@Mock
65+
private DataFetchingEnvironment dataFetchingEnvironment;
66+
67+
@Mock
68+
private GraphQLOutputType outputType;
69+
6170
private ArangoTraversalQueryExecutor executor;
6271

6372
@Before
@@ -75,13 +84,15 @@ public void execute() {
7584
when(arango.db("my_db")).thenReturn(database);
7685
when(database.query(any(), any(), any(), any())).thenReturn(cursor);
7786
when(cursor.asListRemaining()).thenReturn(Collections.EMPTY_LIST);
87+
when(context.getEnvironment()).thenReturn(dataFetchingEnvironment);
88+
when(dataFetchingEnvironment.getFieldType()).thenReturn(outputType);
7889

7990
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
8091

8192
ArangoTraversalQueryResult result = executor.execute(query);
8293

8394

84-
assertThat(result.getResult().size(), equalTo(0));
95+
assertThat(result.getResult(), nullValue());
8596

8697
verify(database).query(captor.capture(), any(), any(), any());
8798
assertThat(captor.getValue(), equalTo("RETURN 1"));

0 commit comments

Comments
 (0)