Skip to content

Commit ce01f1b

Browse files
authored
feat(server): Add Persisted Queries support (#707)
1 parent 767e167 commit ce01f1b

20 files changed

Lines changed: 918 additions & 6 deletions

config/schema/graphql.schema.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ graphql.graphql_servers.*:
2525
label: 'Batching'
2626
schema_configuration:
2727
type: 'graphql.schema.[%parent.schema]'
28+
persisted_queries_settings:
29+
type: sequence
30+
label: 'Persisted queries settings'
31+
orderby: key
32+
sequence:
33+
type: plugin.plugin_configuration.persisted_query.[%key]
34+
label: 'The configuration for a single persisted query plugin.'
2835

2936
graphql.schema.*:
3037
type: mapping
@@ -39,3 +46,14 @@ graphql.schema.composable:
3946
type: sequence
4047
sequence:
4148
type: boolean
49+
50+
graphql.default_persisted_query_configuration:
51+
type: mapping
52+
label: 'Persisted query settings'
53+
mapping:
54+
weight:
55+
type: integer
56+
label: 'The weight'
57+
58+
plugin.plugin_configuration.persisted_query.*:
59+
type: graphql.default_persisted_query_configuration

graphql.libraries.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ voyager:
2424
- core/drupal
2525
- core/jquery
2626
- core/jquery.once
27+
28+
persisted_queries:
29+
version: VERSION
30+
js:
31+
js/persisted_queries.js: {}
32+
dependencies:
33+
- core/drupal
34+
- core/jquery
35+
- core/jquery.once

graphql.links.task.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ entity.graphql_server.delete_form:
99
title: Delete
1010
weight: 10
1111

12+
entity.graphql_server.persisted_queries_form:
13+
route_name: entity.graphql_server.persisted_queries_form
14+
base_route: entity.graphql_server.edit_form
15+
title: Persisted queries
16+
1217
graphql.explorer:
1318
route_name: graphql.explorer
1419
base_route: entity.graphql_server.edit_form

graphql.routing.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ entity.graphql_server.edit_form:
2828
options:
2929
_admin_route: TRUE
3030

31+
entity.graphql_server.persisted_queries_form:
32+
path: '/admin/config/graphql/servers/manage/{graphql_server}/persisted_queries'
33+
defaults:
34+
_entity_form: 'graphql_server.persisted_queries'
35+
_title: 'Persisted queries'
36+
requirements:
37+
_permission: 'administer graphql configuration'
38+
options:
39+
_admin_route: TRUE
40+
3141
graphql.explorer:
3242
path: '/admin/config/graphql/servers/manage/{graphql_server}/explorer'
3343
defaults:

graphql.services.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ services:
141141
- '\Drupal\graphql\Annotation\DataProducer'
142142
- '%graphql.config%'
143143

144+
plugin.manager.graphql.persisted_query:
145+
class: Drupal\graphql\Plugin\PersistedQueryPluginManager
146+
arguments:
147+
- 'Plugin/GraphQL/PersistedQuery'
148+
- '@container.namespaces'
149+
- '@module_handler'
150+
- '@cache.graphql.definitions'
151+
- '\Drupal\graphql\Plugin\PersistedQueryPluginInterface'
152+
- '\Drupal\graphql\Annotation\PersistedQuery'
153+
- '%graphql.config%'
154+
144155
# Buffers.
145156
graphql.buffer.entity:
146157
class: Drupal\graphql\GraphQL\Buffers\EntityBuffer

js/persisted_queries.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @file
3+
* Attaches the show/hide functionality to checkboxes in the "Persisted queries"
4+
* form.
5+
*/
6+
7+
(function ($) {
8+
9+
'use strict';
10+
11+
Drupal.behaviors.persistedQueries = {
12+
attach: function (context, settings) {
13+
$('.persisted-queries-enabled-wrapper input.form-checkbox', context).each(function () {
14+
var $checkbox = $(this);
15+
var plugin_id = $checkbox.data('id');
16+
17+
var $rows = $('.persisted-queries-weight--' + plugin_id, context);
18+
var tab = $('.persisted-queries-settings--' + plugin_id, context).data('verticalTab');
19+
20+
// Bind a click handler to this checkbox to conditionally show and hide
21+
// the processor's table row and vertical tab pane.
22+
$checkbox.on('click.persistedQueryUpdate', function () {
23+
if ($checkbox.is(':checked')) {
24+
$rows.show();
25+
if (tab) {
26+
tab.tabShow().updateSummary();
27+
}
28+
}
29+
else {
30+
$rows.hide();
31+
if (tab) {
32+
tab.tabHide().updateSummary();
33+
}
34+
}
35+
});
36+
37+
// Attach summary for configurable items (only for screen-readers).
38+
/*if (tab) {
39+
tab.details.drupalSetSummary(function () {
40+
return $checkbox.is(':checked') ? Drupal.t('Enabled') : Drupal.t('Disabled');
41+
});
42+
}*/
43+
44+
// Trigger our bound click handler to update elements to initial state.
45+
$checkbox.triggerHandler('click.persistedQueryUpdate');
46+
});
47+
}
48+
};
49+
50+
})(jQuery);

src/Annotation/PersistedQuery.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
4+
namespace Drupal\graphql\Annotation;
5+
6+
7+
use Doctrine\Common\Annotations\AnnotationException;
8+
use Drupal\Component\Annotation\Plugin;
9+
10+
/**
11+
* Annotation for persisted query plugins.
12+
*
13+
* @Annotation
14+
* @codeCoverageIgnore
15+
*/
16+
class PersistedQuery extends Plugin {
17+
18+
/**
19+
* The plugin ID.
20+
*
21+
* @var string
22+
*/
23+
public $id;
24+
25+
/**
26+
* The component label.
27+
*
28+
* @var string
29+
*/
30+
public $label;
31+
32+
/**
33+
* The component description.
34+
*
35+
* @var string
36+
*/
37+
public $description = '';
38+
39+
/**
40+
* PersistedQuery constructor.
41+
*
42+
* @param $values
43+
* The plugin annotation values.
44+
*
45+
* @throws \Doctrine\Common\Annotations\AnnotationException
46+
* In case of missing required annotation values.
47+
*/
48+
public function __construct($values) {
49+
if (!array_key_exists('id', $values) || !$values['id']) {
50+
throw new AnnotationException('The plugin is missing an "id" property.');
51+
}
52+
53+
parent::__construct($values);
54+
}
55+
}

src/Controller/ServerListBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public function getDefaultOperations(EntityInterface $entity) {
5656
];
5757
}
5858

59+
if (\Drupal::currentUser()->hasPermission("administer graphql configuration")) {
60+
$operations['persisted_queries'] = [
61+
'title' => 'Persisted queries',
62+
'weight' => 10,
63+
'url' => Url::fromRoute('entity.graphql_server.persisted_queries_form', ['graphql_server' => $id]),
64+
];
65+
}
66+
5967
return $operations;
6068
}
6169

0 commit comments

Comments
 (0)