【问题标题】:mutation doesn't save ralation id突变不保存关系ID
【发布时间】:2021-10-13 04:30:18
【问题描述】:

我对@9​​87654324@ 上的lighthouse graphql 有疑问。

查询或简单的创建/更新一切都很好,但是当涉及到关系时就变得困难了。

它不保存关系。

我的例子:

模型

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::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 文件

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


input CreateProduct {
    manufacturer: CreateProductManufacturerRel
}

input CreateProductManufacturerRel {
    connect: ID
    create: CreateProductManufacturer
}

input CreateProductManufacturer{
    name: String!
}

查询发送到服务器

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

此查询创建新产品和制造商,但不保存id_product_manufacturer 进入products 表。

我已经尝试过的:

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

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

但我不知道在哪里以及如何创建这个resolver function。 这些例子解释不多。

【问题讨论】:

    标签: laravel graphql laravel-lighthouse


    【解决方案1】:

    您应该使用相同的关系名称,并将关系类型也附加到相关输入中。例如,如果 Product 有一个 manufacturer,则输入名称应为:CreateManufacturerHasOne

    所以graphql文件应该是这样的:

    extend type Mutation {
        createProduct(
           input: CreateProduct! @spread
        ): Product @create
    }
    
    
    input CreateProduct {
        manufacturer: CreateManufacturerHasOne
    }
    
    input CreateManufacturerHasOne {
        connect: ID
        create: CreateManufacturerInput
    }
    
    input CreateManufacturerInput {
        name: String!
    }
    

    更多关于https://lighthouse-php.com/master/eloquent/nested-mutations.html#hasone的信息。

    【讨论】:

      【解决方案2】:

      感谢您的回复。 不幸的是,这两个答案都不起作用。

      还是一样,发送请求后id_product_manufacturer为空。

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

      【讨论】:

        【解决方案3】:

        好的。我解决了问题。 问题在于 laravel 模型中的关系类型。

        这行得通:

        模型

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

        【讨论】:

          猜你喜欢
          • 2020-06-20
          • 2016-10-17
          • 2020-04-06
          • 1970-01-01
          • 1970-01-01
          • 2013-06-04
          • 1970-01-01
          • 2018-12-25
          • 1970-01-01
          相关资源
          最近更新 更多