Skip to content

Commit

Permalink
Merge pull request #150 from wp-graphql/feature/add-extension-output-…
Browse files Browse the repository at this point in the history
…when-response-is-cached

Add message to extensions when response is returned from object cache
  • Loading branch information
jasonbahl authored Jun 30, 2022
2 parents ab47d08 + d18e6f8 commit ae8398b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 156 deletions.
131 changes: 2 additions & 129 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,130 +1,3 @@
# Welcome

Install the plugin in your WP environment. It is dependent on wp-grapqhl also being installed.

Save a graphql query string for future use by an easy usable sha 256 hash or an alias name.

## Saving Queries

When submiting a query from your client, in the POST request provide a `queryId={value}` parameter with the submitted data. This `queryId` becomes the alias name of the saved query. It can then be used in future requests to invoke the query.

Example POST data:

```
{
"query": "{
posts {
nodes {
title
}
}
}"
"queryId": "query-get-posts-title"
}
```

After that successful POST request, you can use the GET request to invoke the query by queryId.

docker run -v $PWD:/app composer install --optimize-autoloader

```
https://domain.example/graphql?queryId=query-get-posts-title
```

## What about graphql variables?

Queries that require variables to execute the graphql query, will need the variables specified with each POST or GET request. The variables are not saved with the query string in the system.

Here is an example of invoking a saved query and providing variables.

If this is the saved query in the system,

POST

```
{
"query": "query ($count:Int) {
posts(first:$count) {
nodes {
title
}
}
}"
"queryId": "query-get-posts-title"
}
```

The GET request would look like this to provide variables for the request,

```
https://domain.example/graphql?queryId=query-get-posts-title&variables={"count":2}
```

## What about queries with multiple operation names?

Graphql query strings can contain multiple operations per string and an operation name is provided in the request to specify which query to invoke. The same is true for saved queries.

Below is an example of a query string containing multiple operations that can be saved with queryId "multiple-query-get-posts".

POST

```
{
"query": "
query GetPosts {
posts {
nodes{
id
title
}
}
}
query GetPostsSlug {
posts {
nodes{
id
title
slug
}
}
}
",
"queryId": "multiple-query-get-posts"
}
```

The GET request for the saved query specifying operation name looks like this,

https://domain.example/graphql?queryId=multiple-query-get-posts&operationName=GetPostsSlug

And if your query is multiple operations as well as variables, combine all of it together in your saved query and use the correct name/value parameters on the GET query requests.

## Not Found Error

If the queryId is not found in the system an error message will be returned in the response payload. A HTTP code of 200 is returned.

```
{
"errors": [
{
"message": "Query Not Found get-post-with-slug",
"extensions": {
"category": "request"
}
}
],
}
```

## Enable caching results

Log into wp-admin for the WP service.

Select the GraphQL Settings tab.

![Settings tab](./docs/images/wp-graphql-settings.png).
-
Check the box to enable results caching.

![The checkbox to enable the results cache](./docs/images/wp-graphql-smart-cache-settings-cache.png).
# WP GraphQL Smart Cache - BETA USERS

If you've been contacted to beta test this plugin, you can access the docs here: https://docs.google.com/document/d/16n2LxJB5POBkkWF6OIC9et1BogLMiRf6qZwL7ofBT5k/edit?usp=sharing
37 changes: 19 additions & 18 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
=== WP GraphQL Persisted Queries ===
Contributors: markkelnar, jasonbahl
Tags: GraphQL
Requires at least: 4.5
Tested up to: 5.6.1
Requires PHP: 5.6
Stable tag: 0.1.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

== Description ==

WPGraphQL Persisted Queries for WPGraphQL, a plugin that provides an extendable GraphQL schema and API for any WordPress site.

== Changelog ==

= 0.1 =
Initial
=== WP GraphQL Persisted Queries ===
Contributors: markkelnar, jasonbahl
Tags: GraphQL
Requires at least: 4.5
Tested up to: 5.6.1
Requires PHP: 5.6
Stable tag: 0.1.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

== Description ==

WPGraphQL Persisted Queries for WPGraphQL, a plugin that provides an extendable GraphQL schema and API for any WordPress site.

== Changelog ==

= 0.1.1 =

- Initial release to beta users
4 changes: 2 additions & 2 deletions src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function () {
if ( $value < 0 || ! is_numeric( $value ) ) {
return 0;
}
return intval( $value );
return (int) $value;
},
]
);
Expand Down Expand Up @@ -138,7 +138,7 @@ function () {
if ( $value < 0 || ! is_numeric( $value ) ) {
return null;
}
return intval( $value );
return (int) $value;
},
]
);
Expand Down
54 changes: 51 additions & 3 deletions src/Cache/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,27 @@ class Results extends Query {

const GLOBAL_DEFAULT_TTL = 600;

/**
* The cache key for the executed GraphQL Document
*
* @var string
*/
protected $cache_key = '';

/**
* The cached response of a GraphQL Query execution. False if it doesn't exist.
*
* @var mixed|bool|array|object
*/
protected $cached_result;

public function init() {
$this->cached_result = false;

add_filter( 'pre_graphql_execute_request', [ $this, 'get_query_results_from_cache_cb' ], 10, 2 );
add_action( 'graphql_return_response', [ $this, 'save_query_results_to_cache_cb' ], 10, 7 );
add_action( 'wpgraphql_cache_purge_nodes', [ $this, 'purge_nodes_cb' ], 10, 2 );
add_filter( 'graphql_request_results', [ $this, 'add_cache_key_to_response_extensions' ], 10, 1 );

parent::init();
}
Expand All @@ -31,7 +48,37 @@ public function init() {
* @return string|false unique id for this request or false if query not provided
*/
public function the_results_key( $query_id, $query, $variables = null, $operation = null ) {
return $this->build_key( $query_id, $query, $variables, $operation );
$this->cache_key = $this->build_key( $query_id, $query, $variables, $operation );
return $this->cache_key;
}


/**
* Add a message to the extensions when a GraphQL request is returned from the GraphQL Object Cache
*
* @param mixed|array|object $response The response of the GraphQL Request
*
* @return array|mixed
*/
public function add_cache_key_to_response_extensions( $response ) {
$message = [];

// if there's no cache key, or there is no cached_result return the response as-is
if ( ! empty( $this->cache_key ) && ! empty( $this->cached_result ) ) {
$message = [
'message' => __( 'This response was not executed at run-time but has been returned from the GraphQL Object Cache', 'wp-graphql-smart-cache' ),
'cacheKey' => $this->cache_key,
];
}

if ( is_array( $response ) ) {
$response['extensions']['graphqlSmartCache']['graphqlObjectCache'] = $message;
} if ( is_object( $response ) ) {
$response->extensions['graphqlSmartCache']['graphqlObjectCache'] = $message;
}

// return the modified response with the graphqlSmartCache message in the extensions output
return $response;
}

/**
Expand All @@ -56,8 +103,9 @@ public function get_query_results_from_cache_cb( $result, $request ) {
return null;
}

$cached_result = $this->get( $key );
return ( false === $cached_result ) ? null : $cached_result;
$this->cached_result = $this->get( $key );

return ( false === $this->cached_result ) ? null : $this->cached_result;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public function init() {
register_post_type(
self::TYPE_NAME,
[
'description' => __( 'Saved GraphQL queries', 'wp-graphql-smart-cache' ),
'description' => __( 'Saved GraphQL Documents', 'wp-graphql-smart-cache' ),
'labels' => [
'name' => __( 'GraphQLQueries', 'wp-graphql-smart-cache' ),
'singular_name' => __( 'GraphQLQuery', 'wp-graphql-smart-cache' ),
'name' => __( 'GraphQL Documents', 'wp-graphql-smart-cache' ),
'singular_name' => __( 'GraphQL Document', 'wp-graphql-smart-cache' ),
],
'public' => false,
'show_ui' => Settings::show_in_admin(),
Expand Down
2 changes: 1 addition & 1 deletion wp-graphql-smart-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Author: WPGraphQL
* Author URI: http://www.wpgraphql.com
* Domain Path: /languages
* Version: 0.1.0-alpha
* Version: 0.1.1
*
* Persisted Queries and Caching for WPGraphQL
*/
Expand Down

0 comments on commit ae8398b

Please sign in to comment.