graphql.language.ObjectValue Java Examples
The following examples show how to use
graphql.language.ObjectValue.
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: _Any.java From federation-jvm with MIT License | 5 votes |
@Nullable @Override public Object parseLiteral(Object input) throws CoercingParseLiteralException { if (input instanceof NullValue) { return null; } else if (input instanceof FloatValue) { return ((FloatValue) input).getValue(); } else if (input instanceof StringValue) { return ((StringValue) input).getValue(); } else if (input instanceof IntValue) { return ((IntValue) input).getValue(); } else if (input instanceof BooleanValue) { return ((BooleanValue) input).isValue(); } else if (input instanceof EnumValue) { return ((EnumValue) input).getName(); } else if (input instanceof ArrayValue) { return ((ArrayValue) input).getValues() .stream() .map(this::parseLiteral) .collect(Collectors.toList()); } else if (input instanceof ObjectValue) { return ((ObjectValue) input).getObjectFields() .stream() .collect(Collectors.toMap(ObjectField::getName, f -> parseLiteral(f.getValue()))); } return Assert.assertShouldNeverHappen(); }
Example #2
Source File: ObjectNodeCoercing.java From stream-registry with Apache License 2.0 | 5 votes |
@Override public Object parseLiteral(Object input, Map<String, Object> variables) throws CoercingParseLiteralException { if (!(input instanceof ObjectValue)) { log.error("Expected 'ObjectValue', got: {}", input); throw new CoercingParseLiteralException("Expected 'ObjectValue', got: " + input); } return JsonCoercingUtil.parseLiteral(input, variables); }
Example #3
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 #4
Source File: ObjectNodeCoercingTest.java From stream-registry with Apache License 2.0 | 5 votes |
@Test public void parseLiteral() { Value input = ObjectValue.newObjectValue() .objectField(ObjectField.newObjectField() .name("a") .value(StringValue.newStringValue("b").build()) .build()) .build(); ObjectNodeCoercing spy = Mockito.spy(underTest); Object parsed = new Object(); when(spy.parseLiteral(input, emptyMap())).thenReturn(parsed); Object result = spy.parseLiteral(input); assertThat(result, is(sameInstance(parsed))); }
Example #5
Source File: JsonCoercingUtilTest.java From stream-registry with Apache License 2.0 | 5 votes |
@Test public void objectValue() { Value value = ObjectValue.newObjectValue() .objectField(ObjectField.newObjectField() .name("a") .value(StringValue.newStringValue("b").build()) .build()) .build(); Object result = JsonCoercingUtil.parseLiteral(value, emptyMap()); assertThat(result, is(Map.of("a", "b"))); }
Example #6
Source File: GsonScalars.java From graphql-spqr with Apache License 2.0 | 5 votes |
@Override public JsonElement parseLiteral(Object input) { if (input instanceof ObjectValue || input instanceof ArrayValue) { throw literalParsingException(input, StringValue.class, BooleanValue.class, EnumValue.class, FloatValue.class, IntValue.class, NullValue.class); } return parseJsonValue(((Value) input)); }
Example #7
Source File: GsonScalars.java From graphql-spqr with Apache License 2.0 | 5 votes |
private static JsonElement parseJsonValue(Value value) { if (value instanceof BooleanValue) { return new JsonPrimitive(((BooleanValue) value).isValue()); } if (value instanceof EnumValue) { return new JsonPrimitive(((EnumValue) value).getName()); } if (value instanceof FloatValue) { return new JsonPrimitive(((FloatValue) value).getValue()); } if (value instanceof IntValue) { return new JsonPrimitive(((IntValue) value).getValue()); } if (value instanceof NullValue) { return JsonNull.INSTANCE; } if (value instanceof StringValue) { return new JsonPrimitive(((StringValue) value).getValue()); } if (value instanceof ArrayValue) { List<Value> values = ((ArrayValue) value).getValues(); JsonArray jsonArray = new JsonArray(values.size()); values.forEach(v -> jsonArray.add(parseJsonValue(v))); return jsonArray; } if (value instanceof ObjectValue) { final JsonObject result = new JsonObject(); ((ObjectValue) value).getObjectFields().forEach(objectField -> result.add(objectField.getName(), parseJsonValue(objectField.getValue()))); return result; } //Should never happen, as it would mean the variable was not replaced by the parser throw new CoercingParseLiteralException("Unknown scalar AST type: " + value.getClass().getName()); }
Example #8
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 #9
Source File: ExtendedJpaDataFetcher.java From graphql-jpa with MIT License | 5 votes |
private PageInformation extractPageInformation(DataFetchingEnvironment environment, Field field) { Optional<Argument> paginationRequest = field.getArguments().stream().filter(it -> GraphQLSchemaBuilder.PAGINATION_REQUEST_PARAM_NAME.equals(it.getName())).findFirst(); if (paginationRequest.isPresent()) { field.getArguments().remove(paginationRequest.get()); ObjectValue paginationValues = (ObjectValue) paginationRequest.get().getValue(); IntValue page = (IntValue) paginationValues.getObjectFields().stream().filter(it -> "page".equals(it.getName())).findFirst().get().getValue(); IntValue size = (IntValue) paginationValues.getObjectFields().stream().filter(it -> "size".equals(it.getName())).findFirst().get().getValue(); return new PageInformation(page.getValue().intValue(), size.getValue().intValue()); } return new PageInformation(1, Integer.MAX_VALUE); }
Example #10
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 #11
Source File: GsonScalars.java From graphql-spqr with Apache License 2.0 | 4 votes |
@Override public Object parseLiteral(Object input) { return parseJsonValue(literalOrException(input, ObjectValue.class)); }
Example #12
Source File: JacksonObjectScalars.java From graphql-spqr with Apache License 2.0 | 4 votes |
@Override public Object parseLiteral(Object input, Map<String, Object> variables) { return parseJsonValue(literalOrException(input, ObjectValue.class), variables); }
Example #13
Source File: Scalars.java From graphql-spqr with Apache License 2.0 | 4 votes |
@Override public Object parseLiteral(Object input, Map<String, Object> variables) { return parseObjectValue(literalOrException(input, ObjectValue.class), variables); }
Example #14
Source File: Scalars.java From notification with Apache License 2.0 | 4 votes |
@Nullable @Override public Object parseLiteral(Object input) { return parseObjectValue(literalOrException(input, ObjectValue.class)); }
Example #15
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)); } }