Skip to content

Commit

Permalink
Nullable fixes, duplication protection
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Aug 2, 2021
1 parent 781e981 commit af72974
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 30 deletions.
2 changes: 1 addition & 1 deletion _config/schema-default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ SilverStripe\GraphQL\Schema\Schema:
schemas:
default:
config:
parseShortcodes: true
modelConfig:
DataObject:
parseShortcodes: true
operations:
read:
plugins:
Expand Down
7 changes: 5 additions & 2 deletions src/Schema/DataObject/Plugin/DBFieldArgs/DBDateArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ protected function getResolver(): callable
}

/**
* @param DBDate $obj
* @param mixed $obj
* @param array $args
* @return DBField | string
* @throws Exception
*/
public static function resolve(DBDate $obj, array $args)
public static function resolve($obj, array $args)
{
if (!$obj instanceof DBDate) {
return $obj;
}
$format = $args['format'] ?? null;
$custom = $args['customFormat'] ?? null;

Expand Down
8 changes: 5 additions & 3 deletions src/Schema/DataObject/Plugin/DBFieldArgs/DBFieldArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\GraphQL\Schema\Field\ModelField;
use SilverStripe\GraphQL\Schema\Type\Enum;
Expand All @@ -28,12 +27,15 @@ abstract public function getEnum(): Enum;
abstract public function applyToField(ModelField $field): void;

/**
* @param DBField $obj
* @param mixed $obj
* @param array $args
* @return mixed
*/
public static function baseFormatResolver(DBField $obj, array $args)
public static function baseFormatResolver($obj, array $args)
{
if (!$obj instanceof DBField) {
return $obj;
}
$format = $args['format'] ?? null;
if ($format) {
if ($obj->hasMethod($format)) {
Expand Down
23 changes: 17 additions & 6 deletions src/Schema/DataObject/Plugin/DBFieldArgs/DBHTMLTextArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

namespace SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs;

use SilverStripe\Control\HTTP;
use SilverStripe\GraphQL\QueryHandler\SchemaConfigProvider;
use SilverStripe\GraphQL\Schema\Field\ModelField;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use Exception;
use SilverStripe\ORM\FieldType\DBString;

class DBHTMLTextArgs extends DBTextArgs
{
Expand All @@ -22,19 +23,29 @@ public function applyToField(ModelField $field): void
}

/**
* @param DBString $obj
* @param mixed $obj
* @param array $args
* @param array $context
* @return DBField
* @throws Exception
*/
public static function resolve(DBString $obj, array $args)
public static function resolve($obj, array $args, array $context)
{
$result = parent::resolve($obj, $args, $context);
if (!$result instanceof DBHTMLText) {
return $result;
}

/* @var DBHTMLText $obj */
$parse = $args['parseShortcodes'] ?? null;
if ($parse !== null) {
$obj->setProcessShortcodes($parse);
if ($parse === null) {
$config = SchemaConfigProvider::get($context);
if ($config) {
$parse = $config->getModelConfiguration('DataObject')->get('parseShortcodes', true);
}
}
$obj->setProcessShortcodes($parse);

return parent::resolve($obj, $args);
return $obj->RAW();
}
}
10 changes: 7 additions & 3 deletions src/Schema/DataObject/Plugin/DBFieldArgs/DBTextArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

use SilverStripe\GraphQL\Schema\Field\ModelField;
use SilverStripe\GraphQL\Schema\Type\Enum;
use SilverStripe\ORM\FieldType\DBString;
use Exception;
use SilverStripe\ORM\FieldType\DBText;

class DBTextArgs extends DBFieldArgs
{
Expand Down Expand Up @@ -54,11 +54,15 @@ protected function getResolver(): callable
}

/**
* @param DBString $obj
* @param mixed $obj
* @param array $args
* @param array $context
*/
public static function resolve(DBString $obj, array $args)
public static function resolve($obj, array $args, array $context)
{
if (!$obj instanceof DBText) {
return $obj;
}
$format = $args['format'] ?? null;
$limit = $args['limit'] ?? null;

Expand Down
8 changes: 5 additions & 3 deletions src/Schema/DataObject/Plugin/DBFieldArgs/DBTimeArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use SilverStripe\GraphQL\Schema\Field\ModelField;
use SilverStripe\GraphQL\Schema\Type\Enum;
use SilverStripe\ORM\FieldType\DBDate;
use SilverStripe\ORM\FieldType\DBField;
use Exception;
use SilverStripe\ORM\FieldType\DBTime;
Expand Down Expand Up @@ -42,13 +41,16 @@ protected function getResolver(): callable
}

/**
* @param DBTime $obj
* @param mixed $obj
* @param array $args
* @return DBField | string
* @throws Exception
*/
public static function resolve(DBTime $obj, array $args)
public static function resolve($obj, array $args)
{
if (!$obj instanceof DBTime) {
return $obj;
}
$format = $args['format'] ?? null;
$custom = $args['customFormat'] ?? null;

Expand Down
5 changes: 2 additions & 3 deletions src/Schema/DataObject/Plugin/ScalarDBField.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public function apply(ModelType $type, Schema $schema, array $config = []): void
{
foreach ($type->getFields() as $field) {
if ($field instanceof ModelField && $field->getModel() instanceof DataObjectModel) {
$dataClass = $field->getMetadata()->get('dataClass');
if (is_subclass_of($dataClass, DBField::class)) {
if (!$field->isList()) {
$field->addResolverAfterware([static::class, 'resolve']);
}
}
Expand All @@ -47,7 +46,7 @@ public function apply(ModelType $type, Schema $schema, array $config = []): void
public static function resolve($obj)
{
if ($obj instanceof DBField) {
return $obj->forTemplate();
return $obj->getValue();
}

return $obj;
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/DataObject/ReadCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function createOperation(
}

$query = ModelQuery::create($model, $queryName)
->setType("[$typeName]")
->setType("[$typeName!]!")
->setPlugins($plugins)
->setResolver([static::class, 'resolve'])
->setResolverContext([
Expand Down
36 changes: 28 additions & 8 deletions src/Schema/Plugin/PaginationPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ public static function updateSchema(Schema $schema): void
*/
public function apply(Field $field, Schema $schema, array $config = []): void
{
// Set the new return type
$plainType = $field->getNamedType();
$fullType = $field->getType();
$ref = TypeReference::create($fullType);

$defaultLimit = $config['defaultLimit'] ?? $this->config()->get('default_limit');
$connectionName = $config['connection'] ?? $field->getName();
$connectionName = $config['connection'] ?? $plainType;
$max = $this->config()->get('max_limit');
$limit = min($defaultLimit, $max);
$field->addArg('limit', "Int = $limit")
Expand All @@ -103,14 +108,26 @@ public function apply(Field $field, Schema $schema, array $config = []): void
['maxLimit' => $max]
);

// Set the new return type
$plainType = $field->getNamedType();
$fullType = $field->getType();
$ref = TypeReference::create($fullType);
$existing = $schema->getState()->get(['connections', $fullType]);
if ($existing) {
$field->setType($existing, $ref->isRequired());
return;
}

$connectionName = ucfirst($connectionName) . 'Connection';
$connectionTypeStr = $ref->isRequired() ? $connectionName . '!' : $connectionName;
$field->setType($connectionTypeStr);

// Dedupe. If the connection exists for the same type
// (possibly with different wrapper type, e.g. not required)
if ($existing = $schema->getType($connectionName)) {
$i = 1;
$rootConnectionName = $connectionName;
while ($schema->getType($connectionName)) {
$connectionName = $rootConnectionName . $i;
$i++;
}
}

$field->setType($connectionName, $ref->isRequired());

// Create the edge type for this query
$edgeType = Type::create($connectionName . 'Edge')
Expand All @@ -123,11 +140,14 @@ public function apply(Field $field, Schema $schema, array $config = []): void

// Create the connection type for this query
$connectionType = Type::create($connectionName)
->addField('edges', "[{$edgeType->getName()}]!")
->addField('edges', "[{$edgeType->getName()}!]!")
->addField('nodes', $fullType)
->addField('pageInfo', 'PageInfo!');

$schema->addType($connectionType);

// Cache the connection for this type so it can be reused
$schema->getState()->set(['connections', $fullType], $connectionType->getName());
}

/**
Expand Down

0 comments on commit af72974

Please sign in to comment.