Skip to content

Commit

Permalink
API Update API to reflect changes to CLI interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 4, 2024
1 parent ef4cbfe commit e7e982a
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 215 deletions.
17 changes: 4 additions & 13 deletions _config/dev.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
---
Name: graphql-dev
---
SilverStripe\ORM\DatabaseAdmin:
SilverStripe\Dev\Command\DbBuild:
extensions:
- SilverStripe\GraphQL\Extensions\DevBuildExtension
- SilverStripe\GraphQL\Extensions\DbBuildExtension

SilverStripe\Dev\DevelopmentAdmin:
registered_controllers:
graphql:
controller: SilverStripe\GraphQL\Dev\DevelopmentAdmin
links:
graphql: 'List GraphQL development tools'
SilverStripe\GraphQL\Dev\DevelopmentAdmin:
registered_controllers:
build:
controller: SilverStripe\GraphQL\Dev\Build
links:
build: Build the GraphQL schema
commands:
'graphql/build': 'SilverStripe\GraphQL\Dev\Build'
2 changes: 1 addition & 1 deletion _config/logging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ after: '#logging'
---

SilverStripe\Core\Injector\Injector:
# Omits the HTTPOutputHandler from the logger so errors won't appear in output
# Omits the ErrorOutputHandler from the logger so errors won't appear in output
Psr\Log\LoggerInterface.graphql-quiet:
type: singleton
class: Monolog\Logger
Expand Down
100 changes: 64 additions & 36 deletions src/Dev/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
namespace SilverStripe\GraphQL\Dev;

use Psr\Log\LoggerInterface;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\DebugView;
use SilverStripe\Dev\Command\DevCommand;
use SilverStripe\Dev\DevelopmentAdmin;
use SilverStripe\HybridExecution\HybridOutput;
use SilverStripe\GraphQL\Schema\DataObject\FieldAccessor;
use SilverStripe\GraphQL\Schema\Exception\EmptySchemaException;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
Expand All @@ -18,38 +17,44 @@
use SilverStripe\GraphQL\Schema\SchemaBuilder;
use SilverStripe\GraphQL\Schema\Storage\CodeGenerationStore;
use SilverStripe\ORM\Connect\NullDatabaseException;
use SilverStripe\Security\PermissionProvider;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

class Build extends Controller
class Build extends DevCommand implements PermissionProvider
{
private static $url_handlers = [
'' => 'build'
];
protected static string $commandName = 'graphql:build';

protected static string $description = 'Build the GraphQL schema(s)';

private static $allowed_actions = [
'build'
private static array $permissions_for_browser_execution = [
'CAN_DEV_GRAPHQL',
];

/**
* @throws SchemaBuilderException
* @throws SchemaNotFoundException
*/
public function build(HTTPRequest $request): void
public function getTitle(): string
{
$isBrowser = !Director::is_cli();
if ($isBrowser) {
$renderer = DebugView::create();
echo $renderer->renderHeader();
echo $renderer->renderInfo("GraphQL Schema Builder", Director::absoluteBaseURL());
echo "<div class=\"build\">";
}
$clear = true;

$this->buildSchema($request->getVar('schema'), $clear);
return 'GraphQL Schema Builder';
}

if ($isBrowser) {
echo "</div>";
echo $renderer->renderFooter();
protected function execute(InputInterface $input, HybridOutput $output): int
{
$originalLogger = Injector::inst()->get(LoggerInterface::class . '.graphql-build');
try {
$logger = Logger::singleton();
$logger->setOutput($output);
Injector::inst()->registerService($logger, LoggerInterface::class . '.graphql-build');
$this->buildSchema($input->getOption('schema'), true, $output);
} finally {
// Restore default logger back to its starting state
Injector::inst()->registerService($originalLogger, LoggerInterface::class . '.graphql-build');
}
return Command::SUCCESS;
}

protected function getHeading(): string
{
return '';
}

/**
Expand Down Expand Up @@ -97,15 +102,15 @@ public function buildSchema(string $key = null, bool $clear = true): void
break;
}
}
$logger->warning("
Your schema configuration requires access to the database. This can happen
$logger->warning(
"Your schema configuration requires access to the database. This can happen
when you add fields that require type introspection (i.e. custom getters).
It is recommended that you specify an explicit type when adding custom getters
to your schema.");
to your schema."
);
if ($candidate) {
$logger->warning(sprintf(
"
This most likely happened when you tried to add the field '%s' to '%s'",
"This most likely happened when you tried to add the field '%s' to '%s'",
$candidate['args'][1],
get_class($candidate['args'][0])
));
Expand All @@ -114,9 +119,32 @@ public function buildSchema(string $key = null, bool $clear = true): void
throw $e;
}

$logger->info(
Benchmark::end('build-schema-' . $key, 'Built schema in %sms.')
);
$logger->info(Benchmark::end('build-schema-' . $key, 'Built schema in %sms.'));
}
}

public function getOptions(): array
{
return [
new InputOption(
'schema',
null,
InputOption::VALUE_REQUIRED,
'The name of the schema to be built. If not passed, all schemas will be built',
suggestedValues: array_keys(Schema::config()->get('schemas') ?? [])
)
];
}

public function providePermissions(): array
{
return [
'CAN_DEV_GRAPHQL' => [
'name' => _t(__CLASS__ . '.CAN_DEV_GRAPHQL_DESCRIPTION', 'Can view and execute /dev/graphql'),
'help' => _t(__CLASS__ . '.CAN_DEV_GRAPHQL_HELP', 'Can view and execute GraphQL development tools (/dev/graphql).'),
'category' => DevelopmentAdmin::permissionsCategory(),
'sort' => 80
],
];
}
}
146 changes: 0 additions & 146 deletions src/Dev/DevelopmentAdmin.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@

use Psr\Log\LoggerInterface;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Command\DbBuild;
use SilverStripe\HybridExecution\HybridOutput;
use SilverStripe\GraphQL\Dev\Build;
use SilverStripe\GraphQL\Schema\Logger;
use SilverStripe\ORM\DatabaseAdmin;
use SilverStripe\Core\Extension;

/**
* @extends Extension<DatabaseAdmin>
* @extends Extension<DbBuild>
*/
class DevBuildExtension extends Extension
class DbBuildExtension extends Extension
{
use Configurable;

Expand All @@ -23,7 +24,7 @@ class DevBuildExtension extends Extension
*/
private static bool $enabled = true;

protected function onAfterBuild(): void
protected function onAfterBuild(HybridOutput $output): void
{
if (!static::config()->get('enabled')) {
return;
Expand All @@ -35,7 +36,7 @@ protected function onAfterBuild(): void
try {
// Define custom logger
$logger = Logger::singleton();
$logger->setVerbosity(Logger::INFO);
$logger->setOutput($output);
Injector::inst()->registerService($logger, LoggerInterface::class . '.graphql-build');

Build::singleton()->buildSchema();
Expand Down
7 changes: 7 additions & 0 deletions src/Extensions/TestSessionEnvironmentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace SilverStripe\GraphQL\Extensions;

use SilverStripe\Control\Director;
use SilverStripe\Core\Extension;
use SilverStripe\GraphQL\Schema\Logger;
use SilverStripe\GraphQL\Schema\Schema;
use SilverStripe\GraphQL\Schema\SchemaBuilder;
use SilverStripe\GraphQL\Schema\Exception\EmptySchemaException;
use SilverStripe\GraphQL\Dev\Benchmark;
use SilverStripe\HybridExecution\HybridOutput;
use SilverStripe\TestSession\TestSessionEnvironment;

/**
Expand All @@ -24,7 +26,12 @@ class TestSessionEnvironmentExtension extends Extension
*/
protected function onAfterStartTestSession(): void
{
$output = HybridOutput::create(
Director::is_cli() ? HybridOutput::FORMAT_ANSI : HybridOutput::FORMAT_HTML,
HybridOutput::VERBOSITY_QUIET
);
$logger = Logger::singleton();
$logger->setOutput($output);
$keys = array_keys(Schema::config()->get('schemas') ?? []);
$keys = array_filter($keys ?? [], function ($key) {
return $key !== Schema::ALL;
Expand Down
Loading

0 comments on commit e7e982a

Please sign in to comment.