-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for include and skip directive with references
- Loading branch information
1 parent
1409d7f
commit f386387
Showing
4 changed files
with
396 additions
and
18 deletions.
There are no files selected for viewing
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
42 changes: 42 additions & 0 deletions
42
src/main/java/com/intuit/graphql/orchestrator/utils/QueryDirectivesUtil.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,42 @@ | ||
package com.intuit.graphql.orchestrator.utils; | ||
|
||
import graphql.language.Argument; | ||
import graphql.language.BooleanValue; | ||
import graphql.language.Directive; | ||
import graphql.language.Field; | ||
import graphql.language.Value; | ||
import graphql.language.VariableReference; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class QueryDirectivesUtil { | ||
|
||
public static boolean shouldIgnoreNode(Field node, Map<String, Object> queryVariables) { | ||
Optional<Directive> optionalIncludesDir = node.getDirectives("include").stream().findFirst(); | ||
Optional<Directive> optionalSkipDir = node.getDirectives("skip").stream().findFirst(); | ||
if(optionalIncludesDir.isPresent() || optionalSkipDir.isPresent()) { | ||
if(optionalIncludesDir.isPresent() && (!getIfValue(optionalIncludesDir.get(), queryVariables))) { | ||
return true; | ||
} | ||
return optionalSkipDir.isPresent() && (getIfValue(optionalSkipDir.get(), queryVariables)); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static boolean getIfValue(Directive directive, Map<String, Object> queryVariables){ | ||
Argument ifArg = directive.getArgument("if"); | ||
Value ifValue = ifArg.getValue(); | ||
|
||
boolean defaultValue = directive.getName().equals("skip"); | ||
|
||
if(ifValue instanceof VariableReference) { | ||
String variableRefName = ((VariableReference) ifValue).getName(); | ||
return (boolean) queryVariables.getOrDefault(variableRefName, defaultValue); | ||
} else if(ifValue instanceof BooleanValue) { | ||
return ((BooleanValue) ifValue).isValue(); | ||
} | ||
return false; | ||
} | ||
} |
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
Oops, something went wrong.