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;
}
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());
}
}
}
}
}
}
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;
}
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;
}
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);
}
}
Aggregations