allowedVariableUsage function

bool allowedVariableUsage(
  1. GraphQLSchema schema,
  2. GraphQLType varType,
  3. ValueNode? varDefaultValue,
  4. GraphQLType locationType,
  5. Object? locationDefaultValue,
)

Returns true if the variable is allowed in the location it was found, which includes considering if default values exist for either the variable or the location at which it is located.

Implementation

bool allowedVariableUsage(
  GraphQLSchema schema,
  GraphQLType varType,
  ValueNode? varDefaultValue,
  GraphQLType locationType,
  Object? locationDefaultValue,
) {
  if (locationType is GraphQLNonNullType && varType is! GraphQLNonNullType) {
    final hasNonNullVariableDefaultValue =
        varDefaultValue != null && varDefaultValue is! NullValueNode;
    final hasLocationDefaultValue = locationDefaultValue != null;
    if (!hasNonNullVariableDefaultValue && !hasLocationDefaultValue) {
      return false;
    }
    final nullableLocationType = locationType.ofType;
    return isTypeSubTypeOf(schema, varType, nullableLocationType);
  }
  return isTypeSubTypeOf(schema, varType, locationType);
}