use of graphql.schema.InputValueWithState in project graphql-java by graphql-java.
the class ValuesResolver method externalValueToInternalValueForObject.
/**
* performs validation
*/
private Object externalValueToInternalValueForObject(GraphqlFieldVisibility fieldVisibility, GraphQLInputObjectType inputObjectType, Map<String, Object> inputMap) throws NonNullableValueCoercedAsNullException, CoercingParseValueException {
List<GraphQLInputObjectField> fieldDefinitions = fieldVisibility.getFieldDefinitions(inputObjectType);
List<String> fieldNames = map(fieldDefinitions, GraphQLInputObjectField::getName);
for (String providedFieldName : inputMap.keySet()) {
if (!fieldNames.contains(providedFieldName)) {
throw new InputMapDefinesTooManyFieldsException(inputObjectType, providedFieldName);
}
}
Map<String, Object> coercedValues = new LinkedHashMap<>();
for (GraphQLInputObjectField inputFieldDefinition : fieldDefinitions) {
GraphQLInputType fieldType = inputFieldDefinition.getType();
String fieldName = inputFieldDefinition.getName();
InputValueWithState defaultValue = inputFieldDefinition.getInputFieldDefaultValue();
boolean hasValue = inputMap.containsKey(fieldName);
Object value;
Object fieldValue = inputMap.getOrDefault(fieldName, null);
value = fieldValue;
if (!hasValue && inputFieldDefinition.hasSetDefaultValue()) {
Object coercedDefaultValue = defaultValueToInternalValue(fieldVisibility, defaultValue, fieldType);
coercedValues.put(fieldName, coercedDefaultValue);
} else if (isNonNull(fieldType) && (!hasValue || value == null)) {
throw new NonNullableValueCoercedAsNullException(fieldName, emptyList(), fieldType);
} else if (hasValue) {
if (value == null) {
coercedValues.put(fieldName, null);
} else {
value = externalValueToInternalValue(fieldVisibility, fieldType, value);
coercedValues.put(fieldName, value);
}
}
}
return coercedValues;
}
use of graphql.schema.InputValueWithState in project graphql-java by graphql-java.
the class ValuesResolver method getArgumentValuesImpl.
private Map<String, Object> getArgumentValuesImpl(GraphqlFieldVisibility fieldVisibility, List<GraphQLArgument> argumentTypes, List<Argument> arguments, Map<String, Object> coercedVariables) {
if (argumentTypes.isEmpty()) {
return Collections.emptyMap();
}
Map<String, Object> coercedValues = new LinkedHashMap<>();
Map<String, Argument> argumentMap = argumentMap(arguments);
for (GraphQLArgument argumentDefinition : argumentTypes) {
GraphQLInputType argumentType = argumentDefinition.getType();
String argumentName = argumentDefinition.getName();
Argument argument = argumentMap.get(argumentName);
InputValueWithState defaultValue = argumentDefinition.getArgumentDefaultValue();
boolean hasValue = argument != null;
Object value;
Value argumentValue = argument != null ? argument.getValue() : null;
if (argumentValue instanceof VariableReference) {
String variableName = ((VariableReference) argumentValue).getName();
hasValue = coercedVariables.containsKey(variableName);
value = coercedVariables.get(variableName);
} else {
value = argumentValue;
}
if (!hasValue && argumentDefinition.hasSetDefaultValue()) {
Object coercedDefaultValue = defaultValueToInternalValue(fieldVisibility, defaultValue, argumentType);
coercedValues.put(argumentName, coercedDefaultValue);
} else if (isNonNull(argumentType) && (!hasValue || isNullValue(value))) {
throw new NonNullableValueCoercedAsNullException(argumentDefinition);
} else if (hasValue) {
if (isNullValue(value)) {
coercedValues.put(argumentName, value);
} else if (argumentValue instanceof VariableReference) {
coercedValues.put(argumentName, value);
} else {
value = literalToInternalValue(fieldVisibility, argumentType, argument.getValue(), coercedVariables);
coercedValues.put(argumentName, value);
}
}
}
return coercedValues;
}
use of graphql.schema.InputValueWithState in project graphql-java by graphql-java.
the class IntrospectionWithDirectivesSupport method addAppliedDirectives.
private GraphQLObjectType addAppliedDirectives(GraphQLObjectType originalType, GraphQLCodeRegistry.Builder codeRegistry, GraphQLObjectType appliedDirectiveType, GraphQLObjectType directiveArgumentType) {
GraphQLObjectType objectType = originalType.transform(bld -> bld.field(fld -> fld.name("appliedDirectives").type(nonNull(list(nonNull(appliedDirectiveType))))));
DataFetcher<?> df = env -> {
Object source = env.getSource();
GraphQLSchema schema = env.getGraphQLSchema();
if (source instanceof GraphQLDirectiveContainer) {
GraphQLDirectiveContainer type = env.getSource();
List<GraphQLAppliedDirective> appliedDirectives = DirectivesUtil.toAppliedDirectives(type);
return filterAppliedDirectives(schema, false, type, appliedDirectives);
}
if (source instanceof GraphQLSchema) {
List<GraphQLAppliedDirective> appliedDirectives = DirectivesUtil.toAppliedDirectives(schema.getSchemaAppliedDirectives(), schema.getSchemaDirectives());
return filterAppliedDirectives(schema, true, null, appliedDirectives);
}
return assertShouldNeverHappen("What directive containing element have we not considered? - %s", originalType);
};
DataFetcher<?> argsDF = env -> {
final GraphQLAppliedDirective directive = env.getSource();
// we only show directive arguments that have values set on them
return directive.getArguments().stream().filter(arg -> arg.getArgumentValue().isSet());
};
DataFetcher<?> argValueDF = env -> {
final GraphQLAppliedDirectiveArgument argument = env.getSource();
InputValueWithState value = argument.getArgumentValue();
Node<?> literal = ValuesResolver.valueToLiteral(value, argument.getType());
return AstPrinter.printAst(literal);
};
codeRegistry.dataFetcher(coordinates(objectType, "appliedDirectives"), df);
codeRegistry.dataFetcher(coordinates(appliedDirectiveType, "args"), argsDF);
codeRegistry.dataFetcher(coordinates(directiveArgumentType, "value"), argValueDF);
return objectType;
}
use of graphql.schema.InputValueWithState in project graphql-java by graphql-java.
the class DefaultValuesAreValid method visitGraphQLInputObjectField.
@Override
public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField inputObjectField, TraverserContext<GraphQLSchemaElement> context) {
if (!inputObjectField.hasSetDefaultValue()) {
return TraversalControl.CONTINUE;
}
SchemaValidationErrorCollector errorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class);
GraphQLSchema schema = context.getVarFromParents(GraphQLSchema.class);
InputValueWithState defaultValue = inputObjectField.getInputFieldDefaultValue();
boolean invalid = false;
if (defaultValue.isLiteral() && !validationUtil.isValidLiteralValue((Value<?>) defaultValue.getValue(), inputObjectField.getType(), schema)) {
invalid = true;
} else if (defaultValue.isExternal() && !isValidExternalValue(schema, defaultValue.getValue(), inputObjectField.getType())) {
invalid = true;
}
if (invalid) {
String message = format("Invalid default value %s for type %s", defaultValue.getValue(), simplePrint(inputObjectField.getType()));
errorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.InvalidDefaultValue, message));
}
return TraversalControl.CONTINUE;
}
use of graphql.schema.InputValueWithState in project graphql-java by graphql-java.
the class AppliedDirectiveArgumentsAreValid method checkArgument.
private void checkArgument(GraphQLDirective directive, GraphQLArgument argument, TraverserContext<GraphQLSchemaElement> context) {
if (!argument.hasSetValue()) {
return;
}
GraphQLSchema schema = context.getVarFromParents(GraphQLSchema.class);
SchemaValidationErrorCollector errorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class);
InputValueWithState argumentValue = argument.getArgumentValue();
boolean invalid = false;
if (argumentValue.isLiteral() && !validationUtil.isValidLiteralValue((Value<?>) argumentValue.getValue(), argument.getType(), schema)) {
invalid = true;
} else if (argumentValue.isExternal() && !isValidExternalValue(schema, argumentValue.getValue(), argument.getType())) {
invalid = true;
}
if (invalid) {
String message = format("Invalid argument '%s' for applied directive of name '%s'", argument.getName(), directive.getName());
errorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.InvalidAppliedDirectiveArgument, message));
}
}
Aggregations