use of graphql.language.Argument in project carbon-apimgt by wso2.
the class FieldComplexityCalculatorImpl method getArgumentsValue.
private int getArgumentsValue(List<Argument> argumentList) {
int argumentValue = 0;
if (argumentList.size() > 0) {
for (Argument object : argumentList) {
String argumentName = object.getName();
// The below list of slicing arguments (keywords) effect query complexity to multiply by the factor
// given as the value of the argument.
List<String> slicingArguments = GraphQLConstants.QUERY_COMPLEXITY_SLICING_ARGS;
if (slicingArguments.contains(argumentName.toLowerCase(Locale.ROOT))) {
BigInteger value = null;
if (object.getValue() instanceof IntValue) {
value = ((IntValue) object.getValue()).getValue();
}
int val = 0;
if (value != null) {
val = value.intValue();
}
argumentValue = argumentValue + val;
} else {
argumentValue += 1;
}
}
} else {
argumentValue = 1;
}
return argumentValue;
}
use of graphql.language.Argument in project engine by craftercms.
the class ContentTypeBasedDataFetcher method processSelection.
/**
* Adds the required filters to the ES query for the given field
*/
protected void processSelection(String path, Selection currentSelection, BoolQueryBuilder query, List<String> queryFieldIncludes, DataFetchingEnvironment env) {
if (currentSelection instanceof Field) {
// If the current selection is a field
Field currentField = (Field) currentSelection;
// Get the original field name
String propertyName = getOriginalName(currentField.getName());
// Build the ES-friendly path
String fullPath = StringUtils.isEmpty(path) ? propertyName : path + "." + propertyName;
// If the field has sub selection
if (Objects.nonNull(currentField.getSelectionSet())) {
// If the field is a flattened component
if (fullPath.matches(COMPONENT_INCLUDE_REGEX)) {
// Include the 'content-type' field to make sure the type can be resolved during runtime
String contentTypeFieldPath = fullPath + "." + QUERY_FIELD_NAME_CONTENT_TYPE;
if (!queryFieldIncludes.contains(contentTypeFieldPath)) {
queryFieldIncludes.add(contentTypeFieldPath);
}
}
// Process recursively and finish
currentField.getSelectionSet().getSelections().forEach(selection -> processSelection(fullPath, selection, query, queryFieldIncludes, env));
return;
}
// Add the field to the list
logger.debug("Adding selected field '{}' to query", fullPath);
queryFieldIncludes.add(fullPath);
// Check the filters to build the ES query
Optional<Argument> arg = currentField.getArguments().stream().filter(a -> a.getName().equals(FILTER_NAME)).findFirst();
if (arg.isPresent()) {
logger.debug("Adding filters for field {}", fullPath);
Value<?> argValue = arg.get().getValue();
if (argValue instanceof ObjectValue) {
List<ObjectField> filters = ((ObjectValue) argValue).getObjectFields();
filters.forEach((filter) -> addFieldFilterFromObjectField(fullPath, filter, query, env));
} else if (argValue instanceof VariableReference && env.getVariables().containsKey(((VariableReference) argValue).getName())) {
Map<String, Object> map = (Map<String, Object>) env.getVariables().get(((VariableReference) argValue).getName());
map.entrySet().forEach(filter -> addFieldFilterFromMapEntry(fullPath, filter, query, env));
}
}
} else if (currentSelection instanceof InlineFragment) {
// If the current selection is an inline fragment, process recursively
InlineFragment fragment = (InlineFragment) currentSelection;
fragment.getSelectionSet().getSelections().forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env));
} else if (currentSelection instanceof FragmentSpread) {
// If the current selection is a fragment spread, find the fragment and process recursively
FragmentSpread fragmentSpread = (FragmentSpread) currentSelection;
FragmentDefinition fragmentDefinition = env.getFragmentsByName().get(fragmentSpread.getName());
fragmentDefinition.getSelectionSet().getSelections().forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env));
}
}
use of graphql.language.Argument in project graphql-java by graphql-java.
the class SchemaTypeChecker method checkEnumValues.
private void checkEnumValues(List<GraphQLError> errors, EnumTypeDefinition enumType, List<EnumValueDefinition> enumValueDefinitions) {
// enum unique ness
checkNamedUniqueness(errors, enumValueDefinitions, EnumValueDefinition::getName, (name, inputValueDefinition) -> new NonUniqueNameError(enumType, inputValueDefinition));
// directive checks
enumValueDefinitions.forEach(enumValue -> {
BiFunction<String, Directive, NonUniqueDirectiveError> errorFunction = (directiveName, directive) -> new NonUniqueDirectiveError(enumType, enumValue, directiveName);
checkNamedUniqueness(errors, enumValue.getDirectives(), Directive::getName, errorFunction);
});
enumValueDefinitions.forEach(enumValue -> enumValue.getDirectives().forEach(directive -> {
checkDeprecatedDirective(errors, directive, () -> new InvalidDeprecationDirectiveError(enumType, enumValue));
BiFunction<String, Argument, NonUniqueArgumentError> errorFunction = (argumentName, argument) -> new NonUniqueArgumentError(enumType, enumValue, argumentName);
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, errorFunction);
}));
}
use of graphql.language.Argument in project graphql-java by graphql-java.
the class SchemaTypeExtensionsChecker method checkInputObjectTypeExtensions.
/*
* Input object type extensions have the potential to be invalid if incorrectly defined.
*
* The named type must already be defined and must be a Input Object type.
* All fields of an Input Object type extension must have unique names.
* All fields of an Input Object type extension must not already be a field of the original Input Object.
* Any directives provided must not already apply to the original Input Object type.
*/
private void checkInputObjectTypeExtensions(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) {
typeRegistry.inputObjectTypeExtensions().forEach((name, extensions) -> {
checkTypeExtensionHasCorrespondingType(errors, typeRegistry, name, extensions, InputObjectTypeDefinition.class);
checkTypeExtensionDirectiveRedefinition(errors, typeRegistry, name, extensions, InputObjectTypeDefinition.class);
// field redefinitions
extensions.forEach(extension -> {
List<InputValueDefinition> inputValueDefinitions = extension.getInputValueDefinitions();
// field unique ness
checkNamedUniqueness(errors, inputValueDefinitions, InputValueDefinition::getName, (namedField, fieldDef) -> new NonUniqueNameError(extension, fieldDef));
// directive checks
inputValueDefinitions.forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName, (directiveName, directive) -> new NonUniqueDirectiveError(extension, fld, directiveName)));
inputValueDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
checkDeprecatedDirective(errors, directive, () -> new InvalidDeprecationDirectiveError(extension, fld));
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, (argumentName, argument) -> new NonUniqueArgumentError(extension, fld, argumentName));
}));
//
// fields must be unique within a type extension
forEachBut(extension, extensions, otherTypeExt -> checkForInputValueRedefinition(errors, otherTypeExt, otherTypeExt.getInputValueDefinitions(), inputValueDefinitions));
//
// then check for field re-defs from the base type
Optional<InputObjectTypeDefinition> baseTypeOpt = typeRegistry.getType(extension.getName(), InputObjectTypeDefinition.class);
baseTypeOpt.ifPresent(baseTypeDef -> checkForInputValueRedefinition(errors, extension, inputValueDefinitions, baseTypeDef.getInputValueDefinitions()));
});
});
}
use of graphql.language.Argument in project graphql-java by graphql-java.
the class SchemaTypeExtensionsChecker method checkObjectTypeExtensions.
/*
* Object type extensions have the potential to be invalid if incorrectly defined.
*
* The named type must already be defined and must be an Object type.
* The fields of an Object type extension must have unique names; no two fields may share the same name.
* Any fields of an Object type extension must not be already defined on the original Object type.
* Any directives provided must not already apply to the original Object type.
* Any interfaces provided must not be already implemented by the original Object type.
* The resulting extended object type must be a super-set of all interfaces it implements.
*/
private void checkObjectTypeExtensions(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry) {
typeRegistry.objectTypeExtensions().forEach((name, extensions) -> {
checkTypeExtensionHasCorrespondingType(errors, typeRegistry, name, extensions, ObjectTypeDefinition.class);
checkTypeExtensionDirectiveRedefinition(errors, typeRegistry, name, extensions, ObjectTypeDefinition.class);
extensions.forEach(extension -> {
List<FieldDefinition> fieldDefinitions = extension.getFieldDefinitions();
// field unique ness
checkNamedUniqueness(errors, extension.getFieldDefinitions(), FieldDefinition::getName, (namedField, fieldDef) -> new NonUniqueNameError(extension, fieldDef));
// field arg unique ness
extension.getFieldDefinitions().forEach(fld -> checkNamedUniqueness(errors, fld.getInputValueDefinitions(), InputValueDefinition::getName, (namedField, inputValueDefinition) -> new NonUniqueArgumentError(extension, fld, name)));
// directive checks
extension.getFieldDefinitions().forEach(fld -> checkNamedUniqueness(errors, fld.getDirectives(), Directive::getName, (directiveName, directive) -> new NonUniqueDirectiveError(extension, fld, directiveName)));
fieldDefinitions.forEach(fld -> fld.getDirectives().forEach(directive -> {
checkDeprecatedDirective(errors, directive, () -> new InvalidDeprecationDirectiveError(extension, fld));
checkNamedUniqueness(errors, directive.getArguments(), Argument::getName, (argumentName, argument) -> new NonUniqueArgumentError(extension, fld, argumentName));
}));
//
// fields must be unique within a type extension
forEachBut(extension, extensions, otherTypeExt -> checkForFieldRedefinition(errors, otherTypeExt, otherTypeExt.getFieldDefinitions(), fieldDefinitions));
//
// then check for field re-defs from the base type
Optional<ObjectTypeDefinition> baseTypeOpt = typeRegistry.getType(extension.getName(), ObjectTypeDefinition.class);
baseTypeOpt.ifPresent(baseTypeDef -> checkForFieldRedefinition(errors, extension, fieldDefinitions, baseTypeDef.getFieldDefinitions()));
});
});
}
Aggregations