【问题标题】:Nested mutations don't seem to work in Lighthouse 3.7嵌套突变似乎在 Lighthouse 3.7 中不起作用
【发布时间】:2019-11-21 16:54:34
【问题描述】:

我正在尝试在 Lighthouse 3.7/Laravel 5.8 中设置架构 我想要实现的是: 用户应该能够创建列表。 User 和 Clist 之间存在一对多关系。 我正在尝试按照here 的描述实现嵌套突变。

我已经实现了“查询”部分,它工作正常。 但是当我在 GraphQL Playground 中测试 createClist 突变时,我得到了这个错误:

"debugMessage": "Array to string conversion",

"message": "Internal server error",

"extensions": {
        "category": "internal"
      },
...

而且我不知道自己做错了什么。

这是我的代码:

type Mutation {
  createClist(input: CreateClistInput! @spread): Clist @create
}

input CreateClistInput {
    name: String!
    description: String
    starred: Boolean
    user: CreateUserRelation!
    ctags: CreateCtagRelation
}

input CreateUserRelation {
  connect: ID!
}

input CreateCtagRelation {
  create: [CreateCtagInput!]
  connect: [ID!]
  sync: [ID!]
}

input CreateCtagInput {
  name: String!
}


这是 GraphQL Playground 的截图:

【问题讨论】:

  • 你能展示你的模型吗?使用 Lighthouse @spread 指令时,需要键入提示关系的返回类型。 lighthouse-php.com/3.7/eloquent/…
  • 就是这样!有用。非常感谢!
  • 我把它作为答案发布了,所以你可以接受它:)

标签: laravel graphql laravel-lighthouse


【解决方案1】:

使用@spread 指令时,需要对模型中的关系进行类型提示。

取自docs有以下例子:

use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Post extends Model 
{
    // WORKS
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    // DOES NOT WORK
    public function comments()
    {
        return $this->hasMany(Comment::class);        
    }
}

Lighthouse 使用类型提示来确定它应该如何处理这种关系。

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 2011-09-14
    • 1970-01-01
    • 2020-04-20
    • 2014-06-26
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多