use of graphql.language.VariableReference in project graphql-java by graphql-java.
the class GraphqlAntlrToLanguage method getValue.
private Value getValue(GraphqlParser.ValueWithVariableContext ctx) {
if (ctx.IntValue() != null) {
IntValue intValue = new IntValue(new BigInteger(ctx.IntValue().getText()));
newNode(intValue, ctx);
return intValue;
} else if (ctx.FloatValue() != null) {
FloatValue floatValue = new FloatValue(new BigDecimal(ctx.FloatValue().getText()));
newNode(floatValue, ctx);
return floatValue;
} else if (ctx.BooleanValue() != null) {
BooleanValue booleanValue = new BooleanValue(Boolean.parseBoolean(ctx.BooleanValue().getText()));
newNode(booleanValue, ctx);
return booleanValue;
} else if (ctx.NullValue() != null) {
newNode(Null, ctx);
return Null;
} else if (ctx.stringValue() != null) {
StringValue stringValue = new StringValue(quotedString(ctx.stringValue()));
newNode(stringValue, ctx);
return stringValue;
} else if (ctx.enumValue() != null) {
EnumValue enumValue = new EnumValue(ctx.enumValue().getText());
newNode(enumValue, ctx);
return enumValue;
} else if (ctx.arrayValueWithVariable() != null) {
ArrayValue arrayValue = new ArrayValue();
newNode(arrayValue, ctx);
for (GraphqlParser.ValueWithVariableContext valueWithVariableContext : ctx.arrayValueWithVariable().valueWithVariable()) {
arrayValue.getValues().add(getValue(valueWithVariableContext));
}
return arrayValue;
} else if (ctx.objectValueWithVariable() != null) {
ObjectValue objectValue = new ObjectValue();
newNode(objectValue, ctx);
for (GraphqlParser.ObjectFieldWithVariableContext objectFieldWithVariableContext : ctx.objectValueWithVariable().objectFieldWithVariable()) {
ObjectField objectField = new ObjectField(objectFieldWithVariableContext.name().getText(), getValue(objectFieldWithVariableContext.valueWithVariable()));
objectValue.getObjectFields().add(objectField);
}
return objectValue;
} else if (ctx.variable() != null) {
VariableReference variableReference = new VariableReference(ctx.variable().name().getText());
newNode(variableReference, ctx);
return variableReference;
}
return Assert.assertShouldNeverHappen();
}
use of graphql.language.VariableReference in project graphql-java by graphql-java.
the class ValuesResolver method coerceValueAstForInputObject.
private Object coerceValueAstForInputObject(GraphqlFieldVisibility fieldVisibility, GraphQLInputObjectType type, ObjectValue inputValue, Map<String, Object> variables) {
Map<String, Object> result = new LinkedHashMap<>();
Map<String, ObjectField> inputValueFieldsByName = mapObjectValueFieldsByName(inputValue);
List<GraphQLInputObjectField> inputFields = fieldVisibility.getFieldDefinitions(type);
for (GraphQLInputObjectField inputTypeField : inputFields) {
if (inputValueFieldsByName.containsKey(inputTypeField.getName())) {
boolean putObjectInMap = true;
ObjectField field = inputValueFieldsByName.get(inputTypeField.getName());
Value fieldInputValue = field.getValue();
Object fieldObject = null;
if (fieldInputValue instanceof VariableReference) {
String varName = ((VariableReference) fieldInputValue).getName();
if (!variables.containsKey(varName)) {
putObjectInMap = false;
} else {
fieldObject = variables.get(varName);
}
} else {
fieldObject = coerceValueAst(fieldVisibility, inputTypeField.getType(), fieldInputValue, variables);
}
if (fieldObject == null) {
if (!field.getValue().isEqualTo(NullValue.Null)) {
fieldObject = inputTypeField.getDefaultValue();
}
}
if (putObjectInMap) {
result.put(field.getName(), fieldObject);
} else {
assertNonNullInputField(inputTypeField);
}
} else if (inputTypeField.getDefaultValue() != null) {
result.put(inputTypeField.getName(), inputTypeField.getDefaultValue());
} else {
assertNonNullInputField(inputTypeField);
}
}
return result;
}
use of graphql.language.VariableReference in project engine by craftercms.
the class ContentTypeBasedDataFetcher method addFieldFilterFromObjectField.
protected void addFieldFilterFromObjectField(String path, ObjectField filter, BoolQueryBuilder query, DataFetchingEnvironment env) {
boolean isVariable = filter.getValue() instanceof VariableReference;
switch(filter.getName()) {
case ARG_NAME_NOT:
BoolQueryBuilder notQuery = boolQuery();
if (isVariable) {
((List<Map<String, Object>>) env.getVariables().get(((VariableReference) filter.getValue()).getName())).forEach(notFilter -> notFilter.entrySet().forEach(entry -> addFieldFilterFromMapEntry(path, entry, notQuery, env)));
} else {
((ArrayValue) filter.getValue()).getValues().forEach(notFilter -> ((ObjectValue) notFilter).getObjectFields().forEach(notField -> addFieldFilterFromObjectField(path, notField, notQuery, env)));
}
if (!notQuery.filter().isEmpty()) {
notQuery.filter().forEach(query::mustNot);
}
break;
case ARG_NAME_AND:
if (isVariable) {
((List<Map<String, Object>>) env.getVariables().get(((VariableReference) filter.getValue()).getName())).forEach(andFilter -> andFilter.entrySet().forEach(entry -> addFieldFilterFromMapEntry(path, entry, query, env)));
} else {
((ArrayValue) filter.getValue()).getValues().forEach(andFilter -> ((ObjectValue) andFilter).getObjectFields().forEach(andField -> addFieldFilterFromObjectField(path, andField, query, env)));
}
break;
case ARG_NAME_OR:
BoolQueryBuilder tempQuery = boolQuery();
if (isVariable) {
((List<Map<String, Object>>) env.getVariables().get(((VariableReference) filter.getValue()).getName())).forEach(orFilter -> orFilter.entrySet().forEach(entry -> addFieldFilterFromMapEntry(path, entry, tempQuery, env)));
} else {
((ArrayValue) filter.getValue()).getValues().forEach(orFilter -> ((ObjectValue) orFilter).getObjectFields().forEach(orField -> addFieldFilterFromObjectField(path, orField, tempQuery, env)));
}
if (!tempQuery.filter().isEmpty()) {
BoolQueryBuilder orQuery = boolQuery();
tempQuery.filter().forEach(orQuery::should);
query.filter(boolQuery().must(orQuery));
}
break;
default:
QueryBuilder builder = getFilterQueryFromObjectField(path, filter, env);
if (builder != null) {
query.filter(builder);
}
}
}
use of graphql.language.VariableReference in project engine by craftercms.
the class ContentTypeBasedDataFetcher method processSelection.
/**
* Adds the required filters to the ES query for the given field
*/
protected void processSelection(String path, Selection currentSelection, BoolQueryBuilder query, List<String> queryFieldIncludes, DataFetchingEnvironment env) {
if (currentSelection instanceof Field) {
// If the current selection is a field
Field currentField = (Field) currentSelection;
// Get the original field name
String propertyName = getOriginalName(currentField.getName());
// Build the ES-friendly path
String fullPath = StringUtils.isEmpty(path) ? propertyName : path + "." + propertyName;
// If the field has sub selection
if (Objects.nonNull(currentField.getSelectionSet())) {
// If the field is a flattened component
if (fullPath.matches(COMPONENT_INCLUDE_REGEX)) {
// Include the 'content-type' field to make sure the type can be resolved during runtime
String contentTypeFieldPath = fullPath + "." + QUERY_FIELD_NAME_CONTENT_TYPE;
if (!queryFieldIncludes.contains(contentTypeFieldPath)) {
queryFieldIncludes.add(contentTypeFieldPath);
}
}
// Process recursively and finish
currentField.getSelectionSet().getSelections().forEach(selection -> processSelection(fullPath, selection, query, queryFieldIncludes, env));
return;
}
// Add the field to the list
logger.debug("Adding selected field '{}' to query", fullPath);
queryFieldIncludes.add(fullPath);
// Check the filters to build the ES query
Optional<Argument> arg = currentField.getArguments().stream().filter(a -> a.getName().equals(FILTER_NAME)).findFirst();
if (arg.isPresent()) {
logger.debug("Adding filters for field {}", fullPath);
Value<?> argValue = arg.get().getValue();
if (argValue instanceof ObjectValue) {
List<ObjectField> filters = ((ObjectValue) argValue).getObjectFields();
filters.forEach((filter) -> addFieldFilterFromObjectField(fullPath, filter, query, env));
} else if (argValue instanceof VariableReference && env.getVariables().containsKey(((VariableReference) argValue).getName())) {
Map<String, Object> map = (Map<String, Object>) env.getVariables().get(((VariableReference) argValue).getName());
map.entrySet().forEach(filter -> addFieldFilterFromMapEntry(fullPath, filter, query, env));
}
}
} else if (currentSelection instanceof InlineFragment) {
// If the current selection is an inline fragment, process recursively
InlineFragment fragment = (InlineFragment) currentSelection;
fragment.getSelectionSet().getSelections().forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env));
} else if (currentSelection instanceof FragmentSpread) {
// If the current selection is a fragment spread, find the fragment and process recursively
FragmentSpread fragmentSpread = (FragmentSpread) currentSelection;
FragmentDefinition fragmentDefinition = env.getFragmentsByName().get(fragmentSpread.getName());
fragmentDefinition.getSelectionSet().getSelections().forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env));
}
}
Aggregations