Skip to content

Commit

Permalink
Simplified RelationFieldValue resolver
Browse files Browse the repository at this point in the history
Always run a multiple search, and return the results depending on the multiple flag.
  • Loading branch information
Bertrand Dunogier committed Apr 2, 2019
1 parent b53999a commit 41a1e0f
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/GraphQL/Resolver/DomainContentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,18 @@ public function resolveDomainFieldValue(Content $content, $fieldDefinitionIdenti
public function resolveDomainRelationFieldValue(Field $field, $multiple = false)
{
if ($field->value instanceof FieldType\RelationList\Value) {
if ($multiple) {
if (count($field->value->destinationContentIds) > 0) {
return $this->contentLoader->find(new Query(
['filter' => new Query\Criterion\ContentId($field->value->destinationContentIds)]
));
} else {
return [];
}
} else {
return
isset($field->value->destinationContentIds[0])
? $this->contentLoader->findSingle(new Query\Criterion\ContentId($field->value->destinationContentIds[0]))
: null;
}
} else if($field->value instanceof FieldType\Relation\Value) {
return
isset($field->value->destinationContentId)
? $this->contentLoader->findSingle(new Query\Criterion\ContentId($field->value->destinationContentId))
: null;
$destinationContentIds = $field->value->destinationContentIds;
} else if ($field->value instanceof FieldType\Relation\Value) {
$destinationContentIds = [$field->value->destinationContentId];
} else {
throw new UserError("$field->fieldTypeIdentifier is not a RelationList field value");
throw new UserError('\$field does not contain a RelationList or Relation Field value');
}

$contentItems = $this->contentLoader->find(new Query(
['filter' => new Query\Criterion\ContentId($destinationContentIds)]
));

return $multiple ? $contentItems : $contentItems[0] ?? null;
}

public function resolveDomainContentType(Content $content)
Expand Down

0 comments on commit 41a1e0f

Please sign in to comment.