Skip to content

Nested resources in GraphQL without Doctrine #836

Description

@zgolus

Hi,

I'm trying to build an API backed by custom data providers that would expose data through a GraphQL endpoint. To try things out I created an entity that has reference to another entity and annotated it.

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource(
 *     itemOperations={
 *          "get",
 *     },
 *     collectionOperations={
 *       "get","post"
 *     },
 *     attributes={
 *          "denormalization_context"={
 *              "groups"={"write"}
 *          },
 *          "normalization_context"={
 *              "groups"={"read"}
 *          }
 *     }
 * )
 */
class BlogPost
{
    /**
     * @var int
     * @ApiProperty(identifier=true)
     * @Groups({"read"})
     */
    private $id;

    /**
     * @var string
     * @ApiProperty()
     * @Groups({"read", "write"})
     */
    public $title;

    /**
     * @var string
     * @ApiProperty()
     * @Groups({"read", "write"})
     */
    public $author;

    /**
     * @var Image[]
     * @ApiSubresource()
     * @Groups({"read", "write"})
     */
    public $images;

    public function __construct(int $id) {
        $this->id     = $id;
        $this->images = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }
}
<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 */
class Image
{
    /**
     * @var int
     * @ApiProperty(identifier=true)
     * @Groups({"read"})
     */
    private $id;

    /**
     * @var string
     * @ApiProperty()
     * @Groups({"read", "write"})
     */
    public $fileName;

    /**
     * @var BlogPost
     * @ApiProperty()
     * @Groups({"read", "write"})
     */
    public $blogPost;

    /**
     * @param int $id
     * @param string $fileName
     */
    public function __construct(int $id, string $fileName)
    {
        $this->id = $id;
        $this->fileName = $fileName;
    }


    public function getId(): ?int
    {
        return $this->id;
    }
}

Result looks good when queried through REST endpoint:

  {
    "id": 0,
    "title": "string",
    "author": "string",
    "images": [
      {
        "id": 0,
        "fileName": "string",
        "blogPost": "string"
      }
    ]
  }
]

images are nested resources of blogPost as expected. But when I try to query with GraphQL, the reference is ignored:

id: ID!
title: String!
author: String!
images: Iterable!
_id: Int!

This time images are just a string iterable.

I have forked the repo and commited my entities there, so it's easier to recreate the issue: zgolus@8e2778d

Please let me know, if I am missing something or if GraphQL actually needs Doctrine to create correct schema?

Best,
Jakub

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions