我正在使用graphql-php
在type language style as defined here中定义我的架构,并使用a custom default resolver来执行某个方法(如果存在的话),并添加以下内容:
elseif (method_exists($source, $fieldName)) {
$property = $source->{$fieldName}();
}
通过这种方式,我定义了每个实现名为GraphQL字段的方法的模型。
文档指定了如何定义联合类型through the inline style,但是我已经通过类型语言指定了自己的架构。从我的模式中摘录以下内容:
type ServiceProvider {
entity: ServiceProviderEntity
data: [ServiceProviderDetails]
}
union ServiceProviderEntity = Provider | Service
我用以下类别定义了ServiceProvider
类型:
class ServiceProvider
{
/**
* A list of service provider details.
*
* @var ServiceProviderDetails[]
*/
private $data = [];
/**
* @var ServiceProviderEntity
*/
private $entity;
/**
* It sets a list of information related to this service-provider relationship.
*
* @param ServiceProviderDetails[] $data The list of data to be set.
*
* @return ServiceProvider
*/
public function setData(array $data): ServiceProvider
{
$this->data = $data;
return $this;
}
public function entity()
{
$this->logger->debug("Calling entity", [$this->entity->isTypeOf()]);
return $this->entity->entity();
}
public function setEntity(ServiceProviderEntity $entity)
{
$this->entity = $entity;
return $this;
}
/**
* It returns the list of details.
*
* @return ServiceProviderDetails[]
*/
public function data()
{
return $this->data;
}
/**
* It adds a service/provider as details related to the entity.
*
* @param ServiceProviderDetails $item An instance of ServiceProviderDetails.
*
* @return ServiceProvider
*/
public function addDataItem(ServiceProviderDetails $item)
{
array_push($this->data, $item);
return $this;
}
}
ServiceProviderEntity
的主要方法如下:
public function setEntity($entity)
{
if (!empty($entity)) {
$this->typename = (new ReflectionClass($entity))->getShortName();
switch ($this->typename) {
case "Service":
$this->entity = (new Service())->setCoreService($entity);
break;
case "Provider":
$this->entity = (new Provider())->setCoreProvider($entity);
break;
default:
throw new ServicesException("Cannot find {$this->typename}");
}
}
return $this;
}
当我执行查询以检索单个提供者的服务时,我创建了一个ServiceProviderEntity
,并使用方法Provider
传递了一个Service
或一个ServiceProviderEntity#setEntity
实例。
但是,我不断收到错误消息:
{
"errors": [
{
"debugMessage": "Abstract type ServiceProviderEntity must resolve to an Object type at runtime for field ServiceProvider.entity with value \"instance of SosTariffe\\Components\\SosServices\\Schema\\Model\\Provider\", received \"null\". Either the ServiceProviderEntity type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.",
"message": "Internal server error",
"extensions": {
"category": "internal"
},
"locations": [
{
"line": 4,
"column": 7
}
],
"path": [
"getServicesByProviderId",
"entity"
],
"trace": [
{
"file": "/home/ccastelli/Documents/sos-services/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
"line": 1038,
"call": "GraphQL\\Executor\\ReferenceExecutor::ensureValidRuntimeType(null, GraphQLType: ServiceProviderEntity, instance of GraphQL\\Type\\Definition\\ResolveInfo, instance of SosTariffe\\Components\\SosServices\\Schema\\Model\\Provider)"
},
...
所以我真的不知道该如何定义联合类型。我不知道应该如何/在何处定义resolveType
或isTypeOf
以及应该返回什么。