Skip to content

Navigation Menu

Sign in
Sign up

Nested Mutation with Pivot model #2618

Unanswered
rafapaulin asked this question in Q&A
Discussion options

Hey guys. So, I am posting this on the discussions, since I am not exactly sure if this is a bug (which seems so) or if it is just something that I am missing.

Consider the following schema:

input UpsertContact {
 id: ID
 name: String
 email: String
 phoneType: PhoneType
 phone: String
 isPhoneWpp: Boolean
 notes: String
}
input ContactBelongsTo {
 upsert: UpsertContact
 delete: ID
}
type SupplierAdditionalInfo {
 contact: Contact
 notes: String
}
input UpsertSupplierAdditionalInfo {
 id: ID
 notes: String
 contact: ContactBelongsTo
}
input CustomerManageSupplierBelongsToMany {
 connect: [UpsertSupplierAdditionalInfo]
 disconnect: [ID]
}
input CustomerManageSupplier {
 id: ID
 suppliers: CustomerManageSupplierBelongsToMany
}
type Mutation @guard @isEmailVerified @isUserActive {
 customerManageSupplier(input: CustomerManageSupplier! @spread): Company!
 @upsert
}

...and the following Eloquent Models:

class Company extends Model {
 public function suppliers(): BelongsToMany {
 return $this
 ->belongsToMany(Company::class, 'customer_supplier', 'customer_id', 'supplier_id')
 ->using(CustomerSupplier::class)
 ->withPivot(['contact_id', 'notes']);
 }
}
class CustomerSupplier extends Pivot {
 protected $fillable = [
 'customer_id',
 'supplier_id',
 'contact_id',
 'notes',
 ];
 public function contact(): BelongsTo {
 return $this->belongsTo(Contact::class);
 }
}

I am running the following mutation:

 customerManageSupplier(input: {
 id: $customerId
 suppliers: {
 connect: [{
 id: $supplierId
 notes: "holly crap"
 contact: {
 upsert: {
 name: "papum"
 email: "pa@pum.bola"
 }
 }
 }]
 }
 }) {
 id
 name
 suppliers {
 id
 name
 additionalInfo {
 notes
 contact {
 name
 email
 }
 }
 }
 }

The expected result would be:
1- Customer and supplier connected through the pivot table customer_suplier
2- notes added to pivot data on customer_suplier
3- A Contact created (upserted)
4- The new Contact associated with the customer_supplier relationship (id added to contact_id column on customer_supplier pivot table)

With the schema/models and the mutation I described above, I got 1 and 2 working, but 3 and 4 does not happen, and I am not being able to figure out why.

(Just as a side note: I have another "double-nested" mutation that uses that ContactBelongsTo input, which works. customer > users > create > contact > upsert)

It seems that for some reason nested mutations does not get through Pivot models (Illuminate\Database\Eloquent\Relations\Pivot), even though the correct relationship is set.

(btw, query that relationship works as intended)

You must be logged in to vote

Replies: 1 comment 4 replies

Comment options

You can split Company->belongsToMany(Company) into Company->hasMany(CustomerSupplier) and CustomerSupplier->belongsTo(Company) and structure your nested mutations after that.

You must be logged in to vote
4 replies
Comment options

Nice! That workaround would definetly work =) I'll go for it to avoid getting stuck. Thanks @spawnia =)

But, shouldn't that nested mutation with the custom pivot model work?

If so, maybe if you direct me where to look more or less where to look, i can try to pull request a fix for it =)

Comment options

Hmm, to create the relationship the workaround did the trick, but the delete (just for the CustomerSupplier) did not, since I do not have an ID on its pivot table.

Is there a way to indicate which should be the primary key on that case?

Comment options

🤔 I gave it a try, and it proved more complex of a problem than anticipated. it may take a while

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /