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) {