Api-Platform graphql – create multiple items in one mutation

I have an User entity that can add his/hers contacts from Google using GooglePeopleApi.

The API provides an array of contacts to the front-end as it is (Nextjs). The question is how to insert all those contacts in one single mutation.

I could, of course, have the front-end loop through the array and post the contacts one by one, but that is a bit silly.

It should be possible to create a type array with the Contact input and then, with that, set up a customArgsMutation. ( I’ve seen some examples of that from Hasura ).

Entity wise, it looks like this for now ( only relevant code ):

User.php

 ....
/**
 * @ORMOneToMany(targetEntity="AppEntityContact", mappedBy="user")
 * @Groups({"put-contacts", "get-admin", "get-owner"})
 */
private $contacts;

Contact.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"}},
*          },
*      }
* )
* @ORMEntity(repositoryClass="AppRepositoryContactRepository")
*/
class Contact
{
/**
 * @ORMId()
 * @ORMGeneratedValue()
 * @ORMColumn(type="integer")
 */
private $id;

/**
 * @ORMManyToOne(targetEntity="AppEntityUser", inversedBy="contacts")
 * @Groups({"post", "get-admin", "get-owner"})
 */
private $user;

/**
 * @ORMColumn(type="string", length=180)
 * @Groups({"post", "put", "get-admin", "get-owner"})
 */
private $email;

/**
 * @ORMColumn(type="string", length=180, nullable=true)
 * @Groups({"post", "put", "get-admin", "get-owner"})
 */
private $familyName;

/**
 * @ORMColumn(type="string", length=180, nullable=true)
 * @Groups({"post", "put", "get-admin", "get-owner"})
 */
private $givenName;

/**
 * @ORMColumn(type="string", length=180, nullable=true)
 * @Groups({"post", "put", "get-admin", "get-owner"})
 */
private $displayName;

From graphiql, the createContact input looks like this:

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

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *