diff --git a/src/Utils/AST.php b/src/Utils/AST.php index 9e54300a1..312912bfa 100644 --- a/src/Utils/AST.php +++ b/src/Utils/AST.php @@ -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; @@ -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; diff --git a/tests/Utils/ValueFromAstTest.php b/tests/Utils/ValueFromAstTest.php index ee3f6a843..d924956e0 100644 --- a/tests/Utils/ValueFromAstTest.php +++ b/tests/Utils/ValueFromAstTest.php @@ -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])); + } }