pageballoon / headstart

Decoupled GraphQL-driven content management using OctoberCMS
https://octobercms.com/plugin/nocio-headstart
MIT License
7 stars 2 forks source link

nested mutations not update the relation field. #13

Closed dev-dx closed 3 years ago

dev-dx commented 3 years ago

Hi,

I try to implement nested mutation using this plugin. But it seems the nested mutation is not executed. Only the parent Type was updated.

I notice that on the lighthouse documentation mention like this :

Return Types Required

You have to define return types on your relationship methods so that Lighthouse can detect them.

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

}

My question is, is nested mutations working using your plugin ?

frthjf commented 3 years ago

Hi, Good question. I've never explicitly tested that and I'm not sure how the Lighthouse detection mechanism works in that case. One thing to say though it that October's way of declaration of relationships works a bit differently than in pure Laravel (i.e. using an array property rather than methods). How did you define your relationship in your October model?

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

would have to be something like

class Post extends Model
{
    public $hasMany = [
        'comments' => 'Acme\Blog\Models\Comment'
    ];
}

in October.

dev-dx commented 3 years ago

I extend Rainlab user to have more detail profile like this

    UserModel::extend( function($model) {
        $model->hasOne['profile'] = ['Divertx\Profile\Models\Profile'];
        $model->hasMany['schools'] = ['Divertx\Profile\Models\ProfileSchool'];
        $model->hasMany['works'] = ['Divertx\Profile\Models\ProfileWork'];
        $model->belongsToMany['hobbies'] = [\Divertx\Profile\Models\Hobby::class, 'table' => 'divertx_profile_profilehobbies'];
    });
frthjf commented 3 years ago

This looks right to me. I suspect that Lighthouse uses the type annotation BelongsTo to infer the relationship type which wouldn't work with October's models. Could you work around this by applying the update explicitly in the resolver?

dev-dx commented 3 years ago

Yes, i think it is possible to using specific resolver.

But i like the way how nested mutation work. So, finally i hack the code on the vendor/nuwave/lighthouse/src/execution/MutationExecutor.php to comply with october class model

so far it work.

frthjf commented 3 years ago

Great! Would you like to share the code modifications here? There may be a way to incorporate them into Headstart or even contribute them upstream.

dev-dx commented 3 years ago

Hi,

Here the code

`<?php

namespace Nuwave\Lighthouse\Execution;

use ReflectionClass; use ReflectionNamedType; use Illuminate\Support\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class MutationExecutor { /**