I am trying to use Doctrine with API Platform graphql in symfony application. I am using Postgres for database.
I have two entities viz WallPost
and WallPostLikes
with OneToMany
Relationship.
That means one wall post will have many likes.
While querying wall posts, I am also trying to retrieve all the likes for each post. but doing so throw this error
Provider not found on operation "collection_query"
It looks like I need to create a resolver class for
WallPostLikes
entity but I am not sure how to implement Resolver class of
WallPostLikes
entity because I am calling API for
WallPost
I am including relevant code
WallPost.php
#[ApiResource(
shortName: ‘Post’,
paginationEnabled: false,
graphQlOperations: [
new QueryCollection(
resolver: PostListResolver::class,
args: [
‘parameter’ => [‘type’ => ‘InputPostRequest’],
],
read: false
),
],
)]
#[ORMEntity(repositoryClass: WallControlRepository::class)]
#[ORMTable(name: "wall_posts")]
class WallPost
{
#[ORMOneToMany(mappedBy: 'wallPost', targetEntity: WallPostLike::class)]
#[Groups(SerializationGroupEnum::ALL)]
private Collection $wallPostLikes;
public function __construct()
{
$this->wallPostLikes = new ArrayCollection();
}
/**
* @return Collection<int, WallPostLike>
*/
public function getWallPostLikes(): Collection
{
return $this->wallPostLikes;
}
}
WallPostLike.php
#[ApiResource(
shortName: ‘wallPostLike’,
paginationEnabled: false
)]
#[ORMEntity(repositoryClass: WallControlRepository::class)]
#[ORMTable(name: "wall_post_likes")]
class WallPostLike
{
#[ORMManyToOne(targetEntity: WallPost::class, inversedBy: 'wallPostLikes')]
#[ORMJoinColumn(name: 'wall_post_id', referencedColumnName: 'id', nullable: false)]
private WallPost $wallPost;
public function setWallPost(WallPost $wallPost): self
{
$this->wallPost = $wallPost;
return $this;
}
}
schema.graphql
type Query {
posts(parameter: InputPostRequest): [Post]
}
type Post implements Node {
id: ID!
wall: wall!
wallPostLikes: [wallPostLike]
}
type wallPostLike implements Node {
id: ID!
userId: String!
wallPost: Post!
}
I tried to create a resolver class for WallPostLike
entity and configured Resolver class in API Resource.
The resolver class for WallPostLike
is being called when querying for WallPost
entity. This class is being called each time for every wall posts and individual post is available in context
variable within __invoke
function as well
If I implement a custom resolver for WallPostLike
, then do I need to write another query to retrieve all the likes for a give post.
then it would be like calling database twice for a single graphql request.
Suppose I have one more entity similar to WallPostLike
which is also a collection then I will have to write another database query.
If I want to summarize my question, I don’t know what is the correct way of working with docrine and graphql in symfony application. I couldn’t able to find a suitable implementation as well