Using api-platform to query with GraphQL, how can I create a filter from a virtual field?

93 Views Asked by At

Im using symfony 6 and api-platform/core: v3.1.3. Said that, I have an entity that has a method calculable/virtual that I would like to query using graphQL

That is the entity

<?php


#[
    ApiResource(
        operations: [
            new Get(),
            new Put(
                denormalizationContext: ['groups' => ['update']],
               
            ),
            new Delete(),
            new GetCollection(),
            new Post(),
        ],
        normalizationContext: ['groups' => ['read']],
        denormalizationContext: ['groups' => ['create']],
        order: ['id' => 'ASC'],
      
        processor: Processor::class
    )
]
#[ApiFilter(filterClass: TaggedFilter::class, properties: ['channels'])]
#[
    ApiFilter(
        filterClass: SearchFilter::class,
        properties: [
            'hasFiled' => 'exact',
            
        ]
    )
]
#[
    ApiFilter(
        filterClass: BooleanFilter::class,
        properties: ['isReady']
    )
]

#[
    ApiFilter(
        filterClass: OrderFilter::class,
        properties: [
            'hasFiled',
            
        ],
    )
]
#[
    ApiFilter(
        filterClass: DateFilter::class,
        properties: ['updatedAt', 'createdAt']
    )
]
#[ORM\Table(name: 'table')]
#[ORM\Entity(repositoryClass: Repository::class)]
#[ORM\EntityListeners([Listener::class])]
class ProblemEntity
{
    use TimestampableEntity;
    use BlameableEntity;
    use SoftDeleteableEntity;

    #[ORM\Id]
    #[ORM\Column(name: 'id', type: 'uuid', unique: true)]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: UuidGenerator::class)]
    #[Groups(['read'])]
    private $id;
 
    #[ORM\OneToOne(targetEntity: OtherClass::class, cascade: ['persist'])]
    #[
        ORM\JoinColumn(
            name: 'name',
            referencedColumnName: 'id',
        )
    ]
    private $other = null;

    #[ORM\OneToOne(targetEntity: Other2Class::class, cascade: ['persist'])]
    #[
        ORM\JoinColumn(
            name: 'name2',
            referencedColumnName: 'id',
        )
    ]
    private $other2 = null;

   

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

    

    #[Groups(['read'])]
    #[SerializedName('hasFiled')]
    #[ApiProperty(readableLink: true, writableLink: true)]

    public function hasFiled(): bool
    {
        return $this->other?->hasFiled()
            || $this->other2?->other3?->hasFiled();
    }


}


How can I query using hasFiled?

{
   problemEntity(first: 10, order: { createdAt: "desc" }, hasFiled:true) { [...]}

}

that is the error

{
    "errors": [
        {
            "message": "Unknown argument \"hasField\" on field \"problemEntity\" of type \"Query\".",
            "locations": [
                {
                    "line": 2,
                    "column": 97
                }
            ],
            "extensions": {
                "file": "/usr/share/nginx/vendor/webonyx/graphql-php/src/Validator/Rules/KnownArgumentNames.php",
                "line": 42
            }
        }
    ]
}

I need to filter/query by hasFiled virtual field. Looking for this answer I have spend a lot of time trying to find out a way to do it.

Im using posgres as DB and Doctrine do meneger the DB comunication.

Thx

0

There are 0 best solutions below