Array arguments string conversion bug
kirkmadera opened this issue · 0 comments
kirkmadera commented
We noticed an "Array to string conversion" bug with some of our queries. Not sure if we made the right fix, but we ended up overriding a few classes in order to fix.
GraphQl\Query::constructArguments
:
GraphQL\Util\StringLiteralFormatter
:
Added these methods:
/**
* @param array $array
*
* @return string
*/
public static function formatArrayForGQLQuery(array $array): string
{
$arrString = '{';
$first = true;
foreach ($array as $name => $element) {
if ($first) {
$first = false;
} else {
$arrString .= ', ';
}
if (is_array($element)) {
$arrString .= $name . ':';
if (array_keys($element) !== range(0, count($element) - 1)) {
$arrString .= static::formatAssociativeArray($element);
} else {
$arrString .= static::formatSequentialArray($element);
}
} else {
$arrString .= $name . ':' . \GraphQL\Util\StringLiteralFormatter::formatValueForRHS($element);
}
}
$arrString .= '}';
return $arrString;
}
/**
* @param $array
* @return string
*/
public static function formatSequentialArray($array): string
{
$arrString = '[';
foreach ($array as $value) {
$arrString .= static::formatAssociativeArray($value);
}
$arrString .= ']';
return $arrString;
}
/**
* @param $array
* @return string
*/
public static function formatAssociativeArray($array): string
{
$arrString = '{';
$first = true;
foreach ($array as $key => $val) {
if ($first) {
$first = false;
} else {
$arrString .= ', ';
}
if (is_array($val)) {
$arrString .= $key . ':';
if (array_keys($val) !== range(0, count($val) - 1)) {
$arrString .= static::formatAssociativeArray($val);
} else {
$arrString .= static::formatSequentialArray($val);
}
} else {
$arrString .= $key . ':' . \GraphQL\Util\StringLiteralFormatter::formatValueForRHS($val);
}
}
$arrString .= '}';
return $arrString;
}
Not sure if this is the right approach or if we are using this incorrectly, but this did get us past the issues we were running into.