如何解析通过类型语言指定的联合类型?

时间:2019-06-04 08:19:47

标签: graphql-php

我正在使用graphql-phptype 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)"
        },
  ...

所以我真的不知道该如何定义联合类型。我不知道应该如何/在何处定义resolveTypeisTypeOf以及应该返回什么。

0 个答案:

没有答案