private Set<String> getMissingFields(GraphQLInputObjectType type, Map<String, ObjectField> objectFieldMap, GraphqlFieldVisibility fieldVisibility) { return fieldVisibility.getFieldDefinitions(type).stream() .filter(field -> isNonNull(field.getType())) .map(GraphQLInputObjectField::getName) .filter(((Predicate<String>) objectFieldMap::containsKey).negate()) .collect(Collectors.toSet()); }
public NonNullableValueCoercedAsNullException(GraphQLInputObjectField inputTypeField) { super(format("Input field '%s' has coerced Null value for NonNull type '%s'", inputTypeField.getName(), GraphQLTypeUtil.simplePrint(inputTypeField.getType()))); }
private void assertNonNullInputField(GraphQLInputObjectField inputTypeField) { if (isNonNull(inputTypeField.getType())) { throw new NonNullableValueCoercedAsNullException(inputTypeField); } }
private boolean alwaysHasValue(GraphQLInputObjectField inputField) { return inputField.getDefaultValue() != null || isNonNull(inputField.getType()); }
private void check(GraphQLInputObjectType type, Set<GraphQLType> seen, List<String> path, SchemaValidationErrorCollector validationErrorCollector) { if (seen.contains(type)) { validationErrorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.UnbrokenInputCycle, getErrorMessage(path))); return; } seen.add(type); for (GraphQLInputObjectField field : type.getFieldDefinitions()) { if (isNonNull(field.getType())) { GraphQLType unwrapped = unwrapNonNull((GraphQLNonNull) field.getType()); if (unwrapped instanceof GraphQLInputObjectType) { path = new ArrayList<>(path); path.add(field.getName() + "!"); check((GraphQLInputObjectType) unwrapped, new LinkedHashSet<>(seen), path, validationErrorCollector); } } } }
public Object buildObjectValue(ObjectValue defaultValue, GraphQLInputObjectType objectType) { Map<String, Object> map = new LinkedHashMap<>(); defaultValue.getObjectFields().forEach(of -> map.put(of.getName(), buildValue(of.getValue(), objectType.getField(of.getName()).getType()))); return map; }
private Object coerceValueForInputObjectType(GraphqlFieldVisibility fieldVisibility, VariableDefinition variableDefinition, GraphQLInputObjectType inputObjectType, Map<String, Object> input) { Map<String, Object> result = new LinkedHashMap<>(); List<GraphQLInputObjectField> fields = fieldVisibility.getFieldDefinitions(inputObjectType); List<String> fieldNames = fields.stream().map(GraphQLInputObjectField::getName).collect(Collectors.toList()); for (String inputFieldName : input.keySet()) { if (!fieldNames.contains(inputFieldName)) { throw new InputMapDefinesTooManyFieldsException(inputObjectType, inputFieldName); } } for (GraphQLInputObjectField inputField : fields) { if (input.containsKey(inputField.getName()) || alwaysHasValue(inputField)) { Object value = coerceValue(fieldVisibility, variableDefinition, inputField.getName(), inputField.getType(), input.get(inputField.getName())); result.put(inputField.getName(), value == null ? inputField.getDefaultValue() : value); } } return result; }
private TypePrinter<GraphQLInputObjectType> inputObjectPrinter() { return (out, type, visibility) -> { if (isIntrospectionType(type)) { return; } printComments(out, type, ""); out.format("input %s%s {\n", type.getName(), directivesString(type.getDirectives())); visibility.getFieldDefinitions(type) .stream() .sorted(Comparator.comparing(GraphQLInputObjectField::getName)) .forEach(fd -> { printComments(out, fd, " "); out.format(" %s: %s", fd.getName(), typeString(fd.getType())); Object defaultValue = fd.getDefaultValue(); if (defaultValue != null) { String astValue = printAst(defaultValue, fd.getType()); out.format(" = %s", astValue); } out.format(directivesString(fd.getDirectives())); out.format("\n"); }); out.format("}\n\n"); }; }
public Builder(GraphQLInputObjectField existing) { this.name = existing.getName(); this.description = existing.getDescription(); this.defaultValue = existing.getDefaultValue(); this.type = existing.getType(); this.definition = existing.getDefinition(); this.directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName)); }
private static Value handleInputObject(Object _value, GraphQLInputObjectType type) { Map mapValue = objToMap(_value); List<GraphQLInputObjectField> fields = type.getFields(); List<ObjectField> fieldNodes = new ArrayList<>(); fields.forEach(field -> { GraphQLInputType fieldType = field.getType(); Value nodeValue = astFromValue(mapValue.get(field.getName()), fieldType); if (nodeValue != null) { fieldNodes.add(ObjectField.newObjectField().name(field.getName()).value(nodeValue).build()); } }); return ObjectValue.newObjectValue().objectFields(fieldNodes).build(); }
private void enterImpl(ObjectField objectField) { GraphQLUnmodifiedType objectType = unwrapAll(getInputType()); GraphQLInputType inputType = null; if (objectType instanceof GraphQLInputObjectType) { GraphQLInputObjectType inputObjectType = (GraphQLInputObjectType) objectType; GraphQLInputObjectField inputField = schema.getFieldVisibility().getFieldDefinition(inputObjectType, objectField.getName()); if (inputField != null) inputType = inputField.getType(); } addInputType(inputType); }
fieldObject = coerceValueAst(fieldVisibility, inputTypeField.getType(), fieldInputValue, variables);
private boolean isValidLiteralValue(Value value, GraphQLInputObjectType type, GraphQLSchema schema) { if (!(value instanceof ObjectValue)) { handleNotObjectError(value, type); return false; } GraphqlFieldVisibility fieldVisibility = schema.getCodeRegistry().getFieldVisibility(); ObjectValue objectValue = (ObjectValue) value; Map<String, ObjectField> objectFieldMap = fieldMap(objectValue); Set<String> missingFields = getMissingFields(type, objectFieldMap, fieldVisibility); if (!missingFields.isEmpty()) { handleMissingFieldsError(value, type, missingFields); return false; } for (ObjectField objectField : objectValue.getObjectFields()) { GraphQLInputObjectField inputObjectField = fieldVisibility.getFieldDefinition(type, objectField.getName()); if (inputObjectField == null) { handleExtraFieldError(value, type, objectField); return false; } if (!isValidLiteralValue(objectField.getValue(), inputObjectField.getType(), schema)) { handleFieldNotValidError(objectField, type); return false; } } return true; }
private Set<String> getMissingFields(GraphQLInputObjectType type, Map<String, ObjectField> objectFieldMap, GraphqlFieldVisibility fieldVisibility) { return fieldVisibility.getFieldDefinitions(type).stream() .filter(field -> isNonNull(field.getType())) .map(GraphQLInputObjectField::getName) .filter(((Predicate<String>) objectFieldMap::containsKey).negate()) .collect(Collectors.toSet()); }
private boolean alwaysHasValue(GraphQLInputObjectField inputField) { return inputField.getDefaultValue() != null || isNonNull(inputField.getType()); }
private void assertNonNullInputField(GraphQLInputObjectField inputTypeField) { if (isNonNull(inputTypeField.getType())) { throw new NonNullableValueCoercedAsNullException(inputTypeField); } }
public Object buildObjectValue(ObjectValue defaultValue, GraphQLInputObjectType objectType) { Map<String, Object> map = new LinkedHashMap<>(); defaultValue.getObjectFields().forEach(of -> map.put(of.getName(), buildValue(of.getValue(), objectType.getField(of.getName()).getType()))); return map; }
public Builder(GraphQLInputObjectField existing) { this.name = existing.getName(); this.description = existing.getDescription(); this.defaultValue = existing.getDefaultValue(); this.type = existing.getType(); this.definition = existing.getDefinition(); this.directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName)); }
private static Value handleInputObject(Object _value, GraphQLInputObjectType type) { Map mapValue = objToMap(_value); List<GraphQLInputObjectField> fields = type.getFields(); List<ObjectField> fieldNodes = new ArrayList<>(); fields.forEach(field -> { GraphQLInputType fieldType = field.getType(); Value nodeValue = astFromValue(mapValue.get(field.getName()), fieldType); if (nodeValue != null) { fieldNodes.add(ObjectField.newObjectField().name(field.getName()).value(nodeValue).build()); } }); return ObjectValue.newObjectValue().objectFields(fieldNodes).build(); }
private void enterImpl(ObjectField objectField) { GraphQLUnmodifiedType objectType = unwrapAll(getInputType()); GraphQLInputType inputType = null; if (objectType instanceof GraphQLInputObjectType) { GraphQLInputObjectType inputObjectType = (GraphQLInputObjectType) objectType; GraphQLInputObjectField inputField = schema.getFieldVisibility().getFieldDefinition(inputObjectType, objectField.getName()); if (inputField != null) inputType = inputField.getType(); } addInputType(inputType); }