veysiyildiz / module-graphql-performance
A comprehensive GraphQL performance optimization module for Magento 2
Fund package maintenance!
veysiyildiz
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 1
Open Issues: 0
Type:magento2-module
Requires
- php: ~7.4.0||~8.1.0
- magento/framework: ^103.0.6
- magento/module-catalog-graph-ql: ^100.4.6
- magento/module-customer-graph-ql: ^100.4.6
- magento/module-graph-ql: ^100.4.6
- magento/module-store-graph-ql: ^100.4.3
Requires (Dev)
- magento/framework: ^103.0.6
- magento/magento-coding-standard: ^31.0
- magento/magento2-functional-testing-framework: ^3.0
- magento/module-catalog: ^104.0.6
- magento/module-customer: ^103.0.6
- magento/module-integration: ^100.4.6
- magento/module-store: ^101.1.6
- phpcompatibility/php-compatibility: ^9.3
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ~3.7.2
- vimeo/psalm: ^5.0
README
Overview
The Sterk GraphQL Performance module is a comprehensive solution for optimizing GraphQL performance in Magento 2. It addresses common performance bottlenecks, implements efficient data loading patterns, and provides robust caching strategies.
Features
1. Data Loading Optimization
- DataLoader pattern implementation for batch loading
- Prevention of N+1 query problems
- Efficient data retrieval for:
- Products
- Categories
- Customers
- CMS Pages/Blocks
- Orders and Invoices
- Brand Categories
- Credit Memos
2. Caching Infrastructure
- Multi-level caching strategy
- Intelligent cache tag management
- Automatic cache warming
- Query-level caching
- Field-level result caching
- Cache invalidation optimization
3. Performance Monitoring
- Query execution time tracking
- Performance metrics collection
- GraphQL query complexity analysis
- Resource usage monitoring
- Performance reporting
4. Database Optimization
- Connection pooling
- Transaction management
- Batch query optimization
- Efficient resource utilization
Installation
- Install via Composer:
composer require veysiyildiz/module-graphql-performance
- Enable the module:
bin/magento module:enable Sterk_GraphQlPerformance bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:clean
Configuration
Admin Configuration
Navigate to Stores > Configuration > Sterk > GraphQL Performance to configure:
-
Cache Settings
- Cache lifetime for different entities
- Cache warming patterns
- Cache invalidation rules
-
Performance Limits
- Query complexity limits
- Query depth restrictions
- Batch size configurations
-
Monitoring Settings
- Performance logging
- Metric collection
- Report generation
Cache Warming Configuration
Configure cache warming in etc/config.xml
:
<config> <default> <graphql_performance> <cache> <warming_enabled>1</warming_enabled> <warming_interval>3600</warming_interval> </cache> </graphql_performance> </default> </config>
Usage
Basic Implementation
use Sterk\GraphQlPerformance\Model\DataLoader\ProductDataLoader; class ProductResolver implements ResolverInterface { public function __construct( private readonly ProductDataLoader $dataLoader ) {} public function resolve( Field $field, $context, ResolveInfo $info, array $value = null, array $args = null ) { return $this->dataLoader->load($args['id']); } }
Advanced Features
Custom DataLoader
use Sterk\GraphQlPerformance\Model\DataLoader\BatchDataLoader; class CustomDataLoader extends BatchDataLoader { protected function loadFromDatabase(array $ids): array { // Implement batch loading logic } protected function generateCacheKey(string $id): string { return "custom_entity_{$id}"; } }
Cache Tag Management
use Sterk\GraphQlPerformance\Model\Cache\TagManager; class CustomResolver { public function __construct( private readonly TagManager $tagManager ) {} public function resolve($field) { $tags = $this->tagManager->getEntityTags('custom_entity', $id); // Use tags for cache operations } }
Performance Monitoring
Metrics Collection
The module automatically collects:
- Query execution times
- Cache hit rates
- Database query counts
- Memory usage
- Query complexity scores
Reporting
Access performance reports via:
- GraphQL API endpoint
- Admin dashboard
- Command line interface
Best Practices
1. Query Optimization
- Use field selection wisely
- Implement pagination
- Leverage query batching
- Monitor query complexity
2. Caching Strategy
- Configure appropriate cache lifetimes
- Use specific cache tags
- Implement proper invalidation
- Enable cache warming
3. Resource Management
- Configure connection pooling
- Monitor memory usage
- Implement query timeouts
- Use batch processing
Troubleshooting
Common Issues
- Cache Invalidation
Issue: Stale data after updates
Solution: Check cache tags and invalidation events
- Performance Degradation
Issue: Slow query response
Solution: Monitor query complexity and database connections
- Memory Usage
Issue: High memory consumption
Solution: Adjust batch sizes and connection pool settings
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
License
MIT License - see LICENSE.md for details
Support
For support, please:
- Check documentation
- Search existing issues
- Create a new issue
- Contact veysiyildiz@gmail.com
Changelog
See CHANGELOG.md for version history