11
11
* Load or save a persisted query from a custom post type. This allows users to
12
12
* avoid sending the query over the wire, saving bandwidth. In particular, it
13
13
* allows for moving to GET requests, which can be cached at the edge.
14
- *
15
- * @package WPGraphQL\Extensions\PersistedQueries
16
14
*/
17
15
class Loader {
18
16
/**
19
- * Whether query persistence is enabled .
17
+ * Namespace for WP filters .
20
18
*
21
- * @var bool
19
+ * @var string
22
20
*/
23
- private $ enabled = true ;
21
+ private $ namespace = ' graphql_persisted_queries ' ;
24
22
25
23
/**
26
24
* Post type for default query persistence. Unused if query persistence is
@@ -37,22 +35,16 @@ class Loader {
37
35
* @return void
38
36
*/
39
37
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
-
47
38
/**
48
39
* Post type to use to persist queries. Unused if disabled.
49
40
*
50
41
* @param string $post_type
42
+ * @since 1.0.0
51
43
*/
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 );
53
45
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 ) ) {
56
48
return ;
57
49
}
58
50
@@ -86,41 +78,50 @@ public function load( $query_id ) {
86
78
* @return array
87
79
*/
88
80
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.
90
82
if ( ! empty ( $ request_data ['queryId ' ] ) && ! empty ( $ request_data ['query ' ] ) ) {
91
83
$ this ->save ( $ request_data ['queryId ' ], $ request_data ['query ' ], $ request_data ['operation ' ] );
92
84
}
93
85
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.
95
87
if ( ! empty ( $ request_data ['queryId ' ] ) && empty ( $ request_data ['query ' ] ) ) {
96
88
$ request_data ['query ' ] = $ this ->load ( $ request_data ['queryId ' ] );
97
89
}
98
90
99
- // We've got this; call off any other persistence implementations.
91
+ // We've got this. Call off any other persistence implementations.
100
92
unset( $ request_data ['queryId ' ] );
101
93
102
94
return $ request_data ;
103
95
}
104
96
105
97
/**
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.
108
101
*
109
102
* @return void
110
103
*/
111
104
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
+
112
113
register_post_type (
113
114
$ this ->post_type ,
114
115
[
115
116
'label ' => 'Queries ' ,
116
117
'public ' => false ,
117
- 'query_var ' => true ,
118
+ 'query_var ' => false ,
118
119
'rewrite ' => false ,
119
120
'show_in_rest ' => false ,
120
- 'show_in_graphql ' => false ,
121
+ 'show_in_graphql ' => $ show_in_graphql ,
121
122
'graphql_single_name ' => 'persistedQuery ' ,
122
123
'graphql_plural_name ' => 'persistedQueries ' ,
123
- 'show_ui ' => true ,
124
+ 'show_ui ' => is_admin () ,
124
125
'supports ' => [ 'title ' , 'editor ' ],
125
126
]
126
127
);
0 commit comments