-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW: DBField arguments, Enum, Composite support (#398)
* Initial commit * Refactor of args work * Ready for tests * Scalar dbfield * Begin integration test * Tests done * Allow ignore enum, prevent duplication * Nullable fixes, duplication protection * New tests for dedupe, custom name, etc * Fix race condition in interface builder adding to queries before interfaces are ready * Update src/Schema/Plugin/PaginationPlugin.php Co-authored-by: Steve Boyd <emteknetnz@gmail.com> * BUGFIX: Don't apply metadata when explicit type is specified * Revisions per emteknetnz * Fix tests Co-authored-by: Steve Boyd <emteknetnz@gmail.com>
- Loading branch information
1 parent
285de89
commit d2a0318
Showing
37 changed files
with
1,543 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
Name: graphql-db-args | ||
--- | ||
# For the DBFieldArgsPlugin, assign each DBField type an args factory | ||
SilverStripe\ORM\FieldType\DBText: | ||
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBTextArgs | ||
SilverStripe\ORM\FieldType\DBHTMLText: | ||
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBHTMLTextArgs | ||
SilverStripe\ORM\FieldType\DBDate: | ||
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBDateArgs | ||
SilverStripe\ORM\FieldType\DBDateTime: | ||
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBDatetimeArgs | ||
SilverStripe\ORM\FieldType\DBTime: | ||
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBTimeArgs | ||
SilverStripe\ORM\FieldType\DBDecimal: | ||
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBDecimalArgs | ||
SilverStripe\ORM\FieldType\DBFloat: | ||
graphql_args: SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs\DBFloatArgs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
|
||
namespace SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs; | ||
|
||
use SilverStripe\GraphQL\Schema\Field\ModelField; | ||
use SilverStripe\GraphQL\Schema\Type\Enum; | ||
use SilverStripe\ORM\FieldType\DBDate; | ||
use Exception; | ||
use SilverStripe\ORM\FieldType\DBField; | ||
|
||
class DBDateArgs extends DBFieldArgs | ||
{ | ||
public function getEnum(): Enum | ||
{ | ||
return Enum::create( | ||
'DBDateFormattingOptions', | ||
$this->getValues(), | ||
'Formatting options for fields that map to DBDate data types' | ||
); | ||
} | ||
|
||
public function applyToField(ModelField $field): void | ||
{ | ||
$field | ||
->addArg('format', [ | ||
'type' => $this->getEnum()->getName(), | ||
'description' => 'Formatting options for this field', | ||
]) | ||
->addArg('customFormat', [ | ||
'type' => 'String', | ||
'description' => 'If format is CUSTOM, the format string, e.g. "y-MM-dd HH:mm:ss"', | ||
]) | ||
->addResolverAfterware($this->getResolver()); | ||
} | ||
|
||
/** | ||
* @return callable | ||
*/ | ||
protected function getResolver(): callable | ||
{ | ||
return [static::class, 'resolve']; | ||
} | ||
|
||
/** | ||
* @param mixed $obj | ||
* @param array $args | ||
* @return DBField | string | ||
* @throws Exception | ||
*/ | ||
public static function resolve($obj, array $args) | ||
{ | ||
if (!$obj instanceof DBDate) { | ||
return $obj; | ||
} | ||
$format = $args['format'] ?? null; | ||
$custom = $args['customFormat'] ?? null; | ||
|
||
if ($format === 'Format') { | ||
if (!$custom) { | ||
throw new Exception('The "custom" option requires a value for "customFormat"'); | ||
} | ||
return $obj->Format($custom); | ||
} | ||
if ($custom) { | ||
throw new Exception('The "customFormat" argument should not be set for formats that are not "custom"'); | ||
} | ||
|
||
if ($obj->hasMethod($format)) { | ||
return $obj->obj($format); | ||
} | ||
|
||
return $obj; | ||
} | ||
|
||
public function getValues(): array | ||
{ | ||
return [ | ||
'TIMESTAMP' => 'Timestamp', | ||
'NICE' => 'Nice', | ||
'DAY_OF_WEEK' => 'DayOfWeek', | ||
'MONTH' => 'Month', | ||
'YEAR' => 'Year', | ||
'SHORT_MONTH' => 'ShortMonth', | ||
'DAY_OF_MONTH' => 'DayOfMonth', | ||
'SHORT' => 'Short', | ||
'LONG' => 'Long', | ||
'FULL' => 'Full', | ||
'CUSTOM' => 'Format', | ||
]; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Schema/DataObject/Plugin/DBFieldArgs/DBDatetimeArgs.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
|
||
namespace SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs; | ||
|
||
use SilverStripe\GraphQL\Schema\Type\Enum; | ||
|
||
class DBDatetimeArgs extends DBDateArgs | ||
{ | ||
public function getEnum(): Enum | ||
{ | ||
return Enum::create( | ||
'DBDatetimeFormattingOption', | ||
$this->getValues(), | ||
'Formatting options for fields that map to DBDatetime data types' | ||
); | ||
} | ||
|
||
public function getValues(): array | ||
{ | ||
return array_merge( | ||
parent::getValues(), | ||
[ | ||
'DATE' => 'Date', | ||
'TIME' => 'Time', | ||
'TIME12' => 'Time12', | ||
'TIME24' => 'Time24', | ||
] | ||
); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Schema/DataObject/Plugin/DBFieldArgs/DBDecimalArgs.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
|
||
namespace SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldArgs; | ||
|
||
use SilverStripe\GraphQL\Schema\Field\ModelField; | ||
use SilverStripe\GraphQL\Schema\Type\Enum; | ||
|
||
class DBDecimalArgs extends DBFieldArgs | ||
{ | ||
public function getEnum(): Enum | ||
{ | ||
return Enum::create( | ||
'DBDecimalFormattingOptions', | ||
$this->getValues(), | ||
'Formatting options for fields that map to DBDecimal data types' | ||
); | ||
} | ||
|
||
public function applyToField(ModelField $field): void | ||
{ | ||
$field->addArg('format', [ | ||
'type' => $this->getEnum()->getName(), | ||
'description' => 'Formatting options for this field', | ||
])->addResolverAfterware( | ||
$this->getResolver() | ||
); | ||
} | ||
|
||
/** | ||
* @return callable | ||
*/ | ||
protected function getResolver(): callable | ||
{ | ||
return [DBFieldArgs::class, 'baseFormatResolver']; | ||
} | ||
|
||
|
||
public function getValues(): array | ||
{ | ||
return [ | ||
'INT' => 'Int', | ||
]; | ||
} | ||
} |
Oops, something went wrong.