From 0505e7f27561ab43e5da1e8491c1d56e14003edd Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Mon, 11 Apr 2022 16:41:52 +1200 Subject: [PATCH 01/12] MNT Add a license to the package --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a85a7fa..dc503f4 100644 --- a/composer.json +++ b/composer.json @@ -2,8 +2,9 @@ "name": "silverstripe/graphql-devtools", "description": "Tools to help developers building new applications on SilverStripe’s GraphQL API", "type": "silverstripe-vendormodule", + "license": "BSD-3-Clause", "require": { - "silverstripe/graphql": "*" + "silverstripe/graphql": "3 || 4" }, "autoload": { "psr-4": { From ec9312f360379b27768c03288f86181406b67977 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Mon, 11 Apr 2022 16:42:21 +1200 Subject: [PATCH 02/12] NEW Clear task for GraphQL v4 --- _config/config.yml | 4 ++++ src/Clear.php | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/Clear.php diff --git a/_config/config.yml b/_config/config.yml index 6d678e5..0a99422 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -21,6 +21,10 @@ SilverStripe\GraphQL\Dev\DevelopmentAdmin: controller: 'SilverStripe\GraphQLDevTools\Controller' links: ide: Run the GraphQL IDE + clear: + controller: SilverStripe\GraphQLDevTools\Clear + links: + clear: Clear the GraphQL schema --- Name: graphql3-devtool-routes diff --git a/src/Clear.php b/src/Clear.php new file mode 100644 index 0000000..7859027 --- /dev/null +++ b/src/Clear.php @@ -0,0 +1,39 @@ + 'clear', + ]; + + private static $allowed_actions = [ + 'clear', + ]; + + public function clear(HTTPRequest $request): void + { + + $logger = Injector::inst()->get(LoggerInterface::class . '.graphql-build'); + $dirName = CodeGenerationStore::config()->get('dirName'); + $expectedPath = BASE_PATH . DIRECTORY_SEPARATOR . $dirName; + $fs = new Filesystem(); + + $logger->info('Clearing GraphQL code generation directory'); + if ($fs->exists($expectedPath)) { + $logger->info('Directory has been found'); + $fs->remove($expectedPath); + $logger->info('Directory has been removed'); + } else { + $logger->info('Directory was not found. There is nothing to clear'); + } + + } +} From a3963856b1d479c4d84c785372849db5e2d09ae0 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Thu, 21 Apr 2022 09:41:31 +1200 Subject: [PATCH 03/12] Peer review feedback Co-authored-by: Steve Boyd --- composer.json | 2 +- src/Clear.php | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/composer.json b/composer.json index dc503f4..56a095b 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "silverstripe-vendormodule", "license": "BSD-3-Clause", "require": { - "silverstripe/graphql": "3 || 4" + "silverstripe/graphql": "^3 || ^4" }, "autoload": { "psr-4": { diff --git a/src/Clear.php b/src/Clear.php index 7859027..fbaa52b 100644 --- a/src/Clear.php +++ b/src/Clear.php @@ -20,7 +20,6 @@ class Clear extends Controller public function clear(HTTPRequest $request): void { - $logger = Injector::inst()->get(LoggerInterface::class . '.graphql-build'); $dirName = CodeGenerationStore::config()->get('dirName'); $expectedPath = BASE_PATH . DIRECTORY_SEPARATOR . $dirName; @@ -34,6 +33,5 @@ public function clear(HTTPRequest $request): void } else { $logger->info('Directory was not found. There is nothing to clear'); } - } } From 44872512082c4e60e91c7db058e367da2daf0770 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Thu, 21 Apr 2022 11:30:50 +1200 Subject: [PATCH 04/12] MNT Add tests --- .travis.yml | 4 ++++ composer.json | 12 ++++++++-- phpcs.xml.dist | 28 ++++++++++++++++++++++ phpunit.xml.dist | 18 ++++++++++++++ src/Clear.php | 17 +++++++++++-- tests/ClearTest.php | 58 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 .travis.yml create mode 100644 phpcs.xml.dist create mode 100644 phpunit.xml.dist create mode 100644 tests/ClearTest.php diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..74b9cbd --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +version: ~> 1.0 + +import: + - silverstripe/silverstripe-travis-shared:config/provision/standard-jobs-fixed.yml diff --git a/composer.json b/composer.json index dc503f4..2f0d337 100644 --- a/composer.json +++ b/composer.json @@ -4,11 +4,19 @@ "type": "silverstripe-vendormodule", "license": "BSD-3-Clause", "require": { - "silverstripe/graphql": "3 || 4" + "silverstripe/graphql": "3 || 4", + "symfony/finder": "^4 || ^5", + "symfony/filesystem": "^4 || ^5" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "squizlabs/php_codesniffer": "^3.0", + "silverstripe/versioned": "^1.0@dev" }, "autoload": { "psr-4": { - "SilverStripe\\GraphQLDevTools\\": "src/" + "SilverStripe\\GraphQLDevTools\\": "src/", + "SilverStripe\\GraphQLDevTools\\Tests\\": "tests/" } }, "extra": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..953aa54 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,28 @@ + + + CodeSniffer ruleset for SilverStripe coding conventions. + + src + tests + + */\.graphql-generated/* + + + + + + + + + + + + + + + + + + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..0281bef --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,18 @@ + + + + + tests + + + + + + src/ + + tests/ + + + + + diff --git a/src/Clear.php b/src/Clear.php index 7859027..22027be 100644 --- a/src/Clear.php +++ b/src/Clear.php @@ -7,6 +7,7 @@ use SilverStripe\Core\Injector\Injector; use SilverStripe\GraphQL\Schema\Storage\CodeGenerationStore; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; class Clear extends Controller { @@ -26,11 +27,23 @@ public function clear(HTTPRequest $request): void $expectedPath = BASE_PATH . DIRECTORY_SEPARATOR . $dirName; $fs = new Filesystem(); + $finder = new Finder(); + // Make finder not recursive + $finder->depth('== 0'); + $logger->info('Clearing GraphQL code generation directory'); if ($fs->exists($expectedPath)) { $logger->info('Directory has been found'); - $fs->remove($expectedPath); - $logger->info('Directory has been removed'); + if ($finder->in($expectedPath)->hasResults()) { + foreach ($finder as $file) { + $logger->info('Removing ' . $file->getFilename()); + $fs->remove($file->getRealPath()); + } + $logger->info('Directory now empty'); + } else { + $logger->info('Directory is already empty.'); + } + } else { $logger->info('Directory was not found. There is nothing to clear'); } diff --git a/tests/ClearTest.php b/tests/ClearTest.php new file mode 100644 index 0000000..a39b217 --- /dev/null +++ b/tests/ClearTest.php @@ -0,0 +1,58 @@ +originalDirName = CodeGenerationStore::config()->get('dirName'); + CodeGenerationStore::config()->set('dirName', $this->dirName); + $fs = new Filesystem(); + $fs->mkdir($this->absDirName); + } + + protected function tearDown(): void + { + parent::tearDown(); + CodeGenerationStore::config()->set('dirName', $this->originalDirName); + $fs = new Filesystem(); + if ($fs->exists($this->absDirName)) { + $fs->remove($this->absDirName) + } + } + + public function testClear() + { + $fs = new Filesystem(); + $finder = new Finder(); + $finder->in($this->absDirName); + + $this->assertTrue($fs->exists($this->absDirName), 'Test should begin with a fake code gen folder'); + + $this->get('dev/graphql/clear'); + $this->assertFalse($finder->hasResults(), 'GraphQL clear should not break on an empty folder'); + + $fs->mkdir($this->absDirName . DIRECTORY_SEPARATOR . 'default'); + $this->assertTrue($finder->hasResults(), 'A fake schema folder should have been created'); + + $this->get('dev/graphql/clear'); + $this->assertFalse($finder->hasResults(), 'GraphQL clear should have removed the fake schema folder'); + + $fs->remove($this->absDirName); + $this->get('dev/graphql/clear'); + $this->assertFalse($fs->exists($this->absDirName), 'GraphQL clear should not break on a non-existent folder'); + } + +} From 9aa6b7f2363f647c1f313273947f8eaacf98fb02 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Thu, 21 Apr 2022 11:48:43 +1200 Subject: [PATCH 05/12] Fix broken syntax --- tests/ClearTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/ClearTest.php b/tests/ClearTest.php index a39b217..8b6e050 100644 --- a/tests/ClearTest.php +++ b/tests/ClearTest.php @@ -6,6 +6,8 @@ use SilverStripe\GraphQL\Schema\Storage\CodeGenerationStore; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; +use SilverStripe\Core\Injector\Injector; +use SilverStripe\GraphQL\Schema\Logger; class ClearTest extends FunctionalTest { @@ -18,7 +20,9 @@ protected function setUp(): void { parent::setUp(); $this->originalDirName = CodeGenerationStore::config()->get('dirName'); + Logger::singleton()->setVerbosity(Logger::EMERGENCY); CodeGenerationStore::config()->set('dirName', $this->dirName); + $fs = new Filesystem(); $fs->mkdir($this->absDirName); } @@ -29,7 +33,7 @@ protected function tearDown(): void CodeGenerationStore::config()->set('dirName', $this->originalDirName); $fs = new Filesystem(); if ($fs->exists($this->absDirName)) { - $fs->remove($this->absDirName) + $fs->remove($this->absDirName); } } From 5f2bf2bae733c8b34ec060fa7db173cad5c07e4a Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Thu, 21 Apr 2022 12:08:50 +1200 Subject: [PATCH 06/12] Remove pointless dev requirement --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 2eaddf2..e6884d6 100644 --- a/composer.json +++ b/composer.json @@ -10,8 +10,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.5", - "squizlabs/php_codesniffer": "^3.0", - "silverstripe/versioned": "^1.0@dev" + "squizlabs/php_codesniffer": "^3.0" }, "autoload": { "psr-4": { From 5a63b45d095b480f23c89844ac071ce14b213f0e Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 18 May 2022 14:31:56 +1200 Subject: [PATCH 07/12] Only install v4 --- composer.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/composer.json b/composer.json index e6884d6..978a7b6 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "silverstripe-vendormodule", "license": "BSD-3-Clause", "require": { - "silverstripe/graphql": "^3 || ^4", + "silverstripe/graphql": ^4", "symfony/finder": "^4 || ^5", "symfony/filesystem": "^4 || ^5" }, @@ -19,9 +19,6 @@ } }, "extra": { - "branch-alias": { - "dev-master": "1.x-dev" - }, "expose": [ "client/" ] From b946f5623ece409ce5218cba64b1e41629871f1f Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 18 May 2022 14:33:52 +1200 Subject: [PATCH 08/12] iFix typo --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 978a7b6..2fd9190 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "silverstripe-vendormodule", "license": "BSD-3-Clause", "require": { - "silverstripe/graphql": ^4", + "silverstripe/graphql": "^4", "symfony/finder": "^4 || ^5", "symfony/filesystem": "^4 || ^5" }, From 1c4a0b62a2b28986816e01514d6bb231923e96bf Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 18 May 2022 20:45:11 +1200 Subject: [PATCH 09/12] Explicitely use DB --- tests/ClearTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/ClearTest.php b/tests/ClearTest.php index 8b6e050..8424006 100644 --- a/tests/ClearTest.php +++ b/tests/ClearTest.php @@ -16,6 +16,8 @@ class ClearTest extends FunctionalTest private $dirName = 'test-graphql-generated'; private $absDirName = BASE_PATH . DIRECTORY_SEPARATOR . 'test-graphql-generated'; + protected $usesDatabase = true; + protected function setUp(): void { parent::setUp(); From ee340ceadd8ca925f17f7faf23cdb2524dfef58b Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 18 May 2022 21:29:46 +1200 Subject: [PATCH 10/12] Fix broken test --- composer.json | 2 +- src/Clear.php | 1 - src/Controller.php | 1 - tests/ClearTest.php | 8 ++++++-- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 2fd9190..ca9b7cb 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "silverstripe-vendormodule", "license": "BSD-3-Clause", "require": { - "silverstripe/graphql": "^4", + "silverstripe/graphql": "^3 || ^4", "symfony/finder": "^4 || ^5", "symfony/filesystem": "^4 || ^5" }, diff --git a/src/Clear.php b/src/Clear.php index 86684d7..8562117 100644 --- a/src/Clear.php +++ b/src/Clear.php @@ -42,7 +42,6 @@ public function clear(HTTPRequest $request): void } else { $logger->info('Directory is already empty.'); } - } else { $logger->info('Directory was not found. There is nothing to clear'); } diff --git a/src/Controller.php b/src/Controller.php index da74eb8..5047321 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -117,7 +117,6 @@ protected function findAvailableRoutes($schemas = []): array } } catch (InjectorNotFoundException $ex) { } - } return $routes; } diff --git a/tests/ClearTest.php b/tests/ClearTest.php index 8424006..54c2b39 100644 --- a/tests/ClearTest.php +++ b/tests/ClearTest.php @@ -6,8 +6,8 @@ use SilverStripe\GraphQL\Schema\Storage\CodeGenerationStore; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; -use SilverStripe\Core\Injector\Injector; use SilverStripe\GraphQL\Schema\Logger; +use SilverStripe\GraphQL\Schema\Schema; class ClearTest extends FunctionalTest { @@ -21,6 +21,11 @@ class ClearTest extends FunctionalTest protected function setUp(): void { parent::setUp(); + if (!class_exists(Schema::class)) { + $this->markTestSkipped('GraphQL 4 test ' . __CLASS__ . ' skipped'); + return; + } + $this->originalDirName = CodeGenerationStore::config()->get('dirName'); Logger::singleton()->setVerbosity(Logger::EMERGENCY); CodeGenerationStore::config()->set('dirName', $this->dirName); @@ -60,5 +65,4 @@ public function testClear() $this->get('dev/graphql/clear'); $this->assertFalse($fs->exists($this->absDirName), 'GraphQL clear should not break on a non-existent folder'); } - } From 9f73902bd4fa91cc005fee6c1789a9b7219f4ae8 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Mon, 23 May 2022 21:15:16 +1200 Subject: [PATCH 11/12] Update .travis.yml Co-authored-by: Steve Boyd --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 74b9cbd..c244c3b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ version: ~> 1.0 import: - - silverstripe/silverstripe-travis-shared:config/provision/standard-jobs-fixed.yml + - silverstripe/silverstripe-travis-shared:config/provision/standard-jobs-range.yml From 5b11725bbf212b94c7a7037ff52425e176404af6 Mon Sep 17 00:00:00 2001 From: Maxime Rainville Date: Wed, 1 Jun 2022 21:30:53 +1200 Subject: [PATCH 12/12] DOC Reference the clear task in the doc --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d19569..cf0c5bd 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ This module adds an implementation of [graphiql](https://github.com/graphql/grap This is because GraphQL 4 has its own `DevelopmentAdmin` controller. +The GraphQL v4 version of the module allows you to clear your schema by calling the `/dev/graphql/clear` task. + ## Security By default, the tool has the same restrictions as other development tools like `dev/build`: @@ -101,4 +103,3 @@ SilverStripe\Control\Director: CDN to download the latest distribution and drop it into this repository. Be sure to update the comment at the top of the `bundle.js` file to track the URL it was downloaded from. -