Skip to content

Commit

Permalink
Merge pull request #230 from wp-graphql/fix/#229-disable-maps-when-ob…
Browse files Browse the repository at this point in the history
…ject-cache-is-off

fix: disable cache maps when "Use Object Cache" is disabled
  • Loading branch information
jasonbahl authored Jul 14, 2023
2 parents e3ca36c + 9479e3d commit fe967fc
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 4 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/tests-wordpress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ jobs:
runs-on: ubuntu-latest
name: WordPress ${{ matrix.wordpress }} Integration Tests on PHP ${{ matrix.php }}
strategy:
fail-fast: false
matrix:
php: [ '8.0', '7.4' ]
wordpress: [ '5.9', '5.8', '5.7.2', '5.6' ]
wpgraphql_version: [ 'latest' ]
include:
- php: '8.0'
wordpress: '6.0'
- php: '8.1'
wordpress: '6.1'
# Temporary inclusion while we resolve the tests that broke in the WPGraphQL v1.14.5 release
# this allows us to continue testing new features to this codebase while we work
# toward updating the tests impacted by that release
- php: '8.1'
wordpress: '6.2'
wpgraphql_version: '1.14.4'
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down Expand Up @@ -58,7 +66,7 @@ jobs:
run: composer update

- name: Run acceptance, functional tests
run: docker-compose run -e DEBUG=1 testing
run: docker-compose run -e DEBUG=1 -e WPGRAPHQL_VERSION=${{matrix.wpgraphql_version}} testing
env:
WP_VERSION: ${{ matrix.wordpress }}
PHP_VERSION: ${{ matrix.php }}
Expand Down
23 changes: 22 additions & 1 deletion src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,29 @@ public static function caching_enabled() {
// get the cache_toggle setting
$option = function_exists( 'get_graphql_setting' ) ? \get_graphql_setting( 'cache_toggle', false, 'graphql_cache_section' ) : false;

$enabled = ( 'on' === $option );

$enabled = apply_filters( 'wpgraphql_cache_wordpress_cache_enabled', (bool) $enabled );

// if there's no user logged in, and GraphQL Caching is enabled
return ( 'on' === $option );
return (bool) $enabled;
}

/**
* Whether cache maps are enabled.
*
* Cache maps are used to track which nodes and list keys are associated with which queries,
* and can be referenced to purge specific queries.
*
* Default behavior is to only enable building and storage of the cache maps if "WordPress Cache" (non-network cache) is enabled, but this can be filtered to be enabled without WordPress cache being enabled.
*
* @return bool
*/
public static function cache_maps_enabled() {

// Whether "WordPress Cache" (object/transient) cache is enabled
$enabled = self::caching_enabled();
return (bool) apply_filters( 'wpgraphql_cache_enable_cache_maps', (bool) $enabled );
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Cache/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GraphQL\Executor\ExecutionResult;
use GraphQL\Type\Schema;
use WPGraphQL\Request;
use WPGraphQL\SmartCache\Admin\Settings;

class Collection extends Query {

Expand Down Expand Up @@ -62,6 +63,12 @@ public function save_query_mapping_cb(
$request,
$query_id
) {

// If cache maps are not enabled, do nothing
if ( ! Settings::cache_maps_enabled() ) {
return;
}

$request_key = $this->build_key( $query_id, $query, $variables, $operation );

// get the runtime nodes from the query analyzer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ public function setUp(): void {
*/
$this->clearSchema();

// enable graphql cache maps
add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' );

// prevent default category from being added to posts on creation
update_option( 'default_category', 0 );

Expand Down
10 changes: 9 additions & 1 deletion tests/wpunit/CacheCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

class CacheCollectionTest extends \Codeception\TestCase\WPTestCase {

public function testAddData() {
public function _setUp() {

// enable graphql cache maps
add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' );

parent::_setUp(); // TODO: Change the autogenerated stub
}

public function testAddData() {
$key = uniqid( 'test-' );
$content = 'foo-bar';

Expand Down
5 changes: 4 additions & 1 deletion tests/wpunit/CacheFromConnectionFieldNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class CacheFromConnectionFieldNameTest extends \Codeception\TestCase\WPTestCase

public function setUp(): void {
// clear schema so that the register connection works
\WPGraphQL::clear_schema();
// enable graphql cache maps
add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' );

\WPGraphQL::clear_schema();
parent::setUp();
}

Expand Down
4 changes: 4 additions & 0 deletions tests/wpunit/CacheInvalidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class CacheInvalidationTest extends \Codeception\TestCase\WPTestCase {
protected $collection;

public function setUp(): void {

\WPGraphQL::clear_schema();

// enable graphql cache maps
add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' );

if ( ! defined( 'GRAPHQL_DEBUG' ) ) {
define( 'GRAPHQL_DEBUG', true );
}
Expand Down

0 comments on commit fe967fc

Please sign in to comment.