Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Allow Enum fields in input types #413

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ env:
global:
- COMPOSER_ROOT_VERSION="4.x-dev"
- REQUIRE_RECIPE="4.x-dev"
- REQUIRE_EXTRA="silverstripe/recipe-cms:4.x-dev silverstripe/frameworktest:0.3.0"
- REQUIRE_EXTRA="silverstripe/recipe-cms:4.x-dev silverstripe/frameworktest:^0.4"
- BEHAT_SUITE="asset-admin --config vendor/silverstripe/asset-admin/behat.yml"

matrix:
Expand Down
13 changes: 12 additions & 1 deletion src/Schema/DataObject/CreateCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SilverStripe\GraphQL\QueryHandler\SchemaConfigProvider;
use SilverStripe\GraphQL\QueryHandler\UserContextProvider;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Field\ModelField;
use SilverStripe\GraphQL\Schema\Field\ModelMutation;
use SilverStripe\GraphQL\Schema\Interfaces\ModelOperation;
use SilverStripe\GraphQL\Schema\Schema;
Expand All @@ -22,6 +23,7 @@
use Closure;
use SilverStripe\GraphQL\Schema\Type\ModelType;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBEnum;

/**
* Creates a "create" mutation for a DataObject
Expand Down Expand Up @@ -135,7 +137,16 @@ public function provideInputTypes(ModelType $modelType, array $config = []): arr
continue;
}
$type = $fieldObj->getNamedType();
if ($type && Schema::isInternalType($type)) {
if (!$type) {
continue;
}
$isScalar = Schema::isInternalType($type);
if (!$isScalar && $fieldObj instanceof ModelField) {
$dataClass = $fieldObj->getMetadata()->get('dataClass');
$isScalar = $dataClass === DBEnum::class || is_subclass_of($dataClass, DBEnum::class);
}

if ($isScalar) {
$fieldMap[$fieldName] = $type;
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/Schema/DataObject/UpdateCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SilverStripe\GraphQL\QueryHandler\SchemaConfigProvider;
use SilverStripe\GraphQL\QueryHandler\UserContextProvider;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Field\ModelField;
use SilverStripe\GraphQL\Schema\Field\ModelMutation;
use SilverStripe\GraphQL\Schema\Interfaces\ModelOperation;
use SilverStripe\GraphQL\Schema\Schema;
Expand All @@ -23,6 +24,7 @@
use SilverStripe\GraphQL\Schema\Type\ModelType;
use SilverStripe\ORM\DataList;
use Closure;
use SilverStripe\ORM\FieldType\DBEnum;

/**
* Creates an update operation for a DataObject
Expand Down Expand Up @@ -153,8 +155,16 @@ public function provideInputTypes(ModelType $modelType, array $config = []): arr
continue;
}
$type = $fieldObj->getNamedType();
if (!$type) {
continue;
}
$isScalar = Schema::isInternalType($type);
if (!$isScalar && $fieldObj instanceof ModelField) {
$dataClass = $fieldObj->getMetadata()->get('dataClass');
$isScalar = $dataClass === DBEnum::class || is_subclass_of($dataClass, DBEnum::class);
}
// No nested input types... yet
if ($type && Schema::isInternalType($type)) {
if ($isScalar) {
$fieldMap[$fieldName] = $type;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Schema/Plugin/AbstractQueryFilterPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public static function updateSchema(Schema $schema): void
if (empty($filters)) {
return;
}
foreach (Schema::getInternalTypes() as $typeName) {
$scalarTypes = Schema::getInternalTypes();
foreach ($schema->getEnums() as $enum) {
$scalarTypes[] = $enum->getName();
}
foreach ($scalarTypes as $typeName) {
$type = InputType::create(static::getLeafNodeType($typeName));
foreach ($filters as $id => $filterInstance) {
if ($filterInstance instanceof ListFieldFilterInterface) {
Expand Down
20 changes: 13 additions & 7 deletions src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -1159,26 +1159,32 @@ public static function assertValidConfig(array $config, $allowedKeys = [], $requ
{
static::invariant(
empty($config) || ArrayLib::is_associative($config),
'%s configurations must be key value pairs of names to configurations.
Did you include an indexed array in your config?',
static::class
"%s configurations must be key value pairs of names to configurations.
Did you include an indexed array in your config?

Context: %s",
static::class,
json_encode($config)
);

if (!empty($allowedKeys)) {
$invalidKeys = array_diff(array_keys($config), $allowedKeys);
static::invariant(
empty($invalidKeys),
'Config contains invalid keys: %s',
implode(',', $invalidKeys)
"Config contains invalid keys: %s. Allowed keys are %s.\n\nContext: %s",
implode(',', $invalidKeys),
implode(',', $allowedKeys),
json_encode($config)
);
}

if (!empty($requiredKeys)) {
$missingKeys = array_diff($requiredKeys, array_keys($config));
static::invariant(
empty($missingKeys),
'Config is missing required keys: %s',
implode(',', $missingKeys)
"Config is missing required keys: %s.\n\nContext: %s",
implode(',', $missingKeys),
json_encode($config)
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Schema/Services/NestedInputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ protected function addInputTypesToSchema(
$fieldType = $fieldObj->getNamedType();
$nestedType = $this->schema->getCanonicalType($fieldType);

$isScalar = (bool) Schema::isInternalType($fieldType) || $this->schema->getEnum($fieldType);

if ($data === self::SELF_REFERENTIAL) {
$inputType->addField($fieldName, $inputType->getName());
} elseif (!is_array($data) && !$nestedType && Schema::isInternalType($fieldType)) {
} elseif (!is_array($data) && !$nestedType && $isScalar) {
// Regular field, e.g. scalar
$inputType->addField(
$fieldName,
Expand Down
3 changes: 2 additions & 1 deletion src/Schema/Type/ModelType.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function applyConfig(array $config)
if (isset($fields[Schema::ALL])) {
$all = $fields[Schema::ALL];
unset($fields[Schema::ALL]);
$fields = array_merge([
$fields = array_merge(
[
Schema::ALL => $all,
],
$fields
Expand Down
1 change: 1 addition & 0 deletions tests/Fake/FakeProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FakeProduct extends DataObject implements TestOnly
private static $db = [
'Title' => 'Varchar',
'Price' => 'Int',
'Status' => "Enum('AVAILABLE, UNAVAILABLE')",
];

private static $has_one = [
Expand Down
8 changes: 6 additions & 2 deletions tests/Schema/Services/NestedInputBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Dev\BuildState;
use SilverStripe\GraphQL\Schema\DataObject\Plugin\DBFieldTypes;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Field\Query;
use SilverStripe\GraphQL\Schema\Schema;
Expand Down Expand Up @@ -39,10 +40,12 @@ public function testNestedInputBuilder()
$model->addField('products');
$model->addAllOperations();
})
->addModelbyClassName(FakeProduct::class, function (ModelType $model) {
->addModelbyClassName(FakeProduct::class, function (ModelType $model) use ($schema) {
$model->addField('title');
$model->addField('reviews');
$model->addField('status');
$model->addField('relatedProducts');
(new DBFieldTypes())->apply($model, $schema);
})
->addModelbyClassName(FakeReview::class, function (ModelType $model) {
$model->addField('content');
Expand All @@ -67,6 +70,7 @@ public function testNestedInputBuilder()
'title' => 'String',
'reviews' => 'FakeReviewInputType',
'relatedProducts' => 'FakeProductInputType',
'status' => 'StatusEnum',
],
'FakeReviewInputType' => [
'id' => 'ID',
Expand Down Expand Up @@ -135,7 +139,7 @@ public function testNestedInputBuilderBuildsRepeatedRelationsFilter()
$this->assertNotNull($featuredFilter, "Field featuredProducts not found on {$filterType->getName()}");
$this->assertEquals('FakeProductFilterFields', $featuredFilter->getType());
}

private function assertSchema(array $graph, Schema $schema)
{
foreach ($graph as $typeName => $fields) {
Expand Down