Search in sources :

Example 1 with GraphQLInputObjectField

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);
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with GraphQLInputObjectField

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);
}
Also used : GraphQLInputType(graphql.schema.GraphQLInputType) GraphQLUnmodifiedType(graphql.schema.GraphQLUnmodifiedType) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType)

Example 3 with GraphQLInputObjectField

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);
            }
        }
    }
}
Also used : GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) GraphQLNonNull(graphql.schema.GraphQLNonNull) GraphQLType(graphql.schema.GraphQLType)

Example 4 with GraphQLInputObjectField

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;
}
Also used : VariableReference(graphql.language.VariableReference) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) ObjectField(graphql.language.ObjectField) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) Value(graphql.language.Value) ArrayValue(graphql.language.ArrayValue) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with GraphQLInputObjectField

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;
}
Also used : GraphQLInputObjectField(graphql.schema.GraphQLInputObjectField) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)8 GraphQLInputType (graphql.schema.GraphQLInputType)3 ObjectField (graphql.language.ObjectField)2 ObjectValue (graphql.language.ObjectValue)2 Value (graphql.language.Value)2 GraphQLInputObjectType (graphql.schema.GraphQLInputObjectType)2 LinkedHashMap (java.util.LinkedHashMap)2 ArrayValue (graphql.language.ArrayValue)1 NullValue (graphql.language.NullValue)1 VariableReference (graphql.language.VariableReference)1 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)1 GraphQLNonNull (graphql.schema.GraphQLNonNull)1 GraphQLType (graphql.schema.GraphQLType)1 GraphQLUnmodifiedType (graphql.schema.GraphQLUnmodifiedType)1 GraphqlFieldVisibility (graphql.schema.visibility.GraphqlFieldVisibility)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1