private Value getArrayValueWrappedType(ArrayValue value) { // empty array [] is equivalent to [null] if (value.getValues().isEmpty()) { return NullValue.Null; } // get rid of null values List<Value> nonNullValueList = value.getValues().stream() .filter(v -> !(v instanceof NullValue)) .collect(Collectors.toList()); // [null, null, ...] unwrapped is null if (nonNullValueList.isEmpty()) { return NullValue.Null; } // make sure the array isn't polymorphic Set<Class<? extends Value>> distinctTypes = nonNullValueList.stream() .map(Value::getClass) .distinct() .collect(Collectors.toSet()); assertTrue(distinctTypes.size() == 1, "Arrays containing multiple types of values are not supported yet. Detected the following types [%s]", nonNullValueList.stream() .map(Value::getClass) .map(Class::getSimpleName) .distinct() .sorted() .collect(Collectors.joining(","))); // peek at first value, value exists and is assured to be non-null return nonNullValueList.get(0); }
@Override public ArrayValue deepCopy() { return new ArrayValue(deepCopy(values), getSourceLocation(), getComments(), getIgnoredChars()); }
private static Value handleList(Object _value, GraphQLList type) { GraphQLType itemType = type.getWrappedType(); boolean isIterable = _value instanceof Iterable; if (isIterable || (_value != null && _value.getClass().isArray())) { Iterable iterable = isIterable ? (Iterable) _value : FpKit.toCollection(_value); List<Value> valuesNodes = new ArrayList<>(); for (Object item : iterable) { Value itemNode = astFromValue(item, itemType); if (itemNode != null) { valuesNodes.add(itemNode); } } return ArrayValue.newArrayValue().values(valuesNodes).build(); } return astFromValue(_value, itemType); }
private Builder(ArrayValue existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.values = existing.getValues(); this.ignoredChars = existing.getIgnoredChars(); }
@Override public TraversalControl visitArrayValue(ArrayValue node, TraverserContext<Node> context) { return changeNode(context, node.transform(builder -> builder.values(Collections.emptyList()))); }
public ArrayValue build() { ArrayValue arrayValue = new ArrayValue(values, sourceLocation, comments, ignoredChars); return arrayValue; } }
private Builder(ArrayValue existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.values = existing.getValues(); this.ignoredChars = existing.getIgnoredChars(); }
@Override public ArrayValue withNewChildren(NodeChildrenContainer newChildren) { return transform(builder -> builder .values(newChildren.getChildren(CHILD_VALUES)) ); }
public ArrayValue build() { ArrayValue arrayValue = new ArrayValue(values, sourceLocation, comments, ignoredChars); return arrayValue; } }
private void checkArgValueMatchesAllowedListType(List<GraphQLError> errors, Value instanceValue, ListType allowedArgType) { if (instanceValue instanceof NullValue) { return; } if (!(instanceValue instanceof ArrayValue)) { addValidationError(errors, EXPECTED_LIST_MESSAGE, instanceValue.getClass().getSimpleName()); return; } ArrayValue arrayValue = ((ArrayValue) instanceValue); Type unwrappedAllowedType = allowedArgType.getType(); // validate each instance value in the list, all instances must match for the list to match arrayValue.getValues().forEach(value -> checkArgValueMatchesAllowedType(errors, value, unwrappedAllowedType)); }
@Override public ArrayValue deepCopy() { return new ArrayValue(deepCopy(values), getSourceLocation(), getComments(), getIgnoredChars()); }
return enumValue.build(); } else if (ctx.arrayValue() != null) { ArrayValue.Builder arrayValue = ArrayValue.newArrayValue(); addCommonData(arrayValue, ctx); List<Value> values = new ArrayList<>();
@Override public TraversalControl visitArrayValue(ArrayValue node, TraverserContext<Node> context) { return changeNode(context, node.transform(builder -> builder.values(Collections.emptyList()))); }
private Object coerceValueAstForList(GraphqlFieldVisibility fieldVisibility, GraphQLList graphQLList, Value value, Map<String, Object> variables) { if (value instanceof ArrayValue) { ArrayValue arrayValue = (ArrayValue) value; List<Object> result = new ArrayList<>(); for (Value singleValue : arrayValue.getValues()) { result.add(coerceValueAst(fieldVisibility, graphQLList.getWrappedType(), singleValue, variables)); } return result; } else { return Collections.singletonList(coerceValueAst(fieldVisibility, graphQLList.getWrappedType(), value, variables)); } }
return enumValue.build(); } else if (ctx.arrayValueWithVariable() != null) { ArrayValue.Builder arrayValue = ArrayValue.newArrayValue(); addCommonData(arrayValue, ctx); List<Value> values = new ArrayList<>();
@Override public ArrayValue withNewChildren(NodeChildrenContainer newChildren) { return transform(builder -> builder .values(newChildren.getChildren(CHILD_VALUES)) ); }
private boolean isValidLiteralValue(Value value, GraphQLList type, GraphQLSchema schema) { GraphQLType wrappedType = type.getWrappedType(); if (value instanceof ArrayValue) { List<Value> values = ((ArrayValue) value).getValues(); for (int i = 0; i < values.size(); i++) { if (!isValidLiteralValue(values.get(i), wrappedType, schema)) { handleFieldNotValidError(values.get(i), wrappedType, i); return false; } } return true; } else { return isValidLiteralValue(value, wrappedType, schema); } }
private static Value handleList(Object _value, GraphQLList type) { GraphQLType itemType = type.getWrappedType(); boolean isIterable = _value instanceof Iterable; if (isIterable || (_value != null && _value.getClass().isArray())) { Iterable iterable = isIterable ? (Iterable) _value : FpKit.toCollection(_value); List<Value> valuesNodes = new ArrayList<>(); for (Object item : iterable) { Value itemNode = astFromValue(item, itemType); if (itemNode != null) { valuesNodes.add(itemNode); } } return ArrayValue.newArrayValue().values(valuesNodes).build(); } return astFromValue(_value, itemType); }
public Object buildValue(Value value, GraphQLType requiredType) { Object result = null; if (GraphQLTypeUtil.isNonNull(requiredType)) { requiredType = unwrapOne(requiredType); } if (value == null) { return null; } if (requiredType instanceof GraphQLScalarType) { result = parseLiteral(value, (GraphQLScalarType) requiredType); } else if (value instanceof EnumValue && requiredType instanceof GraphQLEnumType) { result = ((EnumValue) value).getName(); } else if (value instanceof ArrayValue && isList(requiredType)) { ArrayValue arrayValue = (ArrayValue) value; GraphQLType wrappedType = unwrapOne(requiredType); result = arrayValue.getValues().stream() .map(item -> this.buildValue(item, wrappedType)).collect(Collectors.toList()); } else if (value instanceof ObjectValue && requiredType instanceof GraphQLInputObjectType) { result = buildObjectValue((ObjectValue) value, (GraphQLInputObjectType) requiredType); } else if (!(value instanceof NullValue)) { assertShouldNeverHappen( "cannot build value of %s from %s", requiredType.getName(), String.valueOf(value)); } return result; }
return enumValue.build(); } else if (ctx.arrayValue() != null) { ArrayValue.Builder arrayValue = ArrayValue.newArrayValue(); addCommonData(arrayValue, ctx); List<Value> values = new ArrayList<>();