3

I am trying to get a HasOne relation to work in Laravel Lighthouse GraphQL.
It doesn't work. The mutation does not give an error, but also doesn't update the relation (the employee id stays the same, while I want it to change to 2):

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

My schema looks like this:

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
}

The Account and Employee models looks like this:

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');
    }
}

Any help or suggestions would be appreciated.
So far I've tried to add/remove @hasOne, or change ID! to [ID!]!, but nothing works.

Hendrik Jan
  • 4,396
  • 8
  • 39
  • 75
  • What do your `Account` and `Employee` types look like? – Nathan Dec 31 '19 at 17:42
  • I've added the ```Account``` and ```Employee``` types to the question (only the fields that seem relevant). Would these types make any difference? – Hendrik Jan Dec 31 '19 at 18:32
  • The only thing I can see in your code that doesn't match mine is your use of the `@hasOne` directive in the input. – Nathan Dec 31 '19 at 19:28
  • Are you sure it is the same? If I read the documentation correctly, what I try to do is not yet possible in Lighthouse. I've opened an issue here: https://github.com/nuwave/lighthouse/issues/1109 . – Hendrik Jan Jan 01 '20 at 08:45

1 Answers1

0

I found out that Lighthouse does not provide the functionality that I wanted, because Laravel does not provide it, and Lighthouse tries to mirror the Laravel functionality.

More information can be found in this answer to this issue: https://github.com/nuwave/lighthouse/issues/1109#issuecomment-570544848

In short:
A hasOne relation does not have an associate() or dissociate() method.
A hasMany relation does not have sync(), attach() or detach().
But the belongsTo relation does have associate() and dissociate().

Therefore if an account hasOne employee, I con not do this:

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

Also if an Account hasMany employees, I can not do this:

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

But what I can do to achieve the same result is this:

mutation {
  updateEmployee(input: {
    id: 2
    account: {
      connect: 2
    }
  }) {
    id
    account {
      id
    }
  }
}
Hendrik Jan
  • 4,396
  • 8
  • 39
  • 75