use of graphql.schema.GraphQLInputObjectField in project graphql-java by graphql-java.
the class AstValueHelper method handleInputObject.
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(new ObjectField(field.getName(), nodeValue));
}
});
return new ObjectValue(fieldNodes);
}
use of graphql.schema.GraphQLInputObjectField in project graphql-java by graphql-java.
the class TraversalContext method enterImpl.
private void enterImpl(ObjectField objectField) {
GraphQLUnmodifiedType objectType = schemaUtil.getUnmodifiedType(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);
}
use of graphql.schema.GraphQLInputObjectField in project graphql-java by graphql-java.
the class NoUnbrokenInputCycles method check.
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 (field.getType() instanceof GraphQLNonNull) {
GraphQLType unwrapped = unwrapNonNull((GraphQLNonNull) field.getType());
if (unwrapped instanceof GraphQLInputObjectType) {
path = new ArrayList<>(path);
path.add(field.getName() + "!");
check((GraphQLInputObjectType) unwrapped, new HashSet<>(seen), path, validationErrorCollector);
}
}
}
}
use of graphql.schema.GraphQLInputObjectField in project graphql-java by graphql-java.
the class ValuesResolver method coerceValueAstForInputObject.
private Object coerceValueAstForInputObject(GraphqlFieldVisibility fieldVisibility, GraphQLInputObjectType type, ObjectValue inputValue, Map<String, Object> variables) {
Map<String, Object> result = new LinkedHashMap<>();
Map<String, ObjectField> inputValueFieldsByName = mapObjectValueFieldsByName(inputValue);
List<GraphQLInputObjectField> inputFields = fieldVisibility.getFieldDefinitions(type);
for (GraphQLInputObjectField inputTypeField : inputFields) {
if (inputValueFieldsByName.containsKey(inputTypeField.getName())) {
boolean putObjectInMap = true;
ObjectField field = inputValueFieldsByName.get(inputTypeField.getName());
Value fieldInputValue = field.getValue();
Object fieldObject = null;
if (fieldInputValue instanceof VariableReference) {
String varName = ((VariableReference) fieldInputValue).getName();
if (!variables.containsKey(varName)) {
putObjectInMap = false;
} else {
fieldObject = variables.get(varName);
}
} else {
fieldObject = coerceValueAst(fieldVisibility, inputTypeField.getType(), fieldInputValue, variables);
}
if (fieldObject == null) {
if (!field.getValue().isEqualTo(NullValue.Null)) {
fieldObject = inputTypeField.getDefaultValue();
}
}
if (putObjectInMap) {
result.put(field.getName(), fieldObject);
} else {
assertNonNullInputField(inputTypeField);
}
} else if (inputTypeField.getDefaultValue() != null) {
result.put(inputTypeField.getName(), inputTypeField.getDefaultValue());
} else {
assertNonNullInputField(inputTypeField);
}
}
return result;
}
use of graphql.schema.GraphQLInputObjectField in project graphql-java by graphql-java.
the class ValuesResolver method coerceValueForInputObjectType.
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;
}
Aggregations