Skip to content

PHPStan rules ​

The bundle ships PHPStan rules checking, at analysis time, the field names given to the repositories and the query builder against the entity mapping: a typo is reported in your editor or your CI instead of as an API error at runtime.

Installation ​

With phpstan/extension-installer, the rules are enabled automatically. Otherwise, include them in your PHPStan configuration:

neon
includes:
    - vendor/hecht-a/graphql-orm/extension.neon

The entity classes must be loadable by PHPStan (autoloaded by Composer), as their attributes are read the same way the bundle reads them at runtime.

What is checked ​

php
/** @var TaskRepository $tasks extends GraphqlEntityRepository<Task> */
$tasks->findBy(['titel' => 'A']);
// Field "titel" is not mapped on App\GraphQL\Entity\Task. Did you mean "title"?

$tasks->createQueryBuilder()->select('title.length');
// Field "title" of App\GraphQL\Entity\Task is not a relation: "title.length" cannot be selected.
CallKeys or arguments checkedExpected name
findBy(), findOneBy()criteria and ordering keysGraphQL or PHP property name
count()criteria keysGraphQL or PHP property name
sum(), avg(), min(), max()field, and criteria keysGraphQL or PHP property name
GraphqlQueryBuilder::orderBy()fieldGraphQL or PHP property name
GraphqlQueryBuilder::select(), addSelect()paths, segment by segmentGraphQL or PHP property name

A field of an embedded value object is named by its path (total.amount). Only constant values are checked: a field name computed at runtime is not. The entity is taken from the generic type of the repository or query builder, so repositories must declare it (@extends ServiceGraphqlEntityRepository<Task>, as the generated ones do, or @extends GraphqlEntityRepository<Task>).

Fields that the entity does not map ​

Criteria and ordering keys are sent as is to the API, which may accept fields the entity does not map (a foreign key such as user_id, for instance). They are reported by default, as they are most often typos. To allow one, ignore it:

php
$tasks->findBy(['user_id' => $userId]); // @phpstan-ignore graphqlOrm.unmappedField

To allow all of them, and only report what is certainly wrong (a path through a field that is not a relation):

neon
parameters:
    graphqlOrm:
        reportUnmappedFields: false

Naming strategy ​

The rules read the entities as the bundle does. When the bundle names the fields with a naming strategy other than the default one, set the same class, which must be buildable without argument:

neon
parameters:
    graphqlOrm:
        namingStrategy: GraphqlOrm\Metadata\Naming\SnakeCaseNamingStrategy

Error identifiers ​

IdentifierMeaning
graphqlOrm.unmappedFieldfield not mapped on the entity (reportUnmappedFields)
graphqlOrm.notARelationpath going through a field that is not a relation

Released under the MIT License.