Skip to content

Commit

Permalink
API Update graphql to use new config (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman authored and chillu committed Feb 27, 2017
1 parent 4805d63 commit 72b9d61
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
4 changes: 2 additions & 2 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Name: graphqlconfig
SilverStripe\GraphQL\Scaffolding\Scaffolders\DataObjectScaffolder:
default_fields:
ID: ID

## Map DB fields to GraphQL types
SilverStripe\ORM\FieldType\DBField:
# fallback
Expand All @@ -26,7 +26,7 @@ SilverStripe\ORM\FieldType\DBForeignKey:
graphql_type: 'ID'

## CORS default config
SilverStripe\GraphQL:
SilverStripe\GraphQL\Controller:
cors:
Enabled: false # Off by default
Allow-Origin: # Deny all by default
Expand Down
7 changes: 3 additions & 4 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use SilverStripe\ORM\Versioning\Versioned;
use Exception;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;

/**
* Top level controller for handling graphql requests.
Expand Down Expand Up @@ -39,7 +38,7 @@ public function index(HTTPRequest $request)

// Check for a possible CORS preflight request and handle if necessary
// Refer issue 66: https://github.com/silverstripe/silverstripe-graphql/issues/66
$corsConfig = Config::inst()->get('SilverStripe\GraphQL', 'cors');
$corsConfig = Config::inst()->get(self::class, 'cors');
$corsEnabled = true; // Default to have CORS turned on.

if ($corsConfig && isset($corsConfig['Enabled']) && !$corsConfig['Enabled']) {
Expand Down Expand Up @@ -121,7 +120,7 @@ public function getManager()
}

// Get a service rather than an instance (to allow procedural configuration)
$config = Config::inst()->get('SilverStripe\GraphQL', 'schema');
$config = Config::inst()->get(static::class, 'schema');
$manager = Manager::createFromConfig($config);

return $manager;
Expand Down Expand Up @@ -154,7 +153,7 @@ public function getAuthHandler()
*/
public function addCorsHeaders(HTTPRequest $request, HTTPResponse $response)
{
$corsConfig = Config::inst()->get('SilverStripe\GraphQL', 'cors');
$corsConfig = Config::inst()->get(static::class, 'cors');
if (empty($corsConfig['Enabled'])) {
// If CORS is disabled don't add the extra headers. Simply return the response untouched.
return $response;
Expand Down
7 changes: 5 additions & 2 deletions src/Scaffolding/Scaffolders/DataObjectScaffolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function setFieldDescription($field, $description)
$this->dataObjectClass
));
}

$this->fields->replace($existing, ArrayData::create([
'Name' => $field,
'Description' => $description
Expand Down Expand Up @@ -495,7 +495,10 @@ protected function allFieldsFromDataObject($includeHasOne = false)
{
$fields = [];
$db = DataObject::config()->fixed_fields;
$db = array_merge($db, Config::inst()->get($this->dataObjectClass, 'db', Config::INHERITED));
$extra = Config::inst()->get($this->dataObjectClass, 'db');
if ($extra) {
$db = array_merge($db, $extra);
}

foreach ($db as $fieldName => $type) {
$fields[] = $fieldName;
Expand Down
51 changes: 21 additions & 30 deletions tests/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SilverStripe\GraphQL\Tests;

use PHPUnit_Framework_MockObject_MockBuilder;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Dev\SapphireTest;
Expand All @@ -11,23 +13,26 @@
use SilverStripe\GraphQL\Tests\Fake\TypeCreatorFake;
use SilverStripe\GraphQL\Tests\Fake\QueryCreatorFake;
use SilverStripe\Core\Config\Config;
use SilverStripe\Security\Member;
use GraphQL\Schema;
use GraphQL\Type\Definition\ObjectType;
use ReflectionClass;
use Exception;
use SilverStripe\Control\HTTPResponse_Exception;

class ControllerTest extends SapphireTest
{
public function setUp()
{
Director::set_environment_type('dev');
parent::setUp();

Handler::config()->remove('authenticators');
$this->logInWithPermission('CMS_ACCESS_CMSMain');
}

public function tearDown()
{
Director::set_environment_type('dev');
parent::tearDown();
}

public function testIndex()
{
$controller = new Controller();
Expand All @@ -41,8 +46,7 @@ public function testIndex()

public function testGetGetManagerPopulatesFromConfig()
{
Config::inst()->remove('SilverStripe\GraphQL', 'schema');
Config::inst()->update('SilverStripe\GraphQL', 'schema', [
Config::modify()->set(Controller::class, 'schema', [
'types' => [
'mytype' => TypeCreatorFake::class,
],
Expand All @@ -60,17 +64,12 @@ public function testGetGetManagerPopulatesFromConfig()

public function testIndexWithException()
{
Config::inst()->update('SilverStripe\\Control\\Director', 'environment_type', 'live');
Director::set_environment_type('live');

$controller = new Controller();
$managerMock = $this->getMockBuilder(Schema::class)
/** @var Manager|PHPUnit_Framework_MockObject_MockBuilder $managerMock */
$managerMock = $this->getMockBuilder(Manager::class)
->setMethods(['query'])
->setConstructorArgs([
['query' => new ObjectType([
'name' => 'Query',
'fields' => []
])]
])
->getMock();

$managerMock->method('query')
Expand All @@ -88,17 +87,12 @@ public function testIndexWithException()

public function testIndexWithExceptionIncludesTraceInDevMode()
{
Config::inst()->update('SilverStripe\\Control\\Director', 'environment_type', 'dev');
Director::set_environment_type('dev');

$controller = new Controller();
$managerMock = $this->getMockBuilder(Schema::class)
/** @var Manager|PHPUnit_Framework_MockObject_MockBuilder $managerMock */
$managerMock = $this->getMockBuilder(Manager::class)
->setMethods(['query'])
->setConstructorArgs([
['query' => new ObjectType([
'name' => 'Query',
'fields' => []
])]
])
->getMock();

$managerMock->method('query')
Expand Down Expand Up @@ -150,12 +144,11 @@ public function testAuthenticationProtectionOnQueries($authenticator, $shouldFai
}

/**
* @expectedException SilverStripe\Control\HTTPResponse_Exception
* @expectedException \SilverStripe\Control\HTTPResponse_Exception
*/
public function testAddCorsHeadersOriginDisallowed()
{
Config::inst()->remove('SilverStripe\GraphQL', 'cors');
Config::inst()->update('SilverStripe\GraphQL', 'cors', [
Config::modify()->set(Controller::class, 'cors', [
'Enabled' => true,
'Allow-Origin' => null,
'Allow-Headers' => 'Authorization, Content-Type',
Expand All @@ -175,8 +168,7 @@ public function testAddCorsHeadersOriginDisallowed()

public function testAddCorsHeadersOriginAllowed()
{
Config::inst()->remove('SilverStripe\GraphQL', 'cors');
Config::inst()->update('SilverStripe\GraphQL', 'cors', [
Config::modify()->set(Controller::class, 'cors', [
'Enabled' => true,
'Allow-Origin' => 'localhost',
'Allow-Headers' => 'Authorization, Content-Type',
Expand All @@ -201,12 +193,11 @@ public function testAddCorsHeadersOriginAllowed()
}

/**
* @expectedException SilverStripe\Control\HTTPResponse_Exception
* @expectedException \SilverStripe\Control\HTTPResponse_Exception
*/
public function testAddCorsHeadersResponseCORSDisabled()
{
Config::inst()->remove('SilverStripe\GraphQL', 'cors');
Config::inst()->update('SilverStripe\GraphQL', 'cors', [
Config::modify()->set(Controller::class, 'cors', [
'Enabled' => false
]);

Expand Down

0 comments on commit 72b9d61

Please sign in to comment.