From 456b93b3e6ecb74108929eec1fc209e296790524 Mon Sep 17 00:00:00 2001 From: Zac Scott Date: Mon, 25 Jan 2021 12:47:24 +1000 Subject: [PATCH 1/2] update field cache to allow it to be used wihtout the query_name configuration --- src/FieldCache.php | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/FieldCache.php b/src/FieldCache.php index b4578b8..b9e3a48 100644 --- a/src/FieldCache.php +++ b/src/FieldCache.php @@ -75,16 +75,9 @@ function activate(AbstractBackend $backend) function __action_do_graphql_request(string $query, $operation_name) { - // Capture from operation name if available - if ($operation_name === $this->query_name) { - $this->query = $query; - return; - } - $current_query_name = Utils::get_query_name($query); - if ($current_query_name === $this->query_name) { - $this->query = $query; - } + $this->query = $query; + } function __filter_graphql_pre_resolve_field( @@ -98,6 +91,7 @@ function __filter_graphql_pre_resolve_field( $field, $field_resolver ) { + // No query, no query name match if (!$this->query) { return $nil; @@ -107,22 +101,31 @@ function __filter_graphql_pre_resolve_field( if (count($info->path) !== 1) { return $nil; } - + // Check it matches with configured field name if ($this->field_name !== $info->path[0]) { return $nil; } - + // Mark as mached ie. this field should be cached $this->match = true; - $query_name = Utils::sanitize($this->query_name); $field_name = Utils::sanitize($this->field_name); $args_hash = Utils::hash(Utils::stable_string($args)); - $query_hash = Utils::hash($this->query); $user_id = get_current_user_id(); - $this->key = "field-{$query_name}-${field_name}-${user_id}-{$query_hash}-${args_hash}"; + if ( $this->query_name ) { + // If query name is configured, include the query hash in the cache key. + + $query_name = Utils::sanitize($this->query_name); + $query_hash = Utils::hash($this->query); + + $this->key = "field-${query_name}-${field_name}-${user_id}-${query_hash}-${args_hash}"; + } else { + // If query name is not configured, do not include the query hash in the cache key. + + $this->key = "field-${field_name}-${user_id}-${args_hash}"; + } $this->read_cache(); From 848cd035798df8cf11f76113e298c5877e7c3699 Mon Sep 17 00:00:00 2001 From: Zac Scott Date: Mon, 25 Jan 2021 13:05:24 +1000 Subject: [PATCH 2/2] remove stray whitespace --- src/FieldCache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FieldCache.php b/src/FieldCache.php index b9e3a48..8fec3d7 100644 --- a/src/FieldCache.php +++ b/src/FieldCache.php @@ -101,12 +101,12 @@ function __filter_graphql_pre_resolve_field( if (count($info->path) !== 1) { return $nil; } - + // Check it matches with configured field name if ($this->field_name !== $info->path[0]) { return $nil; } - + // Mark as mached ie. this field should be cached $this->match = true;