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