[英]Api-Platform graphql - create multiple items in one mutation
我有一个用户实体,可以使用 GooglePeopleApi 从 Google 添加他/她的联系人。
API 按原样向前端提供一组联系人 (Nextjs)。 问题是如何将所有这些联系人插入一个单一的突变中。
当然,我可以让前端循环遍历数组并一个接一个地发布联系人,但这有点傻。
应该可以使用 Contact 输入创建一个类型数组,然后设置一个customArgsMutation
。 (我从 Hasura 那里看到了一些这样的例子)。
实体方面,现在看起来像这样(只有相关代码):
用户.php
....
/**
* @ORM\OneToMany(targetEntity="App\Entity\Contact", mappedBy="user")
* @Groups({"put-contacts", "get-admin", "get-owner"})
*/
private $contacts;
联系人:php
/**
* @ApiResource(
* attributes={"pagination_enabled"=false},
* graphql={
* "item_query"={
* "normalization_context"={"groups"={"get-admin", "get-owner"}},
* },
* "collection_query"={
* "normalization_context"={"groups"={"get-admin", "get-owner"}},
* },
* "delete"={"security"="is_granted('IS_AUTHENTICATED_FULLY') and object.getUser() == user"},
* "create"={
* "security"="is_granted('IS_AUTHENTICATED_FULLY')",
* "denormalization_context"={"groups"={"post", "put"}},
* "normalization_context"={"groups"={"get-owner", "get-admin"}},
* },
* }
* )
* @ORM\Entity(repositoryClass="App\Repository\ContactRepository")
*/
class Contact
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="contacts")
* @Groups({"post", "get-admin", "get-owner"})
*/
private $user;
/**
* @ORM\Column(type="string", length=180)
* @Groups({"post", "put", "get-admin", "get-owner"})
*/
private $email;
/**
* @ORM\Column(type="string", length=180, nullable=true)
* @Groups({"post", "put", "get-admin", "get-owner"})
*/
private $familyName;
/**
* @ORM\Column(type="string", length=180, nullable=true)
* @Groups({"post", "put", "get-admin", "get-owner"})
*/
private $givenName;
/**
* @ORM\Column(type="string", length=180, nullable=true)
* @Groups({"post", "put", "get-admin", "get-owner"})
*/
private $displayName;
在graphiql
中, createContact
输入如下所示:
user: String
email: String!
familyName: String
givenName: String
displayName: String
clientMutationId: String
您在这里有几个选项,具体取决于您想要的并发量:
客户端可以使用多个突变作为别名发出单个 HTTP 请求:
mutation CreateUsers {
user1: createUser({ //userInput1 }) { ...userFields }
user2: createUser({ //userInput2 }) { ...userFields }
user3: createUser({ //userInput3 }) { ...userFields }
}
fragment userFields on CreateUserPayload {
firstName
// etc
}
响应将如下所示:
{
"data": {
"user1": {...},
"user2": {...},
"user3": {...},
}
}
优点:
缺点:
创建一个允许您创建多个用户的“批量突变”是一种常见的做法(我猜这很常见)。
mutation CreateUsers {
createUsers([
{ //userInput1 },
{ //userInput2 },
{ //userInput3 }
]) {
firstName
// etc
}
}
优点:
缺点:
我就是这样做的:
创建自定义类型:
class CustomType extends InputObjectType implements TypeInterface
{
public function __construct()
{
$config = [
'name' => 'CustomType',
'fields' => [
'id_1' => Type::nonNull(Type::id()),
'id_2' => Type::nonNull(Type::id()),
'value' => Type::string(),
],
];
parent::__construct($config);
}
public function getName(): string
{
return 'CustomType';
}
}
在 src/config/services.yaml 中注册 CustomType
(...)
App\Type\Definition\CustomType:
tags:
- { name: api_platform.graphql.type }
将自定义突变添加到实体,使用 CustomType 参数作为数组:
/**
* @ApiResource(
* (...)
* graphql={
* "customMutation"={
* "args"={
* (...)
* "customArgumentName"={"type"=[CustomType!]"}
* }
* }
*)
然后可以使用 arguments 数组调用新的 customMutation,如下所示:
mutation myMutation($input: customMutationEntitynameInput!) {
customMutationEntityname(input: $input) {
otherEntity {
id
}
}
}
和 graphql 个变量:
{
"input": {
"customArgumentName": [
{
"id_1": 3,
"id_2": 4
},{
"id_1": 4,
"id_2": 4
}
]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.