From f71619822e8e9cdf2c31101c76bcc833c81cd00f Mon Sep 17 00:00:00 2001 From: Mark Huot Date: Thu, 18 Jan 2018 06:05:15 -0500 Subject: [PATCH] fixing bug where multiple block types were ignoring empty check --- src/Builders/Union.php | 4 +++- src/Listeners/GetMatrixFieldSchema.php | 21 ++++++++------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Builders/Union.php b/src/Builders/Union.php index 9187e04e..521bcbf0 100644 --- a/src/Builders/Union.php +++ b/src/Builders/Union.php @@ -22,7 +22,9 @@ function getResolveType() { } function addType($typeName, $context=null) { - return $this->types[$typeName] = new Schema($this->request, $context); + $this->types[$typeName] = new Schema($this->request, $context); + $this->types[$typeName]->name($typeName); + return $this->types[$typeName]; } function getTypes(): array { diff --git a/src/Listeners/GetMatrixFieldSchema.php b/src/Listeners/GetMatrixFieldSchema.php index f44e0cfa..21dfce82 100644 --- a/src/Listeners/GetMatrixFieldSchema.php +++ b/src/Listeners/GetMatrixFieldSchema.php @@ -18,7 +18,6 @@ function handle($event) { $field = $event->sender; $schema = $event->schema; - $request = $schema->getRequest(); $union = $schema->addUnionField($field) ->lists() @@ -27,13 +26,19 @@ function handle($event) { return ucfirst($field->handle).ucfirst($block->handle); }); - $fieldService = \Yii::$container->get('craftQLFieldService'); - $blockTypes = $field->getBlockTypes(); foreach ($blockTypes as $blockType) { $type = $union->addType(ucfirst($field->handle).ucfirst($blockType->handle), $blockType); $type->addFieldsByLayoutId($blockType->fieldLayoutId); + + if (empty($type->getFields())) { + $warning = 'The block type, `'.$blockType->handle.'` on `'.$field->handle.'`, has no fields. This would violate the GraphQL spec so we filled it in with this placeholder.'; + + $type->addStringField('empty') + ->description($warning) + ->resolve($warning); + } } if (empty($blockTypes)) { @@ -45,16 +50,6 @@ function handle($event) { ->resolve($warning); } - foreach ($union->getTypes() as $typeName => $typeSchema) { - if (empty($typeSchema->getFields())) { - $warning = 'The block type, `'.$typeName.'`, has no fields. This would violate the GraphQL spec so we filled it in with this placeholder.'; - - $type->addStringField('empty') - ->description($warning) - ->resolve($warning); - } - } - if (!empty($blockTypes)) { $inputType = $event->mutation->createInputObjectType(ucfirst($event->sender->handle) . 'Input');