2

Thank you by advance for your help ! Before detailing my need, let me introduce the context :

Project stack:

tech/lib version
php 8.1
symfony v6.1
api-platform v2.6
webonyx/graphql-php v14.11

Doctrine model/entities

To simplify the example, I've used the example found in this doc.

My Event class is abstract :

<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: "event")]
#[ORM\InheritanceType("SINGLE_TABLE")]
#[ApiResource]
abstract class Event
{
//...

}

The others (Concert, Festival and Conference) are extending it :

<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ApiResource]
class Concert extends Event
{
//...

}

and so on for the others.

My goal

I want to use inline fragment to get in a single query my concrete events :

query {
  events {
    edges {
      node {
        id
        name
        minAgeRestriction
        startsAt

        ... on Festival {
          performers
        }

        ... on Concert {
          performingBand
        }

        ... on Conference {
          speakers
          workshops
        }
      }
    }
  }
}

The error I get

Fragment cannot be spread here as objects of type "Event" can never be of type "Concert".

How to tell GraphqlPhp through ApiPlatform to generate union type ? Did I missed something ? Is it not possible yet with api-platform ?

I'm not expert at all on GraphQL but I don't see anything about inline fragment in api-platform while graphql-php looks to support it w: https://webonyx.github.io/graphql-php/type-definitions/unions/

Could be related : https://github.com/api-platform/api-platform/issues/2105 What do you think ? Any clue ?

lenybernard
  • 2,399
  • 22
  • 22
  • Is the `event` type a `union` of `Festival`, `Concert` and `Conference`? – Michel Floyd Oct 27 '22 at 17:30
  • I guess not but actually, I don't know how tell to ApiPlatform / graphql-php to generate such union type. The only thing I do now is to make the `Festival`, `Concert` and `Conference` extend the abstract `Event`. I've tried to add some phpdoc but it didn't works... – lenybernard Oct 27 '22 at 19:48

0 Answers0