1

I have a problem with lighthouse graphql on laravel.

Everything works great with queries or simple create/update, but when it comes to relations it becomes hard.

It doesn't save relation.

My example:

Models

class Product extends Model
{
    public $timestamps = false;
    protected $table = 'products';
    protected $primaryKey = 'id_product';

    public function manufacturer(): HasOne
    {
        return $this->hasOne(Manufacturer::class, 'id_product_manufacturer', 'id_product_manufacturer');
    }

}

class Manufacturer extends Model
{

    protected $table = 'product_manufacturer';
    protected $primaryKey = 'id_product_manufacturer';
    protected $fillable = ['name'];


    public function products(): BelongsTo
    {
        return $this->belongsTo(Product::class, 'id_product_manufacturer', 'id_product_manufacturer');
    }
}

sql schema

Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id_product');
            $table->bigInteger('id_product_manufacturer')->unsigned()->index()->nullable();
});

Schema::create('product_manufacturer', function (Blueprint $table) {
            $table->bigIncrements('id_product_manufacturer');
            $table->string('name', 64);
});

graphql file

extend type Mutation {
    createProduct(
       input: CreateProduct! @spread
    ): Product @create
}


input CreateProduct {
    manufacturer: CreateProductManufacturerRel
}

input CreateProductManufacturerRel {
    connect: ID
    create: CreateProductManufacturer
}

input CreateProductManufacturer{
    name: String!
}

query sended to server

mutation {
    createProduct(
        input: {
            manufacturer: {
                create: {
                    name: "test"
                }
            }
        }
    ){
        id_product
    }
}

This query create new product and manufacturer but it doesn't save id_product_manufacturer into products table.

What i have already tried:

https://lighthouse-php.com/master/eloquent/nested-mutations.html#return-types-required

and

https://lighthouse-php.com/master/concepts/arg-resolvers.html#solution

but i don't know where and how create this resolver function. These examples do not explain much.

burasuk
  • 162
  • 2
  • 8

3 Answers3

0

You should use the same relation name and also append the relation type in your related input too. For example if a Product has one manufacturer the input name should be: CreateManufacturerHasOne.

So the graphql file should be like this:

extend type Mutation {
    createProduct(
       input: CreateProduct! @spread
    ): Product @create
}


input CreateProduct {
    manufacturer: CreateManufacturerHasOne
}

input CreateManufacturerHasOne {
    connect: ID
    create: CreateManufacturerInput
}

input CreateManufacturerInput {
    name: String!
}

More information on https://lighthouse-php.com/master/eloquent/nested-mutations.html#hasone.

Mohsen Nazari
  • 1,281
  • 1
  • 3
  • 13
0

Thanks for response. Unfortunately, neither answer works.

Still the same, id_product_manufacturer is null after send request.

mutation {
    createProduct(
        input: {
            manufacturer: {
                create: {
                    name: "test"
                }
            }
        }
    ){
        id_product
    }
}

enter image description here

burasuk
  • 162
  • 2
  • 8
0

Ok. I solved problem. Problem was in relationship types in laravel models.

This works:

models

class Product extends Model
{
    public $timestamps = false;
    protected $table = 'products';
    protected $primaryKey = 'id_product';

    public function manufacturer(): BelongsTo
    {
        return $this->belongsTo(Manufacturer::class, 'id_product_manufacturer', 'id_product_manufacturer');
    }

}

class Manufacturer extends Model
{

    protected $table = 'product_manufacturer';
    protected $primaryKey = 'id_product_manufacturer';
    protected $fillable = ['name'];


    public function products(): HasMany
    {
        return $this->hasMany(Product::class, 'id_product_manufacturer', 'id_product_manufacturer');
    }
}
burasuk
  • 162
  • 2
  • 8