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

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


0

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']
    )
]
#[ORMTable(name: 'table')]
#[ORMEntity(repositoryClass: Repository::class)]
#[ORMEntityListeners([Listener::class])]
class ProblemEntity
{
    use TimestampableEntity;
    use BlameableEntity;
    use SoftDeleteableEntity;

    #[ORMId]
    #[ORMColumn(name: 'id', type: 'uuid', unique: true)]
    #[ORMGeneratedValue(strategy: 'CUSTOM')]
    #[ORMCustomIdGenerator(class: UuidGenerator::class)]
    #[Groups(['read'])]
    private $id;
 
    #[ORMOneToOne(targetEntity: OtherClass::class, cascade: ['persist'])]
    #[
        ORMJoinColumn(
            name: 'name',
            referencedColumnName: 'id',
        )
    ]
    private $other = null;

    #[ORMOneToOne(targetEntity: Other2Class::class, cascade: ['persist'])]
    #[
        ORMJoinColumn(
            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

New contributor

Marco Farnezi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

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