Skip to content

Commit

Permalink
Merge pull request #140 from spawnia/generator-directives
Browse files Browse the repository at this point in the history
Cacheable schema manipulation via directives
  • Loading branch information
chrissm79 authored Jun 20, 2018
2 parents 4f8e516 + 9119e03 commit 5d43d1d
Show file tree
Hide file tree
Showing 108 changed files with 2,734 additions and 1,520 deletions.
1 change: 1 addition & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ return PhpCsFixer\Config::create()
'no_unused_imports' => false,
'ordered_imports' => true,
'phpdoc_order' => true,
'not_operator_with_successor_space' => true
])->setFinder($finder);
5 changes: 0 additions & 5 deletions assets/node.graphql

This file was deleted.

7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@
}
},
"scripts": {
"test" : "vendor/bin/phpunit --colors=always",
"test:ci": "composer test -- --verbose --coverage-text --coverage-clover=coverage.xml"
"test" : "phpunit --colors=always",
"test:unit" : "phpunit --colors=always --testsuite Unit",
"test:integration" : "phpunit --colors=always --testsuite Integration",
"test:ci": "phpunit --colors=always --verbose --coverage-text --coverage-clover=coverage.xml",
"style": "php-cs-fixer fix"
},
"extra": {
"laravel": {
Expand Down
15 changes: 9 additions & 6 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
// 'middleware' => ['web','api'], // [ 'loghttp']
],


/*
|--------------------------------------------------------------------------
| Directive registry
|--------------------------------------------------------------------------
|
| This package allows you to create your own server-side directives. Change
| these values to register the directory that will hold all of your
| custom directives.
| This package allows you to create your own server-side directives.
| List directories that will be scanned for custom directives.
| Hint: Directives must implement \Nuwave\Lighthouse\Schema\Directives\Directive
|
*/
'directives' => [__DIR__.'/../app/Http/GraphQL/Directives'],
Expand Down Expand Up @@ -67,10 +66,14 @@
| Schema Cache
|--------------------------------------------------------------------------
|
| Specify where the GraphQL schema should be cached.
| A large part of the Schema generation is parsing into an AST.
| This operation is cached by default when APP_ENV is set to 'production'
|
*/
'cache' => null,
'cache' => [
'enable' => true,
'key' => 'lighthouse-schema',
],

/*
|--------------------------------------------------------------------------
Expand Down
10 changes: 6 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory>./tests/</directory>
<testsuite name="Unit">
<directory>./tests/Unit/</directory>
</testsuite>
<testsuite name="Integration">
<directory>./tests/Integration/</directory>
</testsuite>
</testsuites>
<filter>
Expand All @@ -27,7 +29,7 @@
</filter>
<logging>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
Expand Down
157 changes: 86 additions & 71 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@

use GraphQL\GraphQL as GraphQLBase;
use GraphQL\Type\Schema;
use Nuwave\Lighthouse\Schema\CacheManager;
use Nuwave\Lighthouse\Schema\Factories\DirectiveFactory;
use Nuwave\Lighthouse\Schema\AST\ASTBuilder;
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
use Nuwave\Lighthouse\Schema\AST\SchemaStitcher;
use Nuwave\Lighthouse\Schema\DirectiveRegistry;
use Nuwave\Lighthouse\Schema\MiddlewareManager;
use Nuwave\Lighthouse\Schema\NodeContainer;
use Nuwave\Lighthouse\Schema\SchemaBuilder;
use Nuwave\Lighthouse\Schema\Utils\SchemaStitcher;
use Nuwave\Lighthouse\Schema\TypeRegistry;
use Nuwave\Lighthouse\Support\Traits\CanFormatError;

class GraphQL
{
use CanFormatError;

/**
* Cache manager.
*
* @var CacheManager
*/
protected $cache;

/**
* Schema builder.
*
Expand All @@ -31,12 +26,26 @@ class GraphQL
protected $schema;

/**
* Directive container.
* Directive registry container.
*
* @var DirectiveFactory
* @var DirectiveRegistry
*/
protected $directives;

/**
* Type registry container.
*
* @var TypeRegistry
*/
protected $types;

/**
* Instance of node container.
*
* @var NodeContainer
*/
protected $nodes;

/**
* Middleware manager.
*
Expand All @@ -58,12 +67,32 @@ class GraphQL
*/
protected $graphqlSchema;

/**
* Create instance of graphql container.
*
* @param DirectiveRegistry $directives
* @param TypeRegistry $types
* @param MiddlewareManager $middleware
* @param NodeContainer $nodes
*/
public function __construct(
DirectiveRegistry $directives,
TypeRegistry $types,
MiddlewareManager $middleware,
NodeContainer $nodes
) {
$this->directives = $directives;
$this->types = $types;
$this->middleware = $middleware;
$this->nodes = $nodes;
}

/**
* Prepare graphql schema.
*/
public function prepSchema()
{
$this->graphqlSchema = $this->graphqlSchema ?: $this->buildSchema();
return $this->graphqlSchema = $this->graphqlSchema ?: $this->buildSchema();
}

/**
Expand Down Expand Up @@ -124,20 +153,43 @@ public function queryAndReturnResult($query, $context = null, $variables = [], $
}

/**
* Build a new schema instance.
* Build a new executable schema.
*
* @return Schema
*/
public function buildSchema()
{
$schema = $this->cache()->get(function () {
return $this->stitcher()->stitch(
config('lighthouse.global_id_field', '_id'),
config('lighthouse.schema.register')
);
});

return $this->schema()->build($schema);
$documentAST = $this->shouldCacheAST()
? Cache::rememberForever(config('lighthouse.cache.key'), function () {
return $this->buildAST();
})
: $this->buildAST();

return (new SchemaBuilder())->build($documentAST);
}

/**
* Determine if the AST should be cached.
*
* @return bool
*/
protected function shouldCacheAST()
{
return app()->environment('production') && config('cache.enable');
}

/**
* Get the stitched schema and build an AST out of it.
*
* @return DocumentAST
*/
protected function buildAST()
{
$schemaString = SchemaStitcher::stitch(
config('lighthouse.schema.register')
);

return ASTBuilder::generate($schemaString);
}

/**
Expand All @@ -163,71 +215,41 @@ public function batch($abstract, $key, array $data = [], $name = null)
/**
* Get an instance of the schema builder.
*
* @return SchemaBuilder
* @return TypeRegistry
*/
public function schema()
{
if (! $this->schema) {
$this->schema = app(SchemaBuilder::class);
}

return $this->schema;
return $this->types();
}

/**
* Get an instance of the directive container.
*
* @return DirectiveFactory
* @return DirectiveRegistry
*/
public function directives()
{
if (! $this->directives) {
$this->directives = app(DirectiveFactory::class);
}

return $this->directives;
}

/**
* Get instance of middle manager.
* Get instance of type container.
*
* @return MiddlewareManager
* @return TypeRegistry
*/
public function middleware()
public function types()
{
if (! $this->middleware) {
$this->middleware = app(MiddlewareManager::class);
}

return $this->middleware;
return $this->types;
}

/**
* Get instance of cache manager.
*
* @return CacheManager
*/
public function cache()
{
if (! $this->cache) {
$this->cache = app(CacheManager::class);
}

return $this->cache;
}

/**
* Get instance of schema stitcher.
* Get instance of middle manager.
*
* @return SchemaStitcher
* @return MiddlewareManager
*/
public function stitcher()
public function middleware()
{
if (! $this->stitcher) {
$this->stitcher = app(SchemaStitcher::class);
}

return $this->stitcher;
return $this->middleware;
}

/**
Expand All @@ -237,13 +259,6 @@ public function stitcher()
*/
public function nodes()
{
if (! app()->has(NodeContainer::class)) {
return app()->instance(
NodeContainer::class,
resolve(NodeContainer::class)
);
}

return resolve(NodeContainer::class);
return $this->nodes;
}
}
39 changes: 13 additions & 26 deletions src/Providers/LighthouseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ public function boot()
$this->loadRoutesFrom(__DIR__.'/../Support/Http/routes.php');
}

$this->registerSchema();
$this->registerMacros();
}

/**
* Load routes from provided path.
*
* @param string $path
*/
protected function loadRoutesFrom($path)
{
if(Str::contains($this->app->version(), "Lumen")) {
require realpath($path);
return;
if (Str::contains($this->app->version(), 'Lumen')) {
require realpath($path);

return;
}

parent::loadRoutesFrom($path);
}

Expand All @@ -40,33 +46,14 @@ protected function loadRoutesFrom($path)
*/
public function register()
{
$this->app->singleton('graphql', function () {
return new GraphQL();
});

$this->app->alias('graphql', GraphQL::class);
$this->app->singleton(GraphQL::class);
$this->app->alias(GraphQL::class, 'graphql');

if ($this->app->runningInConsole()) {
$this->commands([
\Nuwave\Lighthouse\Support\Console\Commands\CacheCommand::class,
]);
$this->commands([]);
}
}

/**
* Register GraphQL schema.
*/
public function registerSchema()
{
directives()->load(realpath(__DIR__.'/../Schema/Directives/'), 'Nuwave\\Lighthouse\\');
directives()->load(config('lighthouse.directives', []));

graphql()->stitcher()->stitch(
config('lighthouse.global_id_field', '_id'),
config('lighthouse.schema.register')
);
}

/**
* Register lighthouse macros.
*/
Expand Down
Loading

0 comments on commit 5d43d1d

Please sign in to comment.