ApiPlatform best way to convert to exist entity on mutation graphql

ApiPlatform best way to convert to exist entity on mutation graphql


0

I have this entitys for user

#[UniqueEntity(fields: ['email'], message: 'email is exists')]
#[ORMEntity]
#[ORMTable(name: 'user')]
#[ApiPlatformApiResource(
    operations: [],
    graphQlOperations: [
        new ApiPlatformGraphQlQuery(),
        new ApiPlatformGraphQlMutation(
            name: 'create',
            denormalizationContext: ['groups' => ['request:create', 'shopping:create']],
        ),
    ]
)]
class User
{
    #[ORMId]
    #[ORMGeneratedValue(strategy: 'AUTO')]
    #[ORMColumn(type: 'integer')]
    #[SerializerExpose]
    protected $id;

    #[AssertEmail(message: 'email not valid')]
    #[ORMColumn(type: 'string', unique: true, length: 150, nullable: true)]
    #[Groups(['request:create', 'shopping:create'])]
    protected $email;
}

and this for Shopping

#[ORMEntity]
#[ApiPlatformApiResource(
    operations: [],
    graphQlOperations: [
        new ApiPlatformGraphQlQuery(),
        new ApiPlatformGraphQlMutation(
            name: 'create',
            denormalizationContext: ['groups' => ['shopping:create']],
        ),
    ]
)]
class Shopping
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn(type: 'integer')]
    private int $id;

    #[ORMManyToOne(targetEntity: User::class, inversedBy: 'shoppings')]
    #[Groups(['shopping:create'])]
    #[AssertValid()]
    private User $user;

    #[ORMColumn(type: 'string', length: 255, nullable: true)]
    #[Groups(['shopping:create'])]
    private string $name;
}

And execute graphql query like this

mutation {
  createShopping(
    input: {
      user: {
        email: "[email protected]"
      }, 
      name: "foo"
    }
  ) {
    shopping {
      id
    }
  }
}

I get error "email is exists"

I try write custom reslover to conver User entity, to exist entity like this

final class UserResolver implements MutationResolverInterface
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
    ) {
    }

    public function __invoke($item, array $context): object
    {
        if ($item->getEmail()) {
            $existUser = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $item->getEmail()]);
            if ($existUser) {
                return $existUser;
            }
        }



        return $item;
    }
}

and custom resolver for Shopping because resolvers not call cascade

final class ShoppingResolver implements MutationResolverInterface
{
    public function __construct(
        private readonly UserResolver $userResolver,
    ) {
    }

    public function __invoke($item, array $context): ?object
    {
        $item->setUser($this->userResolver->__invoke($item->getUser(), $context));

        return $item;
    }
}

It work if I remove #[AssertValid()] from Shopping entity because validation run before resolvers.

That the best way to solve this problem? Custom denormalizer or use Event system or somthing else?


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

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