Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Graphql 如何解析Lighthouse中的JSON数据?_Graphql_Laravel Lighthouse - Fatal编程技术网

Graphql 如何解析Lighthouse中的JSON数据?

Graphql 如何解析Lighthouse中的JSON数据?,graphql,laravel-lighthouse,Graphql,Laravel Lighthouse,我有一个通知表,其中数据字段存储为json: {"data":{"description":"Event Status has been changed to pending","event_id":19}} 我得到这个错误 "errors": [ { "debugMessage": "Expected a value of type \"String

我有一个通知表,其中数据字段存储为json:

{"data":{"description":"Event Status has been changed to pending","event_id":19}}
我得到这个错误

"errors": [
{
  "debugMessage": "Expected a value of type \"String\" but received: {\"data\":{\"description\":\"Event Status has been changed to pending\",\"event_id\":19}}",
我已尝试在通知模型上添加以下内容:

公共函数getDataAttribute($data)
{
返回json_decode($data,true);
}
但没有解决办法


我尝试在GraphQL模式中使用
[String]
,但没有任何结果。

如果希望以字符串形式返回任意JSON数据,有两个选项:

第一种是使用JSON标量,您可以构建自己的标量,也可以使用JSON标量。它将数据编码为有效的JSON字符串

第二个选项是确保只返回JSON而不是解码的JSON。我假设您的数据已经解码为JSON,因为您正在使用,如果没有,您可以删除
JSON\u decode
调用。您可以添加一个getter将其重新编码回JSON,或者添加一个getter从模型的attributes属性中获取值

公共函数getRawDataAttribute():字符串 { 返回$this->attributes['data']; } //或 公共函数getRawDataAttribute():字符串 { 返回json_encode($this->data); } 您可以在模式中这样使用它:

type MyType {
    data: String! @rename(attribute: "raw_data")
}

但在我看来,第一个选项绝对是最简单也是最好的,因为它在模式中正确地指示字段包含JSON,并为您处理编码(以及在输入中使用时的解码)。

您正在尝试以单个字符串的形式返回graphql中的object/array/record-[complex]单独类型?不可能。。。使用自定义json标量?