From 70e702a74be13a3e2a551ecd9beb04acb04b2475 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 2 Jul 2021 16:25:03 +1200 Subject: [PATCH] FIX Avoid inferring model type Since Versioned->versions() results in an ArrayList, it triggers database queries. The database isn't always available when the schema is built (e.g. on deployment and CI environments). Context: https://github.com/silverstripe/silverstripe-graphql/issues/388 --- src/GraphQL/Plugins/VersionedDataObject.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/GraphQL/Plugins/VersionedDataObject.php b/src/GraphQL/Plugins/VersionedDataObject.php index ec345521..ae7d7d2c 100644 --- a/src/GraphQL/Plugins/VersionedDataObject.php +++ b/src/GraphQL/Plugins/VersionedDataObject.php @@ -23,6 +23,7 @@ use SilverStripe\Versioned\GraphQL\Resolvers\VersionedResolver; use SilverStripe\Versioned\Versioned; use Closure; +use SilverStripe\GraphQL\Schema\Field\ModelField; // GraphQL dependency is optional in versioned, // and the following implementation relies on existence of this class (in GraphQL v4) @@ -114,7 +115,12 @@ public function apply(ModelType $type, Schema $schema, array $config = []): void } $schema->addType($versionType); - $type->addField('versions', '[' . $versionName . ']', function (Field $field) use ($type, $schema, $config) { + $versionsFieldConfig = new ModelField('versions', [ + 'type' => '[' . $versionName . ']', + // TODO This isn't the actual resolvedModelClass, it just avoids triggering database queries through inference + 'resolvedModelClass' => $type->getModel()->getSourceClass() + ], $type->getModel()); + $type->addField('versions', $versionsFieldConfig, function (Field $field) use ($type, $schema, $config) { $field->setResolver([VersionedResolver::class, 'resolveVersionList']) ->addResolverContext('sourceClass', $type->getModel()->getSourceClass()); SortPlugin::singleton()->apply($field, $schema, [