【问题标题】:Laravel Lighthouse GraphQL array to string errorLaravel Lighthouse GraphQL 数组到字符串错误
【发布时间】:2022-12-12 14:03:11
【问题描述】:

为什么在 Lighthouse 文档中列出并解释了上传指令,但试图在模式中使用它:

scalar Upload @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\Upload")


extend type Mutation {
    createImageList(
        id: ID!
        category_id: String!
        image_path: [Upload!]! @upload(disk: "public", path: "images/gallery", public: true)
        thumbnail: String!
        label: String
        description: String
        tags: JSON
        favorite_count: Int
    ): GalleryImageList @upsert
}

回报:

"debugMessage": "No directive found for upload"

后来查看了Lighthouse的changelog,发现在v5.6中增加了upload指令。 使用 composer require nuwave/lighthouse 添加了 Lighthouse 的 v5.58,这是一个意外的行为。

我终于使用了: composer require nuwave/lighthouse dev-master 哪个更新了Lighthouse到最新版v5.68+

现在,我得到了一个新的错误:

"debugMessage": "Array to string conversion",
            "message": "Internal server error"

我希望尽快找到一种使用上传指令上传文件的方法。

【问题讨论】:

  • 为此创建一个自定义突变
  • 我已经尝试了自定义突变,并在模式中进行了导入:scalar Upload @scalar(class: "App\\GraphQL\\Mutations\\Upload") 但是 laravel 找不到该类。我如何让它发挥作用?
  • 现在,它可以看到类。我收到以下错误:"message": "Variable \"$image_path\" got invalid value [{},{},{}]; Expected type Upload; Could not get uploaded file, be sure to conform to GraphQL multipart request specification: https:\/\/github.com\/jaydenseric\/graphql-multipart-request-spec Instead got: [{},{},{}]", 这与我从 [Upload!]! to Upload! 更改字段类型时遇到的错误相同
  • 更新我发现使用字段类型:[Upload!]! 确实有效,因为图像已上传到公共驱动器中指定的文件夹,尽管它仍然给出以下错误,并且数据库未按照突变中所述更新:@ 987654330@
  • 我相信你不应该使用[Upload!]!,而应该只使用Upload!

标签: laravel graphql laravel-lighthouse


【解决方案1】:

==== 已解决 ==== 将 json cast 添加到 image_path 字段,wolla,成功了!它返回以下内容,上传了 3 张图像,并将生成的文件名保存到数据库中。 ["images/gallery/LimSUx2xO9yvLYZg0Pl8L05MRcFISnLTprx1hOUq.jpg","images/gallery/3k8NSpOJFu6oYxplMW7Bik2j12mmVsHcOK1ZM4qJ.jpg","images/gallery/lRnpeAquOItYPZ35jPL2rBBDojWdxM2JkKosJ7Su.jpg"]

我仍在测试 api 上上传图片的 url:

localhost/images/gallery/LimSUx2xO9yvLYZg0Pl8L05MRcFISnLTprx1hOUq.jpg

退货: 404 Not Found

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 2016-09-01
    • 2022-01-25
    • 1970-01-01
    • 2020-10-19
    • 2014-12-14
    • 2018-09-11
    • 1970-01-01
    相关资源
    最近更新 更多