ApiPlatform – GraphQL – Get Mapped Entity fields with serialization groups

API Platform version(s) affected: v2.5.7 (symfony v4.4.13 on PHP 7.4)

Description
Using serialization groups, I want to show field of a linked resource.
For example, a User have an Organization

AppEntityUser

 * @ApiResource(
 *     normalizationContext={"groups"={"User:read"}},
 *     denormalizationContext={"groups"={"User:write"}},
 *     graphql={
 *         "item_query"={"normalization_context"={"groups"={"User:read"}}},
 *         "collection_query"={"normalization_context"={"groups"={"User:read"}}},
 *     }
 * )
[....]
    /**
     * @Groups({"User:read"})
     * @ORMManyToOne(targetEntity="Organization", inversedBy="users")
     * @ORMJoinColumn(name="organization_id", referencedColumnName="id", nullable=true)
     */
    private $organization;

AppEntityOrganization

 * @ApiResource(
 *     graphql={
 *         "item_query"={"normalization_context"={"groups"={"Organization:read"}}},
 *         "collection_query"={"normalization_context"={"groups"={"Organization:read"}}},
 *     }
 * )
[...]
    /**
     * @Groups({"User:read"})
     * @ORMColumn(type="string", length=255, unique=true)
     */
    private $name;

I want to execute this request :

  users {
    edges {
      node {
        email,
        organization {
          id, name
        }
      }
    }
  }
}

The attribute name isn’t in the doc and the request fails with this error Cannot query field "name" on type "Organization".

How to reproduce
Here is a github project to reproduce : https://github.com/bastoune/api-platform-test-project/tree/issue/SerializationGroups

Additional context
This behaviour perfectly works in Rest

This is possibly linked with this issue : https://github.com/api-platform/api-platform/issues/1636

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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