src/Utils/AST.php
public static function valueFromAST($valueNode, InputType $type, ?array $variables = null)
    {
        $undefined = Utils::undefined();

        if ($valueNode === null) {
            // When there is no AST, then there is also no value.
            // Importantly, this is different from returning the GraphQL null value.
            return $undefined;
        }

        if ($type instanceof NonNull) {
            if ($valueNode instanceof NullValueNode) {
                // Invalid: intentionally return no value.
                return $undefined;
            }

            return self::valueFromAST($valueNode, $type->getWrappedType(), $variables);
        }

        if ($valueNode instanceof NullValueNode) {
            // This is explicitly returning the value null.
            return null;
        }

        if ($valueNode instanceof VariableNode) {
            $variableName = $valueNode->name->value;

            if (! $variables || ! array_key_exists($variableName, $variables)) {
                // No valid return value.
                return $undefined;
            }

            // Note: we're not doing any checking that this variable is correct. We're
            // assuming that this query has been validated and the variable usage here
            // is of the correct type.
            return $variables[$variableName];
        }

        if ($type instanceof ListOfType) {
            $itemType = $type->getWrappedType();

            if ($valueNode instanceof ListValueNode) {
                $coercedValues = [];
                $itemNodes     = $valueNode->values;
                foreach ($itemNodes as $itemNode) {
                    if (self::isMissingVariable($itemNode, $variables)) {
                        // If an array contains a missing variable, it is either coerced to
                        // null or if the item type is non-null, it considered invalid.
                        if ($itemType instanceof NonNull) {
                            // Invalid: intentionally return no value.
                            return $undefined;
                        }
                        $coercedValues[] = null;
                    } else {
                        $itemValue = self::valueFromAST($itemNode, $itemType, $variables);
                        if ($undefined === $itemValue) {
                            // Invalid: intentionally return no value.
                            return $undefined;
                        }
                        $coercedValues[] = $itemValue;
                    }
                }

                return $coercedValues;
            }
            $coercedValue = self::valueFromAST($valueNode, $itemType, $variables);
            if ($undefined === $coercedValue) {
                // Invalid: intentionally return no value.
                return $undefined;
            }

            return [$coercedValue];
        }

        if ($type instanceof InputObjectType) {
            if (! $valueNode instanceof ObjectValueNode) {
                // Invalid: intentionally return no value.
                return $undefined;
            }

            $coercedObj = [];
            $fields     = $type->getFields();
            $fieldNodes = Utils::keyMap(
                $valueNode->fields,
                static function ($field) {
                    return $field->name->value;
                }
            );
            foreach ($fields as $field) {
                /** @var ValueNode $fieldNode */
                $fieldName = $field->name;
                $fieldNode = $fieldNodes[$fieldName] ?? null;

                if ($fieldNode === null || self::isMissingVariable($fieldNode->value, $variables)) {
                    if ($field->defaultValueExists()) {
                        $coercedObj[$fieldName] = $field->defaultValue;
                    } elseif ($field->getType() instanceof NonNull) {
                        // Invalid: intentionally return no value.
                        return $undefined;
                    }
                    continue;
                }

                $fieldValue = self::valueFromAST(
                    $fieldNode !== null ? $fieldNode->value : null,
                    $field->getType(),
                    $variables
                );

                if ($undefined === $fieldValue) {
                    // Invalid: intentionally return no value.
                    return $undefined;
                }
                $coercedObj[$fieldName] = $fieldValue;
            }

            return $coercedObj;
        }

        if ($type instanceof EnumType) {
            if (! $valueNode instanceof EnumValueNode) {
                return $undefined;
            }
            $enumValue = $type->getValue($valueNode->value);
            if (! $enumValue) {
                return $undefined;
            }

            return $enumValue->value;
        }

        if ($type instanceof ScalarType) {
            // Scalars fulfill parsing a literal value via parseLiteral().
            // Invalid values represent a failure to parse correctly, in which case
            // no value is returned.
            try {
                return $type->parseLiteral($valueNode, $variables);
            } catch (Exception $error) {
                return $undefined;
            } catch (Throwable $error) {
                return $undefined;
            }
        }

        throw new Error('Unknown type: ' . Utils::printSafe($type) . '.');
    }
public static function valueFromAST($valueNode, InputType $type, ?array $variables = null)
    {
        $undefined = Utils::undefined();

        if ($valueNode === null) {
            // When there is no AST, then there is also no value.
            // Importantly, this is different from returning the GraphQL null value.
            return $undefined;
        }

        if ($type instanceof NonNull) {
            if ($valueNode instanceof NullValueNode) {
                // Invalid: intentionally return no value.
                return $undefined;
            }

            return self::valueFromAST($valueNode, $type->getWrappedType(), $variables);
        }

        if ($valueNode instanceof NullValueNode) {
            // This is explicitly returning the value null.
            return null;
        }

        if ($valueNode instanceof VariableNode) {
            $variableName = $valueNode->name->value;

            if (! $variables || ! array_key_exists($variableName, $variables)) {
                // No valid return value.
                return $undefined;
            }

            // Note: we're not doing any checking that this variable is correct. We're
            // assuming that this query has been validated and the variable usage here
            // is of the correct type.
            return $variables[$variableName];
        }

        if ($type instanceof ListOfType) {
            $itemType = $type->getWrappedType();

            if ($valueNode instanceof ListValueNode) {
                $coercedValues = [];
                $itemNodes     = $valueNode->values;
                foreach ($itemNodes as $itemNode) {
                    if (self::isMissingVariable($itemNode, $variables)) {
                        // If an array contains a missing variable, it is either coerced to
                        // null or if the item type is non-null, it considered invalid.
                        if ($itemType instanceof NonNull) {
                            // Invalid: intentionally return no value.
                            return $undefined;
                        }
                        $coercedValues[] = null;
                    } else {
                        $itemValue = self::valueFromAST($itemNode, $itemType, $variables);
                        if ($undefined === $itemValue) {
                            // Invalid: intentionally return no value.
                            return $undefined;
                        }
                        $coercedValues[] = $itemValue;
                    }
                }

                return $coercedValues;
            }
            $coercedValue = self::valueFromAST($valueNode, $itemType, $variables);
            if ($undefined === $coercedValue) {
                // Invalid: intentionally return no value.
                return $undefined;
            }

            return [$coercedValue];
        }

        if ($type instanceof InputObjectType) {
            if (! $valueNode instanceof ObjectValueNode) {
                // Invalid: intentionally return no value.
                return $undefined;
            }

            $coercedObj = [];
            $fields     = $type->getFields();
            $fieldNodes = Utils::keyMap(
                $valueNode->fields,
                static function ($field) {
                    return $field->name->value;
                }
            );
            foreach ($fields as $field) {
                /** @var ValueNode $fieldNode */
                $fieldName = $field->name;
                $fieldNode = $fieldNodes[$fieldName] ?? null;

                if ($fieldNode === null || self::isMissingVariable($fieldNode->value, $variables)) {
                    if ($field->defaultValueExists()) {
                        $coercedObj[$fieldName] = $field->defaultValue;
                    } elseif ($field->getType() instanceof NonNull) {
                        // Invalid: intentionally return no value.
                        return $undefined;
                    }
                    continue;
                }

                $fieldValue = self::valueFromAST(
                    $fieldNode !== null ? $fieldNode->value : null,
                    $field->getType(),
                    $variables
                );

                if ($undefined === $fieldValue) {
                    // Invalid: intentionally return no value.
                    return $undefined;
                }
                $coercedObj[$fieldName] = $fieldValue;
            }

            return $coercedObj;
        }

        if ($type instanceof EnumType) {
            if (! $valueNode instanceof EnumValueNode) {
                return $undefined;
            }
            $enumValue = $type->getValue($valueNode->value);
            if (! $enumValue) {
                return $undefined;
            }

            return $enumValue->value;
        }

        if ($type instanceof ScalarType) {
            // Scalars fulfill parsing a literal value via parseLiteral().
            // Invalid values represent a failure to parse correctly, in which case
            // no value is returned.
            try {
                return $type->parseLiteral($valueNode, $variables);
            } catch (Exception $error) {
                return $undefined;
            } catch (Throwable $error) {
                return $undefined;
            }
        }

        throw new Error('Unknown type: ' . Utils::printSafe($type) . '.');
    }
public static function valueFromAST ( $ valueNode , InputType $ type , ? array $ variables = null ) { $ undefined = Utils :: undefined ( ) ; if ( $ valueNode === null ) { // When there is no AST, then there is also no value. // Importantly, this is different from returning the GraphQL null value. return $ undefined ; } if ( $ type instanceof NonNull ) { if ( $ valueNode instanceof NullValueNode ) { // Invalid: intentionally return no value. return $ undefined ; } return self :: valueFromAST ( $ valueNode , $ type -> getWrappedType ( ) , $ variables ) ; } if ( $ valueNode instanceof NullValueNode ) { // This is explicitly returning the value null. return null ; } if ( $ valueNode instanceof VariableNode ) { $ variableName = $ valueNode -> name -> value ; if ( ! $ variables || ! array_key_exists ( $ variableName , $ variables ) ) { // No valid return value. return $ undefined ; } // Note: we're not doing any checking that this variable is correct. We're // assuming that this query has been validated and the variable usage here // is of the correct type. return $ variables [ $ variableName ] ; } if ( $ type instanceof ListOfType ) { $ itemType = $ type -> getWrappedType ( ) ; if ( $ valueNode instanceof ListValueNode ) { $ coercedValues = [ ] ; $ itemNodes = $ valueNode -> values ; foreach ( $ itemNodes as $ itemNode ) { if ( self :: isMissingVariable ( $ itemNode , $ variables ) ) { // If an array contains a missing variable, it is either coerced to // null or if the item type is non-null, it considered invalid. if ( $ itemType instanceof NonNull ) { // Invalid: intentionally return no value. return $ undefined ; } $ coercedValues [ ] = null ; } else { $ itemValue = self :: valueFromAST ( $ itemNode , $ itemType , $ variables ) ; if ( $ undefined === $ itemValue ) { // Invalid: intentionally return no value. return $ undefined ; } $ coercedValues [ ] = $ itemValue ; } } return $ coercedValues ; } $ coercedValue = self :: valueFromAST ( $ valueNode , $ itemType , $ variables ) ; if ( $ undefined === $ coercedValue ) { // Invalid: intentionally return no value. return $ undefined ; } return [ $ coercedValue ] ; } if ( $ type instanceof InputObjectType ) { if ( ! $ valueNode instanceof ObjectValueNode ) { // Invalid: intentionally return no value. return $ undefined ; } $ coercedObj = [ ] ; $ fields = $ type -> getFields ( ) ; $ fieldNodes = Utils :: keyMap ( $ valueNode -> fields , static function ( $ field ) { return $ field -> name -> value ; } ) ; foreach ( $ fields as $ field ) { /** @var ValueNode $fieldNode */ $ fieldName = $ field -> name ; $ fieldNode = $ fieldNodes [ $ fieldName ] ?? null ; if ( $ fieldNode === null || self :: isMissingVariable ( $ fieldNode -> value , $ variables ) ) { if ( $ field -> defaultValueExists ( ) ) { $ coercedObj [ $ fieldName ] = $ field -> defaultValue ; } elseif ( $ field -> getType ( ) instanceof NonNull ) { // Invalid: intentionally return no value. return $ undefined ; } continue ; } $ fieldValue = self :: valueFromAST ( $ fieldNode !== null ? $ fieldNode -> value : null , $ field -> getType ( ) , $ variables ) ; if ( $ undefined === $ fieldValue ) { // Invalid: intentionally return no value. return $ undefined ; } $ coercedObj [ $ fieldName ] = $ fieldValue ; } return $ coercedObj ; } if ( $ type instanceof EnumType ) { if ( ! $ valueNode instanceof EnumValueNode ) { return $ undefined ; } $ enumValue = $ type -> getValue ( $ valueNode -> value ) ; if ( ! $ enumValue ) { return $ undefined ; } return $ enumValue -> value ; } if ( $ type instanceof ScalarType ) { // Scalars fulfill parsing a literal value via parseLiteral(). // Invalid values represent a failure to parse correctly, in which case // no value is returned. try { return $ type -> parseLiteral ( $ valueNode , $ variables ) ; } catch ( Exception $ error ) { return $ undefined ; } catch ( Throwable $ error ) { return $ undefined ; } } throw new Error ( 'Unknown type: ' . Utils :: printSafe ( $ type ) . '.' ) ; }
Produces a PHP value given a GraphQL Value AST.

A GraphQL type must be provided, which will be used to interpret different
GraphQL Value literals.

Returns `null` when the value could not be validly coerced according to
the provided type.

| GraphQL Value        | PHP Value     |
| -------------------- | ------------- |
| Input Object         | Assoc Array   |
| List                 | Array         |
| Boolean              | Boolean       |
| String               | String        |
| Int / Float          | Int / Float   |
| Enum Value           | Mixed         |
| Null Value           | null          |

@param ValueNode|null $valueNode
@param mixed[]|null   $variables

@return mixed[]|stdClass|null

@throws Exception

@api
Produces a PHP value given a GraphQL Value AST .