Skip to content

Commit

Permalink
Revisions per review
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Jan 27, 2022
1 parent ff3e285 commit 677c216
Showing 1 changed file with 52 additions and 23 deletions.
75 changes: 52 additions & 23 deletions src/Dev/Initialise.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
<?php


namespace SilverStripe\GraphQL\Dev;

use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Manifest\ModuleManifest;
use SilverStripe\Core\Path;
use SilverStripe\Dev\DebugView;
use SilverStripe\GraphQL\Schema\DataObject\FieldAccessor;
use SilverStripe\GraphQL\Schema\Exception\EmptySchemaException;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Exception\SchemaNotFoundException;
use SilverStripe\GraphQL\Schema\Schema;
use SilverStripe\GraphQL\Schema\SchemaBuilder;
use SilverStripe\ORM\Connect\NullDatabaseException;

/**
* Class Initialise
* @package SilverStripe\GraphQL\Dev
* A task that initialises a schema with boilerplate config and files.
*/
class Initialise extends Controller
{
Expand Down Expand Up @@ -65,7 +57,7 @@ class Initialise extends Controller
/**
* @var string
*/
private $projectDir = 'app';
private $projectDir = '';

/**
* @var string
Expand All @@ -75,7 +67,7 @@ class Initialise extends Controller
/**
* @var string
*/
private $perms = '0777';
private $perms = '';

/**
* @param HTTPRequest $request
Expand All @@ -87,16 +79,23 @@ public function initialise(HTTPRequest $request)
!$isBrowser,
'This task can only be run from CLI'
);

if ($request->getVar('help')) {
$this->showHelp();
return;
}

$appNamespace = $request->getVar('namespace');
Schema::invariant(
$appNamespace,
'Please provide a base namespace for your app, e.g. "namespace=App" or "namespace=MyVendor\MyProject"'
);

if (!$appNamespace) {
echo "Please provide a base namespace for your app, e.g. \"namespace=App\" or \"namespace=MyVendor\MyProject\".\nFor help, run \"dev/graphql/init help=1\"\n";
return;
}

$this->appNamespace = $appNamespace;

$this->projectDir = ModuleManifest::config()->get('project');


$schemaName = $request->getVar('name');
if ($schemaName) {
$this->schemaName = $schemaName;
Expand Down Expand Up @@ -140,13 +139,14 @@ private function createGraphQLConfig(): void
echo "Graphql config directory already exists. Skipping." . PHP_EOL;
return;
}
echo "Creating graphql config directory: $this->graphqlConfigDir" . PHP_EOL;
mkdir($absGraphQLDir, $this->perms);

echo "Creating graphql config directory: $this->graphqlConfigDir" . PHP_EOL;
mkdir($absGraphQLDir, $this->perms);
foreach (['models', 'config', 'types', 'queries', 'mutations'] as $file) {
touch(Path::join($absGraphQLDir, "$file.yml"));
}
$configPath = Path::join($absGraphQLDir, 'config.yml');
$defaultConfig = <<<YAML
$configPath = Path::join($absGraphQLDir, 'config.yml');
$defaultConfig = <<<YAML
resolvers:
- $this->appNamespace\Resolvers
YAML;
Expand All @@ -172,6 +172,7 @@ private function createProjectConfig(): void
$this->schemaName:
src:
- $this->projectDir/$this->graphqlConfigDir
YAML;
file_put_contents($absConfigFile, $defaultProjectConfig);
}
Expand Down Expand Up @@ -206,16 +207,44 @@ private function createResolvers(): void
*/
class Resolvers
{
public static function resolveMyQuery(\$obj, array \$args, \$context): array
public static function resolveMyQuery(\$obj, array \$args, array \$context): array
{
// Return the result of query { myQuery { ... } }
return [];
}
}
PHP;
file_put_contents($resolverFile, $resolverCode);
}

/**
* Outputs help text to the console
*/
private function showHelp(): void
{
echo <<<TXT
****
This task executes a lot of the boilerplate required to build a new GraphQL schema. It will
generate a few files in your project directory. Any files that already exist will not be
overwritten. The task can be run multiple times and is non-destructive.
****
-- Example:
$ vendor/bin/sake dev/graphql/init namespace="MyAgency\MyApp"
-- Arguments:
[namespace]: The root namespace. Required.
<name>: The name of the schema. Default: "default"
<graphqlConfigDir>: The folder where the flushless graphql config files will go. Default: "_graphql"
<graphqlCodeDir>: The subfolder of src/ where your GraphQL code (the resolver class) will go. Follows PSR-4 based on the namespace argument (default: "GraphQL")
TXT;
}
}

0 comments on commit 677c216

Please sign in to comment.