Automattic\WooCommerce\Api\Infrastructure
MetadataController::get_value_scalar
The MetadataValue custom scalar, accepting any GraphQL-compatible scalar.
The autogenerated scalar template hard-codes acceptance of string literals only, so this scalar is hand-built rather than going through ApiBuilder. parseLiteral walks the AST node types and parseValue accepts the already-decoded PHP scalar that variables-mode delivers.
Method of the class: MetadataController{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = MetadataController::get_value_scalar(): CustomScalarType;
MetadataController::get_value_scalar() MetadataController::get value scalar code WC 10.9.1
private static function get_value_scalar(): CustomScalarType {
if ( null === self::$value_scalar ) {
self::$value_scalar = new CustomScalarType(
array(
'name' => 'MetadataValue',
'description' => __(
'Scalar payload of a metadata entry. Accepts a string, integer, float, boolean, or null.',
'woocommerce'
),
// Resolvers return the raw PHP scalar; webonyx serialises it as JSON directly.
'serialize' => static fn( $value ) => $value,
'parseValue' => static function ( $value ) {
if ( null === $value || is_bool( $value ) || is_int( $value ) || is_float( $value ) || is_string( $value ) ) {
return $value;
}
throw new Error( 'MetadataValue must be a string, integer, float, boolean, or null.' );
},
'parseLiteral' => static function ( $value_node, ?array $variables = null ) {
unset( $variables );
if ( $value_node instanceof StringValueNode ) {
return $value_node->value;
}
if ( $value_node instanceof BooleanValueNode ) {
return $value_node->value;
}
if ( $value_node instanceof IntValueNode ) {
return (int) $value_node->value;
}
if ( $value_node instanceof FloatValueNode ) {
return (float) $value_node->value;
}
if ( $value_node instanceof NullValueNode ) {
return null;
}
throw new Error( 'MetadataValue must be a string, integer, float, boolean, or null literal.' );
},
)
);
}
return self::$value_scalar;
}