Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array arguments string conversion bug #67

Open
kirkmadera opened this issue Aug 16, 2021 · 0 comments
Open

Array arguments string conversion bug #67

kirkmadera opened this issue Aug 16, 2021 · 0 comments

Comments

@kirkmadera
Copy link

kirkmadera commented Aug 16, 2021

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:
image

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.

morloderex referenced this issue in mxneyio/php-graphql-client Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant