繁体   English   中英

Laravel Lighthouse Graphql HasOne 嵌套关系不起作用

[英]Laravel Lighthouse Graphql HasOne nested relation not working

我试图让 HasOne 关系在 Laravel Lighthouse GraphQL 中工作。
它不起作用。 突变不会给出错误,但也不会更新关系(员工 ID 保持不变,而我希望将其更改为 2):

mutation {
  updateAccount(input: {
    id: 12
    employee: {
      connect: 2
    }
  }) {
    id
    employee {
      id
    }
  }
}

我的架构如下所示:

type Mutation {
    updateAccount(input: UpdateAccount! @spread): Account! @update
}

input UpdateAccountInput {
    id: ID!
    employee: CreateEmployeeRelationInput @hasOne
}

input CreateEmployeeRelationInput {
    connect: ID!
}

type Account {
    id: ID!
    ...
    employee: Employee! @hasOne
}

type Employee {
    id: ID!
    ...
    account: Account @belongsTo
}

Account 和 Employee 模型如下所示:

class Account extends Model
{
    public function employee(): HasOne
    {
        return $this->hasOne(\App\Models\Employee::class, 'account_id');
    }
}

class Employee extends Model
{
    public function account(): BelongsTo
    {
        return $this->belongsTo(\App\Models\Account::class, 'account_id');
    }
}

任何帮助或建议将不胜感激。
到目前为止,我已经尝试添加/删除@hasOne ,或更改ID! [ID!]! ,但没有任何作用。

我发现 Lighthouse 没有提供我想要的功能,因为 Laravel 没有提供,而 Lighthouse 试图反映 Laravel 的功能。

更多信息可以在这个问题的答案中找到: https : //github.com/nuwave/lighthouse/issues/1109#issuecomment-570544848

简而言之:
hasOne关系具有associate()dissociate()方法。
hasMany关系具有sync() attach()detach()
但是belongsTo关系确实associate()dissociate()

因此,如果一个帐户有一个员工,我就不会这样做:

mutation {
  updateAccount(input: {
    id: 12
    employee: {
      connect: 2
    }
  }) {
    id
    solis_id
    employee {
      id
    }
  }
}

此外,如果帐户的hasMany员工,我不能做到这一点:

mutation {
  updateAccount(input: {
    id: 12
    employees: {
      connect: [2, 3]
    }
  }) {
    id
    solis_id
    employees {
      id
    }
  }
}

但是我可以做些什么来达到同样的结果:

mutation {
  updateEmployee(input: {
    id: 2
    account: {
      connect: 2
    }
  }) {
    id
    account {
      id
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM