【发布时间】:2018-03-19 00:35:55
【问题描述】:
我有一个带有日期字段archivedAt 的 Laravel 模型。这已在模型上的$dates 数组中设置,如下所示:
联系模特
class Contact extends Model
{
/**
* The name of the table in the database.
*
* @laravel-table-name
*
* @var string
*/
protected $table = 'Contacts';
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'createdAt',
'updatedAt',
'archivedAt'
];
}
当我尝试通过 GraphQL 向服务器发送 JSON 有效负载时,我收到以下响应:
{
"data": {
"editContact": null
},
"errors": [
{
"message": "Unexpected data found.\nTrailing data",
"locations": [
{
"line": 2,
"column": 3
}
]
}
]
}
当然,Laravel 的 GraphQL 库很糟糕,并且不包含完整的堆栈跟踪或任何其他信息,但这似乎是我设置 archivedAt 字段的问题。以下是我的做法:
{
"operationName": null,
"variables": {
"contactEditForm": {
"id": "2",
"name": "Maybelle Collier",
"email": "cletus49@example.org",
"phone": "+1 (997) 381-8483",
"archivedAt": "2018-03-19T00:07:57.191Z",
"createdAt": "2018-03-18 23:57:30",
"updatedAt": "2018-03-18 23:57:30"
}
},
"query": "mutation ($contactEditForm: ContactEditForm!) {\n editContact(contactEditForm: $contactEditForm) {\n id\n name\n __typename\n }\n}\n"
}
如您所见,archivedAt 是一个 ISO8601 格式的字符串,它是在我的客户端上生成的:
(new Date())->toISOString();
然后应该将其保存在带有突变的 GraphQL 服务器上,如下所示:
public function resolve($root, $args, $context, ResolveInfo $info)
{
$contact = Contact::find($args['contactEditForm']['id']);
$contact->fill($args['contactEditForm']);
$contact->save();
return $contact;
}
为什么 Laravel 不喜欢这样?
【问题讨论】:
-
Laravel 中的所有日期都被视为碳日期。阅读Carbon docs
-
@Maramal 是的,我知道这一点。
-
我认为我们需要您的后端代码(在控制器方面)来理解问题。
-
@Maramal GraphQL 没有控制器的概念。我已在填充联系人的位置附加了突变解析器。
标签: javascript laravel date iso8601