9.5 API: Update QueryExtension Signatures

This step updates API Platform 2.x QueryExtension method signatures to API Platform 4.x format.

When to skip this step:

  • Your plugin doesn't have custom QueryExtension classes

When to do this step:

  • You have custom QueryExtension classes implementing QueryCollectionExtensionInterface or QueryItemExtensionInterface

Overview of Changes

API Platform 4.x changed the method signature for QueryExtensions to pass an Operation object instead of operation name string:

API Platform 2.x
API Platform 4.x

string $operationName = null

?Operation $operation = null

Check: 'shop_get' === $operationName

Check: $operation?->getName() === 'shop_get'

Key changes:

  • Parameter type changed from string to ?Operation

  • Parameter name changed from $operationName to $operation

  • Must import ApiPlatform\Metadata\Operation

  • Access operation name via $operation?->getName()

1. Identify Files to Migrate

Check if you have QueryExtensions:

2. Update Method Signatures

Signature Changes

For QueryCollectionExtensionInterface:

Before (API Platform 2.x):

After (API Platform 4.x):

For QueryItemExtensionInterface:

Before (API Platform 2.x):

After (API Platform 4.x):

Required changes:

  1. Add import: use ApiPlatform\Metadata\Operation;

  2. Change parameter type: string $operationName = null?Operation $operation = null

  3. Update any references to $operationName in method body

3. Example Migrations

Example 1: Extension That Applies to All Operations

This is the simplest case - only signature needs updating.

Before (API Platform 2.x):

src/Doctrine/ORM/QueryExtension/EnabledExtension.php:

After (API Platform 4.x):

Changes:

  1. Added use ApiPlatform\Metadata\Operation;

  2. Changed string $operationName = null to ?Operation $operation = null

  3. Method body unchanged (doesn't use operation name)

Example 2: Extension That Checks Operation Name

Before (API Platform 2.x):

src/Doctrine/ORM/QueryExtension/Shop/ChannelBasedExtension.php:

After (API Platform 4.x):

Changes:

  1. Added use ApiPlatform\Metadata\Operation;

  2. Changed string $operationName = null to ?Operation $operation = null

  3. Changed 'shop_get_products' !== $operationName to $operation?->getName() !== 'shop_get_products'

Example 3: Item Extension

Before (API Platform 2.x):

src/Doctrine/ORM/QueryExtension/Shop/LocaleBasedExtension.php:

After (API Platform 4.x):

Changes:

  1. Added use ApiPlatform\Metadata\Operation;

  2. Changed string $operationName = null to ?Operation $operation = null

  3. Method body unchanged (doesn't use operation name)

Example 4: Extension Implementing Both Interfaces

Before (API Platform 2.x):

src/Doctrine/ORM/QueryExtension/TaxonBasedExtension.php:

After (API Platform 4.x):

Changes:

  1. Added use ApiPlatform\Metadata\Operation;

  2. Changed string $operationName = null to ?Operation $operation = null in both applyToCollection() and applyToItem() methods

  3. Method bodies unchanged (don't use operation name)

4. Common Operation Name Check Patterns

If your extension checks the operation name, update as follows:

Check if operation matches specific name:

Check if operation is NOT a specific name:

Check if operation exists:

Check multiple operations:

5. Service Registration

No changes needed to service registration. QueryExtensions are auto-tagged.

Example service configuration (remains the same):

6. Validate Changes

Clear the cache:

Verify extensions are registered:

Test API endpoints:

Important Notes

  1. Import required: Always add use ApiPlatform\Metadata\Operation;

  2. Signature change: string $operationName = null?Operation $operation = null

  3. Null-safe operator: Use $operation?->getName() to safely access operation name

  4. Both methods: If implementing both interfaces, update both applyToCollection() and applyToItem()

  5. Service registration: No changes needed - extensions are auto-tagged

  6. Backward compatible: The = null default maintains same behavior

Additional Operation Methods

The Operation object provides additional useful methods:

Reference

For more examples, check Sylius core QueryExtensions:

Example files:

  • Shop/Channel/ChannelBasedExtension.php - Collection extension checking operation

  • Shop/Product/EnabledExtension.php - Simple extension applying to all operations

  • Common/TranslationOrderNameAndLocaleExtension.php - Extension using both interfaces

Last updated

Was this helpful?