Skip to content

Commit d4090b1

Browse files
committed
Adjusting filters, updating README.
1 parent 68f7279 commit d4090b1

File tree

3 files changed

+87
-27
lines changed

3 files changed

+87
-27
lines changed

readme.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
# WPGraphQL Persisted Queries
22

3-
Persist queries in a custom post type, allowing them to be loaded by ID using
4-
the `queryId` operation parameter. This paves the way for using `GET` requests
5-
and leveraging edge cache.
3+
Persisted GraphQL queries allow a GraphQL client to optimistically send a hash
4+
of the query instead of the full query; if the server has seen the query
5+
before, it can satisfy the request. This saves network overhead and makes it
6+
possible to move to `GET` requests instead of `POST`. The primary benefit of
7+
`GET` requests is that they can be easily cached at the edge (e.g., with
8+
Varnish).
9+
10+
This plugin requires [WPGraphQL][wp-graphql] 0.2.0 or newer.
11+
12+
## Compatibility
13+
14+
Apollo Client provides an easy implementation of persisted queries:
15+
16+
https://github.com/apollographql/apollo-link-persisted-queries#automatic-persisted-queries
17+
18+
This plugin aims to be compatible with that implementation, but will work with
19+
any client that sends a `queryId` alongside the `query`. Make sure your client
20+
also sends `operationName` with the optimistic request.
21+
22+
## Implementation
23+
24+
When the client provides a query hash or ID, that query will be persisted in a
25+
custom post type. By default, this post type will be visible in the dashboard
26+
only to admins.
27+
28+
Query IDs are case-insensitive (that is `MyQuery` and `myquery` are equivalent).
29+
30+
## Filters
31+
32+
### `graphql_persisted_queries_post_type`
33+
34+
- Default: `'graphql_query'`
35+
- The custom post type used to persist queries. If empty, queries will not be
36+
persisted.
37+
38+
### `graphql_persisted_queries_show_in_graphql`
39+
40+
- Default: `false`
41+
- Whether the custom post type will itself be exposed via GraphQL. Enabling
42+
allows insight into which queries are persisted. Note that queries are
43+
inserted as drafts, so you will need to query accordingly, e.g.:
44+
45+
```
46+
query PersistedQueryQuery {
47+
persistedQueries (
48+
where: {
49+
status: DRAFT
50+
}
51+
) {
52+
nodes {
53+
id
54+
title
55+
content(format: RAW)
56+
}
57+
}
58+
}
59+
```
60+
61+
If you'd like to further customize the custom post type, filter
62+
`register_post_type_args`.
63+
64+
[wp-graphql]: https://github.com/wp-graphql/wp-graphql

src/loader.php

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111
* Load or save a persisted query from a custom post type. This allows users to
1212
* avoid sending the query over the wire, saving bandwidth. In particular, it
1313
* allows for moving to GET requests, which can be cached at the edge.
14-
*
15-
* @package WPGraphQL\Extensions\PersistedQueries
1614
*/
1715
class Loader {
1816
/**
19-
* Whether query persistence is enabled.
17+
* Namespace for WP filters.
2018
*
21-
* @var bool
19+
* @var string
2220
*/
23-
private $enabled = true;
21+
private $namespace = 'graphql_persisted_queries';
2422

2523
/**
2624
* Post type for default query persistence. Unused if query persistence is
@@ -37,22 +35,16 @@ class Loader {
3735
* @return void
3836
*/
3937
public function init() {
40-
/**
41-
* Whether to enable persisted queries.
42-
*
43-
* @param bool $enabled Enable?
44-
*/
45-
$this->enabled = apply_filters( 'graphql_persisted_query_enabled', $this->enabled );
46-
4738
/**
4839
* Post type to use to persist queries. Unused if disabled.
4940
*
5041
* @param string $post_type
42+
* @since 1.0.0
5143
*/
52-
$this->post_type = apply_filters( 'graphql_persisted_query_post_type', $this->post_type );
44+
$this->post_type = apply_filters( "{$this->namespace}_post_type", $this->post_type );
5345

54-
// If not enabled or the post type doesn't look right, don't hook.
55-
if ( ! $this->enabled || empty( $this->post_type ) || post_type_exists( $this->post_type ) ) {
46+
// If the post type doesn't look right, don't hook.
47+
if ( empty( $this->post_type ) || post_type_exists( $this->post_type ) ) {
5648
return;
5749
}
5850

@@ -86,41 +78,50 @@ public function load( $query_id ) {
8678
* @return array
8779
*/
8880
public function process_request_data( $request_data ) {
89-
// Client sends *both* queryId and query = request to persist query.
81+
// Client sends *both* queryId and query == request to persist query.
9082
if ( ! empty( $request_data['queryId'] ) && ! empty( $request_data['query'] ) ) {
9183
$this->save( $request_data['queryId'], $request_data['query'], $request_data['operation'] );
9284
}
9385

94-
// Client sends queryId but *not* query = optimistic request to use cached query.
86+
// Client sends queryId but *not* query == optimistic request to use cached query.
9587
if ( ! empty( $request_data['queryId'] ) && empty( $request_data['query'] ) ) {
9688
$request_data['query'] = $this->load( $request_data['queryId'] );
9789
}
9890

99-
// We've got this; call off any other persistence implementations.
91+
// We've got this. Call off any other persistence implementations.
10092
unset( $request_data['queryId'] );
10193

10294
return $request_data;
10395
}
10496

10597
/**
106-
* Register the persisted query post type. Filter register_post_type_args to
107-
* show persisted queries in GraphQL. 💅
98+
* Register the persisted query post type. We could filter a lot of individual
99+
* values here, but we won't. If further customization is wanted, filter
100+
* register_post_type_args.
108101
*
109102
* @return void
110103
*/
111104
private function register_post_type() {
105+
/**
106+
* Whether persisted queries can be themselves queried via GraphQL. 💅
107+
*
108+
* @param bool $show_in_graphql Show in GraphQL?
109+
* @since 1.0.0
110+
*/
111+
$show_in_graphql = apply_filters( "{$this->namespace}_show_in_graphql", false );
112+
112113
register_post_type(
113114
$this->post_type,
114115
[
115116
'label' => 'Queries',
116117
'public' => false,
117-
'query_var' => true,
118+
'query_var' => false,
118119
'rewrite' => false,
119120
'show_in_rest' => false,
120-
'show_in_graphql' => false,
121+
'show_in_graphql' => $show_in_graphql,
121122
'graphql_single_name' => 'persistedQuery',
122123
'graphql_plural_name' => 'persistedQueries',
123-
'show_ui' => true,
124+
'show_ui' => is_admin(),
124125
'supports' => [ 'title', 'editor' ],
125126
]
126127
);

wp-graphql-persisted-queries.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Plugin Name: WPGraphQL Persisted Queries
44
* Plugin URI: https://github.com/Quartz/wp-graphql-persisted-queries
5-
* Description: Provides persistence to queries allowing them to be queried by ID (hash)
5+
* Description: Provides persistence to GraphQL queries allowing them to be queried by ID or hash. Requires WPGraphQL 0.2.0 or newer.
66
* Author: Chris Zarate, Quartz
77
* Version: 1.0.0
88
* Author URI: https://qz.com/

0 commit comments

Comments
 (0)