symfony - 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;
从graphiql
,createContact
输入如下所示:
user: String
email: String!
familyName: String
givenName: String
displayName: String
clientMutationId: String
解决方案
我是这样做的:
创建自定义类型:
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
}
]
}
}
推荐阅读
- mysql - MySQL系统数据库损坏
- javascript - 当我尝试在最后给 url 一个变量时,jquery 被第二个 ajax 调用未定义
- java - 即使多次创建类的对象,如何只调用一次类中的方法
- html - 如何在“普通”文本之后在 CSS 中制作闪烁的“光标/矩形”?
- jquery - 更改时多选它在警报中显示值,但在 ajax 发布请求中显示无
- javascript - 如何将灰度应用于 IE11 中包含多个图像的 html 元素?
- performance - x86 MESI 无效缓存线延迟问题
- ruby-on-rails - 下拉菜单不起作用 - 引导程序 - ruby on rails
- r - R中的ggplot binwidth
- mongodb - 数据库结构偏好