Skip to content

Commit

Permalink
NEW Refactor to not require sake
Browse files Browse the repository at this point in the history
There's no straightforward way to have sake run without a database connection.
When using a "fake" HTTPApplication for CLI execution, it executes middleware which rightfully assumes a database connection (e.g. fluent is getting available languages to modify routing).

Relies on silverstripe/silverstripe-framework#9977
  • Loading branch information
chillu committed Jun 13, 2021
1 parent e9d52a5 commit 7cb6bb9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ it is a requirement of `silverstripe/graphql`.
## Configuration

The plugin runs `sake dev/graphql/build` by default, which builds all schemas.
Set an `SS_GRAPHQL_COMPOSER_CMD` environment constant in order to customise this.
Set an `SS_GRAPHQL_COMPOSER_BUILD_SCHEMAS` environment constant in order to influence this behaviour.
You can either limit this to a specific schema
([details](https://docs.silverstripe.org/en/4/developer_guides/graphql/getting_started/building_the_schema/)),
or set it to an empty string to disable execution.
40 changes: 26 additions & 14 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php

namespace SilverStripe\GraphQLComposerPlugin;

use Composer\Composer;
use Composer\Plugin\PluginInterface;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Script\Event;
use Composer\IO\IOInterface;
use SilverStripe\Core\CoreKernel;
use SilverStripe\GraphQL\Schema\Exception\EmptySchemaException;
use SilverStripe\GraphQL\Schema\Schema;
use SilverStripe\GraphQL\Schema\SchemaBuilder;
use SilverStripe\GraphQL\Schema\Storage\CodeGenerationStoreCreator;

//require_once __DIR__ . '/../../../autoload.php';
require_once __DIR__ . '/../../../silverstripe/framework/src/includes/constants.php';

class Plugin implements PluginInterface, EventSubscriberInterface
{
Expand Down Expand Up @@ -44,21 +47,30 @@ public function uninstall(Composer $composer, IOInterface $io)

public function generateSchema(Event $event)
{
$schemas = getenv('SS_GRAPHQL_COMPOSER_BUILD_SCHEMAS');
// TODO Filter by schema

// vendor/bin is temorarily pushed on top of PATH, see https://getcomposer.org/doc/articles/scripts.md
$cmd = defined('SS_GRAPHQL_COMPOSER_CMD') ? SS_GRAPHQL_COMPOSER_CMD : 'sake dev/graphql/build';
// Not using sake because it creates a HTTPApplication through cli-script.php.
// This would trigger middlewares assuming a HTTP request execution
// (rather than CLI), which then connect to the database that might not be available during this build.
$kernel = new CoreKernel(BASE_PATH, false);
$kernel->boot();

// Allow disabling by null'ing the env var
if (!$cmd) {
return;
}

// title() and section() exist in symfony/console, but not through IOInterface
$this->io->write('<info>######################################################</info>');
$this->io->write('<info>silverstripe/graphql-composer-plugin: Building schemas</info>');
$this->io->write('<info>######################################################</info>');

$out = shell_exec($cmd);
$this->io->write($out);
$keys = array_keys(Schema::config()->get('schemas'));
$keys = array_filter($keys, function ($key) {
return $key !== Schema::ALL;
});
foreach ($keys as $key) {
$builder = SchemaBuilder::singleton();
$schema = $builder->boot($key);
$this->io->write(sprintf('Building schema "%s"', $key));
try {
$builder->build($schema);
} catch (EmptySchemaException $e) {
$this->io->write('error');
}
}
}
}

0 comments on commit 7cb6bb9

Please sign in to comment.