首页 > 解决方案 > Api-Platform graphql - 在一个突变中创建多个项目

问题描述

我有一个用户实体,可以使用 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;

graphiqlcreateContact输入如下所示:

 user: String
 email: String!
 familyName: String
 givenName: String
 displayName: String
 clientMutationId: String

标签: symfonygraphqlapi-platform.comgraphql-mutation

解决方案


我是这样做的:

创建自定义类型:

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!]"}
*         }
*     }
*)

然后可以使用如下参数数组调用新的 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
            }
        ]
    }
}

推荐阅读