diff --git a/src/Schema/DataObject/DataObjectModel.php b/src/Schema/DataObject/DataObjectModel.php index 75b0cb22..b98fb3fe 100644 --- a/src/Schema/DataObject/DataObjectModel.php +++ b/src/Schema/DataObject/DataObjectModel.php @@ -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; } diff --git a/src/Schema/Field/Field.php b/src/Schema/Field/Field.php index 0c396354..30dcc6b0 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 52884444..9ff7ebc7 100644 --- a/src/Schema/Field/ModelField.php +++ b/src/Schema/Field/ModelField.php @@ -23,6 +23,11 @@ class ModelField extends Field */ private $modelTypeFields = null; + /** + * @var string|null + */ + private $resolvedModelClass = null; + /** * @var string */ @@ -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) @@ -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']); @@ -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) {