Laravel 灯塔现在注入日期时间
Laravel lighthouse inject now datetime
有没有办法将 now datetime 注入到突变中?
像这样:
customerUpsert(input: CustomerInput! @spread): Customer
@upsert(model: "App\Models\Customer")
@inject(context: ???, name: "customer_since")
我在 laravel 灯塔中制作了一个 Scalar 来满足我的需要:
<?php
namespace App\GraphQL\Scalars;
use GraphQL\Error\Error;
use GraphQL\Type\Definition\ScalarType;
class CustomValue extends ScalarType
{
public function serialize($value): string
{
return $value;
}
public function parseValue($value): string
{
return $value;
}
public function parseLiteral($valueNode, $variables = [])
{
$map = [
"now" => now(),
];
if (!array_key_exists($valueNode->value, $map)) {
throw new Error(
"Query error: Invalid custom value",
$valueNode
);
}
return $map[$valueNode->value];
}
}
如果需要,您可以添加要默认设置的其他值。这是它的用法示例:
input CustomerInput {
id: ID
name: String!
date: CustomValue = "now"
}
有没有办法将 now datetime 注入到突变中?
像这样:
customerUpsert(input: CustomerInput! @spread): Customer
@upsert(model: "App\Models\Customer")
@inject(context: ???, name: "customer_since")
我在 laravel 灯塔中制作了一个 Scalar 来满足我的需要:
<?php
namespace App\GraphQL\Scalars;
use GraphQL\Error\Error;
use GraphQL\Type\Definition\ScalarType;
class CustomValue extends ScalarType
{
public function serialize($value): string
{
return $value;
}
public function parseValue($value): string
{
return $value;
}
public function parseLiteral($valueNode, $variables = [])
{
$map = [
"now" => now(),
];
if (!array_key_exists($valueNode->value, $map)) {
throw new Error(
"Query error: Invalid custom value",
$valueNode
);
}
return $map[$valueNode->value];
}
}
如果需要,您可以添加要默认设置的其他值。这是它的用法示例:
input CustomerInput {
id: ID
name: String!
date: CustomValue = "now"
}