From eb832b4c9534e1673b2918cb2fe0240ef4fb5df7 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Sat, 6 Apr 2024 18:36:24 +0200 Subject: [PATCH 1/4] test(APQ): Simplify dynamic page cache test code --- src/EventSubscriber/ApqSubscriber.php | 1 + ...icPersistedQueriesDynamicPageCacheTest.php | 31 ++++++++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/EventSubscriber/ApqSubscriber.php b/src/EventSubscriber/ApqSubscriber.php index ada2b53dd..a9853c07b 100644 --- a/src/EventSubscriber/ApqSubscriber.php +++ b/src/EventSubscriber/ApqSubscriber.php @@ -48,6 +48,7 @@ public function onBeforeOperation(OperationEvent $event): void { // Add cache context for dynamic page cache compatibility on all // operations that have the hash set. $event->getContext()->addCacheContexts( + //['url.query_args'] ['url.query_args:variables', 'url.query_args:extensions'] ); diff --git a/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php b/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php index 16ee28767..7dbd8298a 100644 --- a/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php +++ b/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php @@ -8,7 +8,7 @@ use Symfony\Component\HttpFoundation\Request; /** - * Tests the automatic persisted query plugin. + * Tests the APQ plugin in combination with dynamic page cache. * * @group graphql */ @@ -108,31 +108,32 @@ public function testPageCacheWithDifferentVariables(): void { $idQuery = 'query($id: String!) { node(id: $id) { id } }'; // Post query to endpoint to register the query hashes. - $parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery); - $parameters['variables'] = '{"id": "2"}'; - $content = json_encode(['query' => $titleQuery] + $parameters); - $request = Request::create($endpoint, 'POST', [], [], [], ['CONTENT_TYPE' => 'application/json'], $content); - $result = $this->container->get('http_kernel')->handle($request); + $extensions['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery); + $variables = ['id' => '2']; + $result = $this->query($titleQuery, $this->server, $variables, $extensions, FALSE, Request::METHOD_POST); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['node' => ['title' => 'Node 2']]], json_decode($result->getContent(), TRUE)); - $parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $idQuery); - $parameters['variables'] = '{"id": "2"}'; - $content = json_encode(['query' => $idQuery] + $parameters); - $request = Request::create($endpoint, 'POST', [], [], [], ['CONTENT_TYPE' => 'application/json'], $content); - $result = $this->container->get('http_kernel')->handle($request); + $extensions['persistedQuery']['sha256Hash'] = hash('sha256', $idQuery); + $variables = ['id' => '2']; + $result = $this->query($idQuery, $this->server, $variables, $extensions, FALSE, Request::METHOD_POST); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['node' => ['id' => 2]]], json_decode($result->getContent(), TRUE)); // Execute apq call. - $parameters['variables'] = '{"id": "1"}'; + /*$parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $idQuery); + $parameters['variables'] = ['id' => '1']; $request = Request::create($endpoint, 'GET', $parameters); - $result = $this->container->get('http_kernel')->handle($request); + $result = $this->container->get('http_kernel')->handle($request);*/ + + $variables = ['id' => '1']; + $this->query($idQuery, $this->server, $variables, $extensions); + $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['node' => ['id' => 1]]], json_decode($result->getContent(), TRUE)); // Execute apq call with different variables. - $parameters['variables'] = '{"id": "2"}'; + /*$parameters['variables'] = '{"id": "2"}'; $request = Request::create($endpoint, 'GET', $parameters); $result = $this->container->get('http_kernel')->handle($request); $this->assertSame(200, $result->getStatusCode()); @@ -144,7 +145,7 @@ public function testPageCacheWithDifferentVariables(): void { $request = Request::create($endpoint, 'GET', $parameters); $result = $this->container->get('http_kernel')->handle($request); $this->assertSame(200, $result->getStatusCode()); - $this->assertSame(['data' => ['node' => ['title' => 'Node 2']]], json_decode($result->getContent(), TRUE)); + $this->assertSame(['data' => ['node' => ['title' => 'Node 2']]], json_decode($result->getContent(), TRUE));*/ } From d3417bba971e9d7058a588cd891e406390fca07c Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Mon, 8 Apr 2024 17:07:38 +0200 Subject: [PATCH 2/4] fixed test case result --- ...icPersistedQueriesDynamicPageCacheTest.php | 24 ++++++------------- .../AutomaticPersistedQueriesTest.php | 4 ++-- tests/src/Traits/HttpRequestTrait.php | 16 +++++++------ 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php b/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php index 7dbd8298a..4495487ee 100644 --- a/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php +++ b/tests/src/Kernel/Framework/AutomaticPersistedQueriesDynamicPageCacheTest.php @@ -83,7 +83,6 @@ public function testPageCacheWithDifferentVariables(): void { $this->server->removeAllPersistedQueryInstances(); $this->server->addPersistedQueryInstance($this->pluginApq); $this->server->save(); - $endpoint = $this->server->get('endpoint'); NodeType::create([ 'type' => 'test', @@ -121,32 +120,23 @@ public function testPageCacheWithDifferentVariables(): void { $this->assertSame(['data' => ['node' => ['id' => 2]]], json_decode($result->getContent(), TRUE)); // Execute apq call. - /*$parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $idQuery); - $parameters['variables'] = ['id' => '1']; - $request = Request::create($endpoint, 'GET', $parameters); - $result = $this->container->get('http_kernel')->handle($request);*/ - $variables = ['id' => '1']; - $this->query($idQuery, $this->server, $variables, $extensions); - + $result = $this->query(NULL, $this->server, $variables, $extensions); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['node' => ['id' => 1]]], json_decode($result->getContent(), TRUE)); // Execute apq call with different variables. - /*$parameters['variables'] = '{"id": "2"}'; - $request = Request::create($endpoint, 'GET', $parameters); - $result = $this->container->get('http_kernel')->handle($request); + $variables = ['id' => '2']; + $result = $this->query(NULL, $this->server, $variables, $extensions); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['node' => ['id' => 2]]], json_decode($result->getContent(), TRUE)); // Execute apq call with same parameters, but different query. - $parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery); - $parameters['variables'] = '{"id": "2"}'; - $request = Request::create($endpoint, 'GET', $parameters); - $result = $this->container->get('http_kernel')->handle($request); + $extensions['persistedQuery']['sha256Hash'] = hash('sha256', $titleQuery); + $variables = ['id' => '2']; + $result = $this->query(NULL, $this->server, $variables, $extensions); $this->assertSame(200, $result->getStatusCode()); - $this->assertSame(['data' => ['node' => ['title' => 'Node 2']]], json_decode($result->getContent(), TRUE));*/ - + $this->assertSame(['data' => ['node' => ['title' => 'Node 2']]], json_decode($result->getContent(), TRUE)); } } diff --git a/tests/src/Kernel/Framework/AutomaticPersistedQueriesTest.php b/tests/src/Kernel/Framework/AutomaticPersistedQueriesTest.php index fd06fc74c..b0d158355 100644 --- a/tests/src/Kernel/Framework/AutomaticPersistedQueriesTest.php +++ b/tests/src/Kernel/Framework/AutomaticPersistedQueriesTest.php @@ -64,7 +64,7 @@ public function testAutomaticPersistedQueries(): void { $extensions = ['persistedQuery' => ['sha256Hash' => 'some random hash']]; // Check we get PersistedQueryNotFound. - $result = $this->query('', $this->server, [], $extensions); + $result = $this->query(NULL, $this->server, [], $extensions); $this->assertSame(200, $result->getStatusCode()); $this->assertSame([ @@ -97,7 +97,7 @@ public function testAutomaticPersistedQueries(): void { $this->assertSame(['data' => ['field_one' => 'this is the field one']], json_decode($result->getContent(), TRUE)); // Execute first GET request again. - $result = $this->query($query, $this->server, [], $extensions); + $result = $this->query(NULL, $this->server, [], $extensions); $this->assertSame(200, $result->getStatusCode()); $this->assertSame(['data' => ['field_one' => 'this is the field one']], json_decode($result->getContent(), TRUE)); } diff --git a/tests/src/Traits/HttpRequestTrait.php b/tests/src/Traits/HttpRequestTrait.php index b7b4174bb..9b06c5b9b 100644 --- a/tests/src/Traits/HttpRequestTrait.php +++ b/tests/src/Traits/HttpRequestTrait.php @@ -21,8 +21,8 @@ trait HttpRequestTrait { /** * Issue a simple query over http. * - * @param string $query - * The query string. + * @param string|null $query + * The query string. Can be omitted when testing auto persisted queries. * @param \Drupal\graphql\Entity\Server|null $server * The server instance. * @param array $variables @@ -40,7 +40,7 @@ trait HttpRequestTrait { * The http response object. */ protected function query( - string $query, + ?string $query, ?Server $server = NULL, array $variables = [], array $extensions = [], @@ -51,13 +51,15 @@ protected function query( $server = $server ?: $this->server; $endpoint = $this->server->get('endpoint'); $extensions = !empty($extensions) ? ['extensions' => $extensions] : []; - // If the persisted flag is true, then instead of sending the full query to - // the server we only send the query id. - $query_key = $persisted ? 'queryId' : 'query'; $data = [ - $query_key => $query, 'variables' => $variables, ] + $extensions; + if (!empty($query)) { + // If the persisted flag is true, then instead of sending the full query to + // the server we only send the query id. + $query_key = $persisted ? 'queryId' : 'query'; + $data[$query_key] = $query; + } if ($operationName) { $data['operationName'] = $operationName; } From b6576a38ce5797c500d26d5c2839d96cec91267f Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Mon, 8 Apr 2024 17:09:40 +0200 Subject: [PATCH 3/4] remove debug code --- src/EventSubscriber/ApqSubscriber.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/EventSubscriber/ApqSubscriber.php b/src/EventSubscriber/ApqSubscriber.php index a9853c07b..ada2b53dd 100644 --- a/src/EventSubscriber/ApqSubscriber.php +++ b/src/EventSubscriber/ApqSubscriber.php @@ -48,7 +48,6 @@ public function onBeforeOperation(OperationEvent $event): void { // Add cache context for dynamic page cache compatibility on all // operations that have the hash set. $event->getContext()->addCacheContexts( - //['url.query_args'] ['url.query_args:variables', 'url.query_args:extensions'] ); From 9d5eaf6ee2d9c789acd1e91504ae70145ba9fcbe Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Mon, 8 Apr 2024 17:14:15 +0200 Subject: [PATCH 4/4] phpcs --- tests/src/Traits/HttpRequestTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/Traits/HttpRequestTrait.php b/tests/src/Traits/HttpRequestTrait.php index 9b06c5b9b..e5ac05e6a 100644 --- a/tests/src/Traits/HttpRequestTrait.php +++ b/tests/src/Traits/HttpRequestTrait.php @@ -55,8 +55,8 @@ protected function query( 'variables' => $variables, ] + $extensions; if (!empty($query)) { - // If the persisted flag is true, then instead of sending the full query to - // the server we only send the query id. + // If the persisted flag is true, then instead of sending the full query + // to the server we only send the query id. $query_key = $persisted ? 'queryId' : 'query'; $data[$query_key] = $query; }