Skip to content

Commit

Permalink
🐛 Fix error on data fetching in GraphQL API when a nested field is no…
Browse files Browse the repository at this point in the history
…n nullable
  • Loading branch information
ujibang committed Dec 6, 2023
1 parent 1a78276 commit 5e36998
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.mongodb.client.AggregateIterable;

import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLList;


public class GQLAggregationDataFetcher extends GraphQLDataFetcher {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand All @@ -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;
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> {
protected static MongoClient mongoClient;
Expand All @@ -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;
}
}

0 comments on commit 5e36998

Please sign in to comment.