Skip to content

Commit

Permalink
FIX Avoid inferring model type
Browse files Browse the repository at this point in the history
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: silverstripe/silverstripe-graphql#388
  • Loading branch information
chillu committed Jul 2, 2021
1 parent 32b7282 commit 782cea9
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/GraphQL/Plugins/VersionedDataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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)
Expand Down Expand Up @@ -110,7 +111,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, [
Expand Down

0 comments on commit 782cea9

Please sign in to comment.