Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced two differents GraphQLDataFetcher (SingleDataFetcher, Mult…
…ipleDataFetcher). Implemented a method to interpolate arguments of mongodb methods (e.g. find(), limit(), skip() etc.) with GraphQL query arguments. Code cleanup.
- Loading branch information
Showing
7 changed files
with
325 additions
and
167 deletions.
There are no files selected for viewing
115 changes: 0 additions & 115 deletions
115
graphql/src/main/java/org/restheart/graphql/GraphQLDataFetcher.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
graphql/src/main/java/org/restheart/graphql/MultipleGraphQLDataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.restheart.graphql; | ||
|
||
import com.mongodb.MongoClient; | ||
import com.mongodb.client.FindIterable; | ||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import graphql.schema.SelectedField; | ||
import org.restheart.exchange.InvalidMetadataException; | ||
import org.restheart.exchange.QueryVariableNotBoundException; | ||
import org.bson.Document; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.mongodb.client.model.Projections.fields; | ||
import static com.mongodb.client.model.Projections.include; | ||
|
||
public class MultipleGraphQLDataFetcher implements DataFetcher<List<Document>> { | ||
|
||
private static MultipleGraphQLDataFetcher instance = null; | ||
|
||
public static MultipleGraphQLDataFetcher getInstance(){ | ||
if (instance ==null){ | ||
instance = new MultipleGraphQLDataFetcher(); | ||
} | ||
return instance; | ||
} | ||
private MultipleGraphQLDataFetcher() { } | ||
|
||
|
||
private static GraphQLApp currentApp = null; | ||
private static MongoClient mongoClient = null; | ||
|
||
|
||
public static GraphQLApp getCurrentApp() { | ||
return currentApp; | ||
} | ||
|
||
public static void setCurrentApp(GraphQLApp app) { | ||
currentApp = app; | ||
} | ||
|
||
public static MongoClient getMongoClient() { | ||
return mongoClient; | ||
} | ||
|
||
public static void setMongoClient(MongoClient mclient) { | ||
mongoClient = mclient; | ||
} | ||
|
||
@Override | ||
public List<Document> get(DataFetchingEnvironment dataFetchingEnvironment) throws Exception { | ||
String queryName = dataFetchingEnvironment.getField().getName(); | ||
try{ | ||
Query queryDef = currentApp.getQueryByName(queryName); | ||
Map<String, Object> graphQLQueryArguments = dataFetchingEnvironment.getArguments(); | ||
|
||
Map<String, Document> interpolatedArguments = queryDef.interpolate(graphQLQueryArguments); | ||
|
||
FindIterable<Document> query; | ||
|
||
if (interpolatedArguments.containsKey("filter")){ | ||
query = mongoClient.getDatabase(queryDef.getDb()) | ||
.getCollection(queryDef.getCollection(), Document.class) | ||
.find(interpolatedArguments.get("filter")); | ||
} | ||
else{ | ||
query = mongoClient.getDatabase(queryDef.getDb()) | ||
.getCollection(queryDef.getCollection(), Document.class) | ||
.find(); | ||
} | ||
|
||
if (interpolatedArguments.containsKey("sort")){ | ||
query = query.sort(interpolatedArguments.get("sort")); | ||
} | ||
|
||
if (interpolatedArguments.containsKey("skip")){ | ||
query = query.skip((int) interpolatedArguments.get("skip").get("skip")); | ||
} | ||
|
||
if (interpolatedArguments.containsKey("limit")){ | ||
query = query.limit((int) interpolatedArguments.get("limit").get("limit")); | ||
} | ||
|
||
// find projection fields | ||
List<String> projField = new LinkedList<String>(); | ||
for (SelectedField field: dataFetchingEnvironment.getSelectionSet().getFields()) { | ||
projField.add(field.getQualifiedName()); | ||
} | ||
ArrayList<Document> result = new ArrayList<Document>(); | ||
|
||
query.projection(fields(include(projField))).into(result); | ||
|
||
return result; | ||
|
||
}catch (NullPointerException e){ | ||
System.out.println("No query found with name "+ queryName ); | ||
return null; | ||
}catch (InvalidMetadataException | QueryVariableNotBoundException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.