I want to create two entities that are related. How can I create the first entity with the required sub-entity.
I've tried the following code but graphql returns the following error:
{
"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
}
]
}
]
}
The mutation:
mutation createProduct ($input: createProductInput!) {
createProduct(input: $input) {
clientMutationId
product {
uuid
name
sku
}
}
}
the variables:
{
"input": {
"name": "ProductAAA",
"sku": "product_aaa",
"stock": {
"quantity": 33,
"unit": "s"
}
}
}
Oddly the createProductInput says that stock is a string instead of an object.
uuid: String!
name: String!
sku: String!
stock: String
clientMutationId: String
These are my entities:
// 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;
}
You cannot create a nested entity in a mutation, you need to create the nested entity first then use its IRI in the mutation. That's why the type is String .
It was possible before but has been removed because it was causing some issues. See: https://github.com/api-platform/core/pull/1886
following your question I created the first entity through its own mutation. Then when I want to use it in the second one, nothing happen. My Cart Item is persisted without any Poster linked to it. I don't understand why. Here are my codes :
/**
* @ApiResource()
* @ORM\Entity(repositoryClass="App\Repository\CartRepository")
*/
class Cart
{
/**
* @ApiSubresource
* @ORM\OneToMany(targetEntity="Poster", mappedBy="cart")
*/
private $posters;
}
GraphQL mutation:
mutation AddCart(
$posters: [String]!
) {
createCart(input: {
posters: $posters
}) {
cart {
id,
posters {
edges {
node {
id
}
}
}
}
}
}
Variables:
{
"posters": [
"/api/posters/4733d25c-c37b-4162-b25b-fabb3e58177e"
]
}
I obviously did a mistake somewhere :) Do you think you can help me? Thanks a lot!
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
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.