Search in sources :

Example 1 with Value

use of graphql.language.Value in project graphql-java by graphql-java.

the class GraphqlAntlrToLanguage method visitVariableDefinition.

@Override
public Void visitVariableDefinition(GraphqlParser.VariableDefinitionContext ctx) {
    VariableDefinition variableDefinition = new VariableDefinition();
    newNode(variableDefinition, ctx);
    variableDefinition.setName(ctx.variable().name().getText());
    if (ctx.defaultValue() != null) {
        Value value = getValue(ctx.defaultValue().value());
        variableDefinition.setDefaultValue(value);
    }
    OperationDefinition operationDefinition = (OperationDefinition) getFromContextStack(ContextProperty.OperationDefinition);
    operationDefinition.getVariableDefinitions().add(variableDefinition);
    addContextProperty(ContextProperty.VariableDefinition, variableDefinition);
    super.visitVariableDefinition(ctx);
    popContext();
    return null;
}
Also used : VariableDefinition(graphql.language.VariableDefinition) FloatValue(graphql.language.FloatValue) Value(graphql.language.Value) ArrayValue(graphql.language.ArrayValue) ObjectValue(graphql.language.ObjectValue) EnumValue(graphql.language.EnumValue) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) BooleanValue(graphql.language.BooleanValue) OperationDefinition(graphql.language.OperationDefinition)

Example 2 with Value

use of graphql.language.Value in project graphql-java by graphql-java.

the class SchemaDiff method checkDirectives.

void checkDirectives(DiffCtx ctx, TypeDefinition old, List<Directive> oldDirectives, List<Directive> newDirectives) {
    if (!options.enforceDirectives) {
        return;
    }
    Map<String, Directive> oldDirectivesMap = sortedMap(oldDirectives, Directive::getName);
    Map<String, Directive> newDirectivesMap = sortedMap(newDirectives, Directive::getName);
    for (String directiveName : oldDirectivesMap.keySet()) {
        Directive oldDirective = oldDirectivesMap.get(directiveName);
        Optional<Directive> newDirective = Optional.ofNullable(newDirectivesMap.get(directiveName));
        if (!newDirective.isPresent()) {
            ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName).reasonMsg("The new API does not have a directive named '%s' on type '%s'", directiveName, old.getName()).build());
            continue;
        }
        Map<String, Argument> oldArgumentsByName = new TreeMap<>(oldDirective.getArgumentsByName());
        Map<String, Argument> newArgumentsByName = new TreeMap<>(newDirective.get().getArgumentsByName());
        if (oldArgumentsByName.size() > newArgumentsByName.size()) {
            ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName).reasonMsg("The new API has less arguments on directive '%s' on type '%s' than the old API", directiveName, old.getName()).build());
            return;
        }
        for (String argName : oldArgumentsByName.keySet()) {
            Argument oldArgument = oldArgumentsByName.get(argName);
            Optional<Argument> newArgument = Optional.ofNullable(newArgumentsByName.get(argName));
            if (!newArgument.isPresent()) {
                ctx.report(DiffEvent.apiBreakage().category(DiffCategory.MISSING).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName, argName).reasonMsg("The new API does not have an argument named '%s' on directive '%s' on type '%s'", argName, directiveName, old.getName()).build());
            } else {
                Value oldValue = oldArgument.getValue();
                Value newValue = newArgument.get().getValue();
                if (oldValue != null && newValue != null) {
                    if (!oldValue.getClass().equals(newValue.getClass())) {
                        ctx.report(DiffEvent.apiBreakage().category(DiffCategory.INVALID).typeName(old.getName()).typeKind(getTypeKind(old)).components(directiveName, argName).reasonMsg("The new API has changed value types on argument named '%s' on directive '%s' on type '%s'", argName, directiveName, old.getName()).build());
                    }
                }
            }
        }
    }
}
Also used : Argument(graphql.language.Argument) Value(graphql.language.Value) TreeMap(java.util.TreeMap) Directive(graphql.language.Directive)

Example 3 with Value

use of graphql.language.Value 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 4 with Value

use of graphql.language.Value in project graphql-java by graphql-java.

the class ValuesResolver method coerceArgumentValues.

/**
 * The http://facebook.github.io/graphql/#sec-Coercing-Variable-Values says :
 *
 * <pre>
 * 1. Let coercedValues be an empty unordered Map.
 * 2. Let variableDefinitions be the variables defined by operation.
 * 3. For each variableDefinition in variableDefinitions:
 *      a. Let variableName be the name of variableDefinition.
 *      b. Let variableType be the expected type of variableDefinition.
 *      c. Let defaultValue be the default value for variableDefinition.
 *      d. Let value be the value provided in variableValues for the name variableName.
 *      e. If value does not exist (was not provided in variableValues):
 *          i. If defaultValue exists (including null):
 *              1. Add an entry to coercedValues named variableName with the value defaultValue.
 *          ii. Otherwise if variableType is a Non‐Nullable type, throw a query error.
 *          iii. Otherwise, continue to the next variable definition.
 *      f. Otherwise, if value cannot be coerced according to the input coercion rules of variableType, throw a query error.
 *      g. Let coercedValue be the result of coercing value according to the input coercion rules of variableType.
 *      h. Add an entry to coercedValues named variableName with the value coercedValue.
 * 4. Return coercedValues.
 * </pre>
 *
 * @param schema              the schema
 * @param variableDefinitions the variable definitions
 * @param variableValues      the supplied variables
 *
 * @return coerced variable values as a map
 */
public Map<String, Object> coerceArgumentValues(GraphQLSchema schema, List<VariableDefinition> variableDefinitions, Map<String, Object> variableValues) {
    GraphqlFieldVisibility fieldVisibility = schema.getFieldVisibility();
    Map<String, Object> coercedValues = new LinkedHashMap<>();
    for (VariableDefinition variableDefinition : variableDefinitions) {
        String variableName = variableDefinition.getName();
        GraphQLType variableType = TypeFromAST.getTypeFromAST(schema, variableDefinition.getType());
        // 3.e
        if (!variableValues.containsKey(variableName)) {
            Value defaultValue = variableDefinition.getDefaultValue();
            if (defaultValue != null) {
                // 3.e.i
                Object coercedValue = coerceValueAst(fieldVisibility, variableType, variableDefinition.getDefaultValue(), null);
                coercedValues.put(variableName, coercedValue);
            } else if (isNonNullType(variableType)) {
                // 3.e.ii
                throw new NonNullableValueCoercedAsNullException(variableDefinition, variableType);
            }
        } else {
            Object value = variableValues.get(variableName);
            // 3.f
            Object coercedValue = getVariableValue(fieldVisibility, variableDefinition, variableType, value);
            // 3.g
            coercedValues.put(variableName, coercedValue);
        }
    }
    return coercedValues;
}
Also used : GraphqlFieldVisibility(graphql.schema.visibility.GraphqlFieldVisibility) VariableDefinition(graphql.language.VariableDefinition) GraphQLType(graphql.schema.GraphQLType) NullValue(graphql.language.NullValue) ObjectValue(graphql.language.ObjectValue) Value(graphql.language.Value) ArrayValue(graphql.language.ArrayValue) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with Value

use of graphql.language.Value in project structr by structr.

the class QueryConfig method handleFieldArguments.

public void handleFieldArguments(final SecurityContext securityContext, final Class type, final Field parentField, final Field field) throws FrameworkException {
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final PropertyKey parentKey = config.getPropertyKeyForJSONName(type, parentField.getName());
    final PropertyKey key = config.getPropertyKeyForJSONName(type, field.getName(), false);
    final List<SearchTuple> searchTuples = new LinkedList<>();
    Occurrence occurrence = Occurrence.REQUIRED;
    // parse arguments
    for (final Argument argument : field.getArguments()) {
        final String name = argument.getName();
        final Value value = argument.getValue();
        switch(name) {
            case "_equals":
                searchTuples.add(new SearchTuple(castValue(securityContext, key.relatedType(), key, value), true));
                break;
            case "_contains":
                searchTuples.add(new SearchTuple(castValue(securityContext, key.relatedType(), key, value), false));
                break;
            case "_conj":
                occurrence = getOccurrence(value);
                break;
            default:
                break;
        }
    }
    // only add field if a value was set
    for (final SearchTuple tuple : searchTuples) {
        addAttribute(parentKey, key.getSearchAttribute(securityContext, occurrence, tuple.value, tuple.exact, null), occurrence);
    }
}
Also used : Argument(graphql.language.Argument) ConfigurationProvider(org.structr.schema.ConfigurationProvider) ObjectValue(graphql.language.ObjectValue) Value(graphql.language.Value) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) BooleanValue(graphql.language.BooleanValue) Occurrence(org.structr.api.search.Occurrence) PropertyKey(org.structr.core.property.PropertyKey) LinkedList(java.util.LinkedList)

Aggregations

Value (graphql.language.Value)11 ObjectValue (graphql.language.ObjectValue)6 ArrayValue (graphql.language.ArrayValue)4 StringValue (graphql.language.StringValue)4 Argument (graphql.language.Argument)3 BooleanValue (graphql.language.BooleanValue)3 IntValue (graphql.language.IntValue)3 NullValue (graphql.language.NullValue)3 Type (graphql.language.Type)2 VariableDefinition (graphql.language.VariableDefinition)2 GraphQLInputObjectField (graphql.schema.GraphQLInputObjectField)2 GraphQLInputType (graphql.schema.GraphQLInputType)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 PropertyKey (org.structr.core.property.PropertyKey)2 Directive (graphql.language.Directive)1 EnumValue (graphql.language.EnumValue)1 FloatValue (graphql.language.FloatValue)1 InputValueDefinition (graphql.language.InputValueDefinition)1 ListType (graphql.language.ListType)1