graphql.language.VariableReference Java Examples
The following examples show how to use
graphql.language.VariableReference.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Generator.java From smallrye-graphql with Apache License 2.0 | 5 votes |
private Type<?> type(Argument argument) { Value<?> value = argument.getValue(); if (value instanceof VariableReference) return resolve(((VariableReference) value).getName()); throw new GraphQlGeneratorException( "unsupported type " + value + " for argument '" + argument.getName() + "'"); }
Example #2
Source File: JsonCoercingUtil.java From stream-registry with Apache License 2.0 | 5 votes |
public static Object parseLiteral(Object input, Map<String, Object> variables) throws CoercingParseLiteralException { if (!(input instanceof Value)) { log.error("Expected 'Value', got: {}", input); throw new CoercingParseLiteralException("Expected 'Value', got: " + input); } Object result = null; if (input instanceof StringValue) { result = ((StringValue) input).getValue(); } else if (input instanceof IntValue) { result = ((IntValue) input).getValue(); } else if (input instanceof FloatValue) { result = ((FloatValue) input).getValue(); } else if (input instanceof BooleanValue) { result = ((BooleanValue) input).isValue(); } else if (input instanceof EnumValue) { result = ((EnumValue) input).getName(); } else if (input instanceof VariableReference) { result = variables.get(((VariableReference) input).getName()); } else if (input instanceof ArrayValue) { result = ((ArrayValue) input).getValues().stream() .map(v -> parseLiteral(v, variables)) .collect(toList()); } else if (input instanceof ObjectValue) { result = ((ObjectValue) input).getObjectFields().stream() .collect(toMap(ObjectField::getName, f -> parseLiteral(f.getValue(), variables))); } return result; }
Example #3
Source File: JacksonObjectScalars.java From graphql-spqr with Apache License 2.0 | 5 votes |
private static JsonNode parseJsonValue(Value value, Map<String, Object> variables) { if (value instanceof BooleanValue) { return JsonNodeFactory.instance.booleanNode(((BooleanValue) value).isValue()); } if (value instanceof EnumValue) { return JsonNodeFactory.instance.textNode(((EnumValue) value).getName()); } if (value instanceof FloatValue) { return JsonNodeFactory.instance.numberNode(((FloatValue) value).getValue()); } if (value instanceof IntValue) { return JsonNodeFactory.instance.numberNode(((IntValue) value).getValue()); } if (value instanceof NullValue) { return JsonNodeFactory.instance.nullNode(); } if (value instanceof StringValue) { return JsonNodeFactory.instance.textNode(((StringValue) value).getValue()); } if (value instanceof ArrayValue) { List<Value> values = ((ArrayValue) value).getValues(); ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode(values.size()); values.forEach(v -> jsonArray.add(parseJsonValue(v, variables))); return jsonArray; } if (value instanceof VariableReference) { return OBJECT_MAPPER.convertValue(variables.get(((VariableReference) value).getName()), JsonNode.class); } if (value instanceof ObjectValue) { final ObjectNode result = JsonNodeFactory.instance.objectNode(); ((ObjectValue) value).getObjectFields().forEach(objectField -> result.set(objectField.getName(), parseJsonValue(objectField.getValue(), variables))); return result; } //Should never happen throw new CoercingParseLiteralException("Unknown scalar AST type: " + value.getClass().getName()); }
Example #4
Source File: ContentTypeBasedDataFetcher.java From engine with GNU General Public License v3.0 | 5 votes |
protected void addFieldFilterFromObjectField(String path, ObjectField filter, BoolQueryBuilder query, DataFetchingEnvironment env) { if (filter.getValue() instanceof ArrayValue) { ArrayValue actualFilters = (ArrayValue) filter.getValue(); switch (filter.getName()) { case ARG_NAME_NOT: BoolQueryBuilder notQuery = boolQuery(); actualFilters.getValues() .forEach(notFilter -> ((ObjectValue) notFilter).getObjectFields() .forEach(notField -> addFieldFilterFromObjectField(path, notField, notQuery, env))); notQuery.filter().forEach(query::mustNot); break; case ARG_NAME_AND: actualFilters.getValues() .forEach(andFilter -> ((ObjectValue) andFilter).getObjectFields() .forEach(andField -> addFieldFilterFromObjectField(path, andField, query, env))); break; case ARG_NAME_OR: BoolQueryBuilder tempQuery = boolQuery(); BoolQueryBuilder orQuery = boolQuery(); actualFilters.getValues().forEach(orFilter -> ((ObjectValue) orFilter).getObjectFields() .forEach(orField -> addFieldFilterFromObjectField(path, orField, tempQuery, env))); tempQuery.filter().forEach(orQuery::should); query.filter(boolQuery().must(orQuery)); break; default: // never happens } } else if (!(filter.getValue() instanceof VariableReference) || env.getVariables().containsKey(((VariableReference)filter.getValue()).getName())) { query.filter(getFilterQueryFromObjectField(path, filter, env)); } }
Example #5
Source File: ContentTypeBasedDataFetcher.java From engine with GNU General Public License v3.0 | 5 votes |
/** * Extracts a scalar value, this is needed because of GraphQL strict types */ protected Object getRealValue(Value value, DataFetchingEnvironment env) { if (value instanceof BooleanValue) { return ((BooleanValue)value).isValue(); } else if (value instanceof FloatValue) { return ((FloatValue) value).getValue(); } else if (value instanceof IntValue) { return ((IntValue) value).getValue(); } else if (value instanceof StringValue) { return ((StringValue) value).getValue(); } else if (value instanceof VariableReference) { return env.getVariables().get(((VariableReference) value).getName()); } return null; }
Example #6
Source File: JsonCoercingUtilTest.java From stream-registry with Apache License 2.0 | 4 votes |
@Test public void variableReferenceValue() { Value value = VariableReference.newVariableReference().name("a").build(); Object result = JsonCoercingUtil.parseLiteral(value, Map.of("a", "b")); assertThat(result, is("b")); }
Example #7
Source File: ContentTypeBasedDataFetcher.java From engine with GNU General Public License v3.0 | 4 votes |
/** * 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)); } }