Skip to content

Commit

Permalink
API New $class arg for DataObjectModel->getModelTypeForField() (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Nov 4, 2021
1 parent 37a5b86 commit 7602dbd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/Schema/DataObject/DataObjectModel.php
Expand Up @@ -323,13 +323,18 @@ public function getAllOperationIdentifiers(): array
* get that field.
*
* @param string $fieldName
* @param string $class Optional class name for model fields which would result in database queries.
* The database is not always available when the schema is built (e.g. on deployment servers).
* @return ModelType|null
* @throws SchemaBuilderException
*/
public function getModelTypeForField(string $fieldName): ?ModelType
public function getModelTypeForField(string $fieldName, $class = null): ?ModelType
{
$result = $this->getFieldAccessor()->accessField($this->dataObject, $fieldName);
$class = $this->getModelClass($result);
if (!$class) {
$result = $this->getFieldAccessor()->accessField($this->dataObject, $fieldName);
$class = $this->getModelClass($result);
}

if (!$class) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions src/Schema/Field/Field.php
Expand Up @@ -171,6 +171,7 @@ public function applyConfig(array $config)
'description',
'resolver',
'resolverContext',
'resolvedModelClass',
'plugins',
]);

Expand Down
14 changes: 12 additions & 2 deletions src/Schema/Field/ModelField.php
Expand Up @@ -23,6 +23,11 @@ class ModelField extends Field
*/
private $modelTypeFields = null;

/**
* @var string|null
*/
private $resolvedModelClass = null;

/**
* @var string
*/
Expand All @@ -37,7 +42,7 @@ class ModelField extends Field
* ModelField constructor.
* @param string $name
* @param $config
* @param SchemaModelInterface $model
* @param SchemaModelInterface $model The model containing this field (different from the model this field might resolve to)
* @throws SchemaBuilderException
*/
public function __construct(string $name, $config, SchemaModelInterface $model)
Expand Down Expand Up @@ -78,6 +83,10 @@ public function applyConfig(array $config)
$this->setResolver($this->getModel()->getDefaultResolver($this->getResolverContext()));
}

if (isset($config['resolvedModelClass'])) {
$this->resolvedModelClass = $config['resolvedModelClass'];
}

$this->modelTypeFields = $config['fields'] ?? null;

unset($config['fields']);
Expand Down Expand Up @@ -105,7 +114,8 @@ public function getModelType(): ?ModelType
if (Schema::isInternalType($type)) {
return null;
}
$model = $this->getModel()->getModelTypeForField($this->getName());

$model = $this->getModel()->getModelTypeForField($this->getName(), $this->resolvedModelClass);
if ($model) {
$config = [];
if ($this->modelTypeFields) {
Expand Down

0 comments on commit 7602dbd

Please sign in to comment.