Skip to content

Fixing arguments order when getting value from AST #533

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Utils/AST.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@
use Throwable;
use Traversable;
use function array_combine;
use function array_flip;
use function array_key_exists;
use function array_keys;
use function array_map;
use function array_replace;
use function count;
use function floatval;
use function intval;
Expand Down Expand Up @@ -410,6 +413,10 @@ static function ($field) {
return $field->name->value;
}
);

// re-order $fields with $fieldNodes order to keep order of the user's input
$fields = array_replace(array_flip(array_keys($fieldNodes)), $fields);

foreach ($fields as $field) {
/** @var VariableNode|NullValueNode|IntValueNode|FloatValueNode|StringValueNode|BooleanValueNode|EnumValueNode|ListValueNode|ObjectValueNode $fieldNode */
$fieldName = $field->name;
Expand Down
8 changes: 8 additions & 0 deletions tests/Utils/ValueFromAstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,12 @@ public function testOmitsInputObjectFieldsForUnprovidedVariables() : void
['int' => 42, 'requiredBool' => true]
);
}

public function testValuesReturnedAreOrderedAsInTheAST()
{
$testInputObj = $this->inputObj();

// assertSame is used here because we have to check that the array keys have the same order
self::assertSame(['requiredBool' => true, 'bool' => true, 'int' => 21], AST::valueFromAST(Parser::parseValue('{ requiredBool: true, bool: $foo, int: 21 }'), $testInputObj, ['foo' => true]));
}
}