簡體   English   中英

如何在 apiplatform/graphql 上創建/變異具有子關系的實體

[英]How to create/mutation entity with sub relation on apiplatform/graphql

我想創建兩個相關的實體。 如何使用所需的子實體創建第一個實體。

我嘗試了以下代碼,但 graphql 返回以下錯誤:

{
  "errors": [
    {
      "message": "Variable \"$stock\" of type \"createProductInput!\" used in position expecting type \"String\".",
      "extensions": {
        "category": "graphql"
      },
      "locations": [
        {
          "line": 7,
          "column": 3
        },
        {
          "line": 15,
          "column": 17
        }
      ]
    }
  ]
}

突變:

mutation createProduct ($input: createProductInput!) {
  createProduct(input: $input) {
    clientMutationId

    product {
      uuid
      name
      sku
    }
  }
}

變量:

{
  "input": {
    "name": "ProductAAA",
    "sku": "product_aaa",
    "stock": {
      "quantity": 33,
      "unit": "s"
    }
  }
}

奇怪的是 createProductInput 說 stock 是一個字符串而不是一個對象。

uuid: String!
name: String!
sku: String!
stock: String
clientMutationId: String

這些是我的實體:

// Product.php

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource
 * @ApiFilter(ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter::class, properties={"name": "partial", "sku": "partial"})
 *
 * @ORM\Table(name="products")
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\Column(name="product_id", type="uuid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @ApiProperty(identifier=true)
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     * @ORM\Column(type="string")
     */
    private $sku;

    /**
     * @ORM\ManyToOne(targetEntity="Stock", cascade={"PERSIST"})
     * @ORM\JoinColumn(name="stock_id", referencedColumnName="stock_id")
     *
     * @ApiSubresource
     */
    private $stock;
}
// Stock.php

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource
 *
 * @ORM\Table(name="stocks")
 */
class Stock
{
    /**
     * @ORM\Id
     * @ORM\Column(name="stock_id", type="uuid", unique=true)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     *
     * @ApiProperty(identifier=true)
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $quantity;

    /**
     * @ORM\Column(type="string")
     */
    private $unit;
}

您不能在突變中創建嵌套實體,您需要先創建嵌套實體,然后在突變中使用其 IRI。 這就是類型為String的原因。

以前是可能的,但已被刪除,因為它導致了一些問題。 見: https : //github.com/api-platform/core/pull/1886

根據您的問題,我通過自己的突變創建了第一個實體。 然后,當我想在第二個中使用它時,什么也沒發生。 “我的購物車商品”保留不變,未鏈接任何海報。 我不明白為什么。 這是我的代碼:

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\CartRepository")
 */
class Cart
{
    /**
     * @ApiSubresource
     * @ORM\OneToMany(targetEntity="Poster", mappedBy="cart")
     */
    private $posters;
}

GraphQL突變:

mutation AddCart(
    $posters: [String]!
  ) {
    createCart(input: {
      posters: $posters
    }) {
      cart {
        id,
        posters {
          edges {
            node {
              id
            }
          }
        }
      }
    }
  }

變量:

{
  "posters": [
    "/api/posters/4733d25c-c37b-4162-b25b-fabb3e58177e"
  ]
}

我顯然在某個地方犯了一個錯誤:)你認為你可以幫助我嗎? 非常感謝!

mutation CreateUser{
  createUser(input: {
    email: "user@test.com", 
    username: "user@test.com", 
    role: "ROLE_USER",
    password: "bonjour",
    enabled: true,
  }) {
    user{
        id
      email
      username
      role
      reference
      enabled
      created
      updated
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM