diff --git a/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLAggregationDataFetcher.java b/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLAggregationDataFetcher.java index bb5e59888..3dedcacce 100644 --- a/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLAggregationDataFetcher.java +++ b/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLAggregationDataFetcher.java @@ -37,7 +37,6 @@ import com.mongodb.client.AggregateIterable; import graphql.schema.DataFetchingEnvironment; -import graphql.schema.GraphQLList; public class GQLAggregationDataFetcher extends GraphQLDataFetcher { @@ -99,9 +98,7 @@ public Object get(DataFetchingEnvironment env) throws Exception { .allowDiskUse(aggregation.getAllowDiskUse().getValue()) .maxTime(this.aggregationTimeLimit, TimeUnit.MILLISECONDS); - boolean isMultiple = env.getFieldDefinition().getType() instanceof GraphQLList; - - if (isMultiple) { + if (isList(env.getFieldDefinition().getType())) { var aggregationResult = new BsonArray(); res.into(aggregationResult.asArray()); return aggregationResult; diff --git a/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLBatchDataFetcher.java b/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLBatchDataFetcher.java index d9b487ef6..4b49fe0ed 100644 --- a/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLBatchDataFetcher.java +++ b/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLBatchDataFetcher.java @@ -20,13 +20,13 @@ */ package org.restheart.graphql.datafetchers; -import graphql.schema.DataFetchingEnvironment; -import graphql.schema.GraphQLList; -import graphql.schema.GraphQLObjectType; import org.bson.BsonValue; import org.dataloader.DataLoader; import org.restheart.graphql.models.QueryMapping; +import graphql.schema.DataFetchingEnvironment; +import graphql.schema.GraphQLObjectType; + public class GQLBatchDataFetcher extends GraphQLDataFetcher{ @@ -53,12 +53,10 @@ public Object get(DataFetchingEnvironment env) throws Exception { return dataLoader.load(int_args, env).thenApply( results -> { - boolean isMultiple = env.getFieldDefinition().getType() instanceof GraphQLList; - if (isMultiple){ + if (isList(env.getFieldDefinition().getType())) { return results; - } - else{ - return results.asArray().size() > 0 ? results.asArray().get(0) : null; + } else { + return !results.asArray().isEmpty() ? results.asArray().get(0) : null; } } ); diff --git a/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLQueryDataFetcher.java b/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLQueryDataFetcher.java index 7f3aec335..4f002cfcd 100644 --- a/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLQueryDataFetcher.java +++ b/graphql/src/main/java/org/restheart/graphql/datafetchers/GQLQueryDataFetcher.java @@ -29,7 +29,6 @@ import org.slf4j.LoggerFactory; import graphql.schema.DataFetchingEnvironment; -import graphql.schema.GraphQLList; public class GQLQueryDataFetcher extends GraphQLDataFetcher { private static final Logger LOGGER = LoggerFactory.getLogger(GQLQueryDataFetcher.class); @@ -76,9 +75,7 @@ public Object get(DataFetchingEnvironment env) throws Exception { query = query.limit(_limit); } - boolean isMultiple = env.getFieldDefinition().getType() instanceof GraphQLList; - - if (isMultiple) { + if (isList(env.getFieldDefinition().getType())) { var queryResult = new BsonArray(); query.into(queryResult.asArray()); return queryResult; diff --git a/graphql/src/main/java/org/restheart/graphql/datafetchers/GraphQLDataFetcher.java b/graphql/src/main/java/org/restheart/graphql/datafetchers/GraphQLDataFetcher.java index 7f3aace25..a333e4c9a 100644 --- a/graphql/src/main/java/org/restheart/graphql/datafetchers/GraphQLDataFetcher.java +++ b/graphql/src/main/java/org/restheart/graphql/datafetchers/GraphQLDataFetcher.java @@ -27,6 +27,9 @@ import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; +import graphql.schema.GraphQLList; +import graphql.schema.GraphQLNonNull; +import graphql.schema.GraphQLType; public abstract class GraphQLDataFetcher implements DataFetcher { protected static MongoClient mongoClient; @@ -52,4 +55,19 @@ protected void storeRootDoc(DataFetchingEnvironment env) { ctx.put("rootDoc", env.getSource()); } } + + /** + * check if type is a list also when the actual type is wrapped in GraphQLNonNull + * @param type + * @return true if the field type is a list + */ + protected boolean isList(GraphQLType type) { + if (type instanceof GraphQLList) { + return true; + } else if (type instanceof GraphQLNonNull nnt) { + return isList(nnt.getWrappedType()); + } + + return false; + } }