graphql.language.ObjectField Java Examples

The following examples show how to use graphql.language.ObjectField. 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 vote down vote up
@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: JsonCoercingUtil.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
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: ObjectNodeCoercingTest.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
@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 #4
Source File: JsonCoercingUtilTest.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
@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 #5
Source File: ContentTypeBasedDataFetcher.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
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 #6
Source File: ContentTypeBasedDataFetcher.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
protected QueryBuilder getFilterQueryFromObjectField(String fieldPath, ObjectField filter,
                                                     DataFetchingEnvironment env) {
    switch (filter.getName()) {
        case ARG_NAME_EQUALS:
            return termQuery(fieldPath, getRealValue(filter.getValue(), env));
        case ARG_NAME_MATCHES:
            return matchQuery(fieldPath, getRealValue(filter.getValue(), env));
        case ARG_NAME_REGEX:
            return regexpQuery(fieldPath, getRealValue(filter.getValue(), env).toString());
        case ARG_NAME_LT:
            return rangeQuery(fieldPath).lt(getRealValue(filter.getValue(), env));
        case ARG_NAME_GT:
            return rangeQuery(fieldPath).gt(getRealValue(filter.getValue(), env));
        case ARG_NAME_LTE:
            return rangeQuery(fieldPath).lte(getRealValue(filter.getValue(), env));
        case ARG_NAME_GTE:
            return rangeQuery(fieldPath).gte(getRealValue(filter.getValue(), env));
        case ARG_NAME_EXISTS:
            boolean exists = (boolean) getRealValue(filter.getValue(), env);
            if (exists) {
                return existsQuery(fieldPath);
            } else {
                return boolQuery().mustNot(existsQuery(fieldPath));
            }
        default:
            // never happens
            return null;
    }
}
 
Example #7
Source File: ContentTypeBasedDataFetcher.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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));
    }
}