Skip to content

Commit

Permalink
Merge pull request #2237 from dunglas/fix-null-classname
Browse files Browse the repository at this point in the history
GraphQL: fix a bug when the class name isn't provided
  • Loading branch information
soyuka authored Oct 5, 2018
2 parents eed4ff7 + bd3261a commit 1484962
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/GraphQl/Type/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ private function convertType(Type $type, bool $input = false, string $mutationNa
}

$resourceClass = $this->isCollection($type) ? $type->getCollectionValueType()->getClassName() : $type->getClassName();
if (null === $resourceClass) {
return null;
}

try {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
if ([] === $resourceMetadata->getGraphql() ?? []) {
Expand Down
31 changes: 31 additions & 0 deletions tests/GraphQl/Type/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,37 @@ public function testGetSchema(bool $paginationEnabled)
);
}

/**
* Tests that the GraphQL SchemaBuilder supports an edge case where a property is typed as an Type::BUILTIN_TYPE_OBJECT but has no class related.
*/
public function testObjectTypeWithoutClass()
{
$propertyMetadataMockBuilder = function ($builtinType, $resourceClassName) {
return new PropertyMetadata(
new Type(
$builtinType
),
"{$builtinType}Description",
true,
true,
null,
null,
null
);
};

$mockedSchemaBuilder = $this->createSchemaBuilder($propertyMetadataMockBuilder, false);
$this->assertEquals([
'node',
'shortName1',
'shortName1s',
'shortName2',
'shortName2s',
'shortName3',
'shortName3s',
], array_keys($mockedSchemaBuilder->getSchema()->getConfig()->getQuery()->getFields()));
}

public function paginationProvider(): array
{
return [
Expand Down

0 comments on commit 1484962

Please sign in to comment.