[GraphQl] Support for unions and interfaces for search functionality
I am trying to create a general search functionality in my app that can return multiple types of objects. For each type of object I want to retrieve different fields.
In the graphQl specification this is supported using interfaces and unions. See this example below:
type Query {
# Search across all content
search(query: String!): [SearchResult]
}
union SearchResult = Conference | Festival | Concert | Venue
query {
search(query: "Madison") {
... on Venue {
id
name
address
}
... on Festival {
id
name
performers
}
... on Concert {
id
name
performingBand
}
... on Conference {
speakers
workshops
}
}
}
See this guide for more details.
I tried making all my entities extend a 'base' entity and creating the search query on that entity, but that did not work.
Thanks in advance!
Is this something that is planned or any advice how to deal with it meanwhile? This rules out a big part of GraphQL capabilities.
would love to see this implemented. in a meantime I got fragments working for collection for my setup like this.
I have different types, not OneComment and AnotherComment. this is just for demonstration
// api/src/Entity/Comment.php
//...
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'type', type: 'string')]
#[ORM\DiscriminatorMap([
'one' => OneComment::class,
'another' => AnotherComment::class,
])]
class Comment
{
//...
// api/src/Entity/OneComment.php
//...
class OneComment extends Comment
{
//...
// api/src/Entity/AnotherComment.php
//...
class AnotherComment extends Comment
{
//...
# api/config/services.yaml:
# ...
App\Type\TypeConverter:
decorates: api_platform.graphql.type_converter
App\Type\UnionCommentType:
tags:
- { name: api_platform.graphql.type }
ApiPlatform\GraphQl\Type\TypeBuilderEnumInterface: '@api_platform.graphql.type_builder'
// api/src/Type/TypeConverter.php
<?php
namespace App\Type;
use ApiPlatform\GraphQl\Type\TypeConverterInterface;
use ApiPlatform\Metadata\GraphQl\Operation;
use App\Entity\Comment;
use GraphQL\Type\Definition\Type as GraphQLType;
use Symfony\Component\PropertyInfo\Type;
final class TypeConverter implements TypeConverterInterface
{
public function __construct(
private TypeConverterInterface $defaultTypeConverter,
private UnionCommentType $unionCommentType,
) {
}
/**
* {@inheritdoc}
*/
public function convertType(
Type $type,
bool $input,
Operation $rootOperation,
string $resourceClass,
string $rootResource,
?string $property,
int $depth
): GraphQLType|string|null {
if ($type->isCollection() && $type->getCollectionValueTypes()[0]->getClassName() === Comment::class) {
return $this->unionCommentType;
}
return $this->defaultTypeConverter->convertType(
type: $type,
input: $input,
rootOperation: $rootOperation,
resourceClass: $resourceClass,
rootResource: $rootResource,
property: $property,
depth: $depth,
);
}
/**
* {@inheritdoc}
*/
public function resolveType(string $type): ?GraphQLType
{
return $this->defaultTypeConverter->resolveType($type);
}
}
// api/src/Type/UnionCommentType.php
<?php
namespace App\Type;
use ApiPlatform\GraphQl\Type\Definition\TypeInterface;
use ApiPlatform\GraphQl\Type\TypeBuilderEnumInterface;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use App\Entity\OneComment;
use App\Entity\AnotherComment;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\UnionType;
final class UnionCommentType extends UnionType implements TypeInterface
{
/**
* @var array<string, ObjectType>
*/
private array $commentTypes;
public function __construct(
private readonly TypeBuilderEnumInterface $typeBuilder,
private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
) {
$this->name = 'UnionComment';
$this->description = 'Union type for all Comment types';
$this->astNode = null;
$this->extensionASTNodes = [];
$this->commentTypes = [
OneComment::class => $this->getCommentType(OneComment::class),
AnotherComment::class => $this->getCommentType(AnotherComment::class),
];
$this->config = [
'name' => 'UnionComment',
'description' => 'Union type for all Comment types',
'types' => $this->commentTypes,
'resolveType' => fn ($objectValue) => $this->commentTypes[$objectValue['#itemResourceClass']],
'astNode' => null,
'extensionASTNodes' => [],
];
}
public function getName(): string
{
return 'UnionComment';
}
private function getCommentType(string $resourceClass): ObjectType
{
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operationName = 'collection_query';
$operation = $resourceMetadataCollection->getOperation($operationName);
assert($operation instanceof QueryCollection);
$type = $this->typeBuilder->getResourceObjectType(
resourceClass: $resourceClass,
resourceMetadataCollection: $resourceMetadataCollection,
operation: $operation,
input: false,
wrapped: false,
depth: 0
);
assert($type instanceof ObjectType);
return $type;
}
}
query {
comments() {
collection {
id
... on OneComment {
id
body
}
... on AnotherComment {
id
body
}
}
}
}
@shanginn I would like to test your aproach but it looks like there have been some small changes in ApiPlatform.
I rewrote the UnionCommentType with FieldsBuilder instead of TypeBuilderEnumInterface because i couldn't import TypeBuilderEnumInterface.
I added ApiPlatform\GraphQl\Type\FieldsBuilder to services.yml like you did it with ApiPlatform\GraphQl\Type\TypeBuild but it seems that the FieldsBuilder depends on many more services that i would have to instantiate via services.yml?
Shouldn't it be possible to use a predefined FieldsBuilder without instantiating all these services on myself?
I use Api-Platform 4.0.10. Could you give an update how you would implement this with Api-Plaform 4?
hi there. I think my comment with services.yaml was cut, so I updated it with correct declaration of TypeBuilderEnumInterface in there, maybe that would help?
not working with api platform at the moment, sorry
I got it working! ✌️
Disabled the union Type for input-Types. But this should just be fine for me:
namespace App\Type;
use ApiPlatform\GraphQl\Type\TypeConverterInterface;
use ApiPlatform\Metadata\GraphQl\Operation;
use App\Entity\Customer\Customer;
use GraphQL\Type\Definition\Type as GraphQLType;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\PropertyInfo\Type;
#[AsDecorator('api_platform.graphql.type_converter')]
final class TypeConverter implements TypeConverterInterface
{
public function __construct(
private TypeConverterInterface $defaultTypeConverter,
private UnionCustomerType $unionCustomerType,
) {
}
/**
* {@inheritdoc}
*/
public function convertType(
Type $type,
bool $input,
Operation $rootOperation,
string $resourceClass,
string $rootResource,
?string $property,
int $depth
): GraphQLType|string|null {
if (
!$input
&& $type->getClassName() === Customer::class
) {
return $this->unionCustomerType;
}
if (
!$input
&& $type->isCollection()
&& $type->getCollectionValueTypes()
&& $type->getCollectionValueTypes()[0]->getClassName() === Customer::class
) {
return $this->unionCustomerType;
}
return $this->defaultTypeConverter->convertType(
type: $type,
input: $input,
rootOperation: $rootOperation,
resourceClass: $resourceClass,
rootResource: $rootResource,
property: $property,
depth: $depth,
);
}
/**
* {@inheritdoc}
*/
public function resolveType(string $type): ?GraphQLType
{
return $this->defaultTypeConverter->resolveType($type);
}
}
I also tweaked the UnionType a little bit:
namespace App\Type;
use ApiPlatform\GraphQl\Type\ContextAwareTypeBuilderInterface;
use ApiPlatform\GraphQl\Type\Definition\TypeInterface;
use ApiPlatform\Metadata\GraphQl\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use App\Entity\Customer\Organization;
use App\Entity\Customer\Person;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\UnionType;
final class UnionCustomerType extends UnionType implements TypeInterface
{
/**
* @var array<string, ObjectType>
*/
private array $personTypes;
public function __construct(
private ContextAwareTypeBuilderInterface $typeBuilder,
private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
) {
parent::__construct([
'name' => 'UnionCustomer',
'description' => 'Union type for all Customer types',
'types' => [
Person::class => $this->getCustomerType(Person::class),
Organization::class => $this->getCustomerType(Organization::class),
],
'resolveType' => fn ($objectValue) => $this->personTypes[$objectValue['#itemResourceClass']],
'astNode' => null,
'extensionASTNodes' => [],
]);
$this->personTypes = $this->config['types'];
}
public function getName(): string
{
return 'UnionCustomer';
}
private function getCustomerType(string $resourceClass): ObjectType
{
$itemType = $this->getCustomerOperationType($resourceClass, 'item_query');
$collectionType = $this->getCustomerOperationType($resourceClass, 'collection_query');
if ($collectionType !== $itemType) {
throw new \RuntimeException('Customer types differ. Different type classes must be implemented.');
}
return $itemType;
}
private function getCustomerOperationType(string $resourceClass, string $operationName): ObjectType
{
$resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
$operation = $resourceMetadataCollection->getOperation($operationName);
assert($operation instanceof Operation);
$type = $this->typeBuilder->getResourceObjectType(
resourceMetadataCollection: $resourceMetadataCollection,
operation: $operation,
context: ['input' => false]
);
assert($type instanceof ObjectType);
return $type;
}
}