From 5b70ff3ad62d58000c52acd90a759fe1938b88ce Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 2 Jul 2021 15:19:53 +1200 Subject: [PATCH] API New $class arg for DataObjectModel->getModelTypeForField() --- src/Schema/DataObject/DataObjectModel.php | 11 ++++++++--- src/Schema/Field/Field.php | 1 + src/Schema/Field/ModelField.php | 14 ++++++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Schema/DataObject/DataObjectModel.php b/src/Schema/DataObject/DataObjectModel.php index 10309ba7..f21dab9a 100644 --- a/src/Schema/DataObject/DataObjectModel.php +++ b/src/Schema/DataObject/DataObjectModel.php @@ -311,13 +311,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; } diff --git a/src/Schema/Field/Field.php b/src/Schema/Field/Field.php index 6b31144e..856cd115 100644 --- a/src/Schema/Field/Field.php +++ b/src/Schema/Field/Field.php @@ -171,6 +171,7 @@ public function applyConfig(array $config) 'description', 'resolver', 'resolverContext', + 'resolvedModelClass', 'plugins', ]); diff --git a/src/Schema/Field/ModelField.php b/src/Schema/Field/ModelField.php index deeaf065..3d2428bd 100644 --- a/src/Schema/Field/ModelField.php +++ b/src/Schema/Field/ModelField.php @@ -22,6 +22,11 @@ class ModelField extends Field */ private $modelTypeFields = null; + /** + * @var string|null + */ + private $resolvedModelClass = null; + /** * @var string */ @@ -31,7 +36,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) @@ -71,6 +76,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']); @@ -98,7 +107,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) {