Graphql 如何解决与拉雷维尔灯塔的接口错误?

Graphql 如何解决与拉雷维尔灯塔的接口错误?,graphql,Graphql,我有一个问题,我有联系人,但分为个人或公司,创建一个界面,但在执行时会生成一个错误: #contact types enum contactType{ persona @enum(value: 2) empresa @enum(value: 3) } enum contactIdType{ cedula @enum(value: 1) nit @enum(value: 2) pasaporte @enum(value: 3) cedula extr

我有一个问题,我有联系人,但分为个人或公司,创建一个
界面
,但在执行时会生成一个错误:

#contact types
enum contactType{
    persona @enum(value: 2)
    empresa @enum(value: 3)
}
enum contactIdType{
    cedula @enum(value: 1)
    nit @enum(value: 2)
    pasaporte @enum(value: 3)
    cedula extranjera @enum(value: 4)
}
interface Contact{
    id_contact:ID!
    type:contactType!
    name:String!
    identification_type:contactIdType!
    identification_number:Int!
    email:String!
    phones:String!
    state:Int!
    address:String
    web_site:String
}

type Person implements Contact{
    id_contact:ID!
    id_parent_contact:Int
    id_job:Int
    type:contactType!
    name:String!
    lastname:String
    identification_type:contactIdType!
    identification_number:Int!
    email:String!
    phones:String!
    state:Int!
    address:String
    web_site:String
}

type Company implements Contact{
    id_contact:ID!
    type:contactType!
    name:String!
    identification_type:contactIdType!
    identification_number:Int!
    email:String!
    phones:String!
    state:Int!
    locate:String
    city:String
    address:String
    web_site:String
}
查询:

type Query {    
    #Contacts
    contacts: [Contact!]! @all(model: "App\\Contact")
    contacts_paginator: [Contact]! @paginate(type: "paginator" model: "App\\Contact")
    contact(name: String! @eq): Contact @find(model: "App\\Contact")

}
///////////////////////////////////////////////////////

query{
  contacts{
    name
    type     
  }
}
结果:

{
  "errors": [
    {
      "debugMessage": "Abstract type Contact must resolve to an Object type at runtime for field Query.contacts with value \"{\"id_contact\":4,\"id_parent_contact\":null,\"id_job\":null,\"type\":2,\"name\":\"Laura\",\"lastname\":\"Sanchez\",\"identification_type\":1,\"identification_number\":1049342129,\"email\":\"laura@gmail.com\",\"phones\":\"3203428890\",\"state\":1,\"locate\":null,\"city\":null,\"address\":null,\"web_site\":null}\", received \"Contact\". Either the Contact type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.",
      "message": "Internal server error",
      "extensions": {
        "category": "internal"
      },
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "contacts",
        0
      ]

我正在与拉威尔灯塔合作

错误信息非常清楚。让我为您详细介绍一下:

Abstract type Contact must resolve to an Object type at runtime for field Query.contacts
您正在从您的字段返回一个界面
联系人
。GraphQL需要确定具体的对象类型,在您的案例中是
公司
个人

Either the Contact type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.
这部分告诉您需要做什么:您需要某种方法来确定
联系人
的条目是
公司
还是
个人

Either the Contact type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.
通常,如果您使用不同的模型类,Lighthouse可以自动确定。看起来您使用的是同一个,因此必须实现自定义类型解析器

您只需使用以下工具创建一个:

php artisan lighthouse:interface Contact

阅读有关Lighthouse中接口的更多信息:

错误信息非常清楚。让我为您详细介绍一下:

Abstract type Contact must resolve to an Object type at runtime for field Query.contacts
您正在从您的字段返回一个界面
联系人
。GraphQL需要确定具体的对象类型,在您的案例中是
公司
个人

Either the Contact type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.
这部分告诉您需要做什么:您需要某种方法来确定
联系人
的条目是
公司
还是
个人

Either the Contact type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.
通常,如果您使用不同的模型类,Lighthouse可以自动确定。看起来您使用的是同一个,因此必须实现自定义类型解析器

您只需使用以下工具创建一个:

php artisan lighthouse:interface Contact

阅读更多关于Lighthouse中接口的信息:

现在错误发生了变化,因为他相信:
灯塔:接口触点
生成此文件的:

<?php

namespace App\GraphQL\Interfaces;

use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Schema\TypeRegistry;

class Contact
{
    /**
     * The type registry.
     *
     * @var \Nuwave\Lighthouse\Schema\TypeRegistry
     */
    protected $typeRegistry;

    /**
     * Constructor.
     *
     * @param  \Nuwave\Lighthouse\Schema\TypeRegistry  $typeRegistry
     * @return void
     */
    public function __construct(TypeRegistry $typeRegistry)
    {
        $this->typeRegistry = $typeRegistry;
    }

    /**
     * Decide which GraphQL type a resolved value has.
     *
     * @param  mixed  $rootValue The value that was resolved by the field. Usually an Eloquent model.
     * @param  \Nuwave\Lighthouse\Support\Contracts\GraphQLContext  $context
     * @param  \GraphQL\Type\Definition\ResolveInfo  $resolveInfo
     * @return \GraphQL\Type\Definition\Type
     */
    public function resolveType($rootValue, GraphQLContext $context, ResolveInfo $resolveInfo): Type
    {
        // Default to getting a type with the same name as the passed in root value
        // TODO implement your own resolver logic - if the default is fine, just delete this class

        return $this->typeRegistry->get(class_basename($rootValue));
    }
}
在我的模型中,我有:


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected  $table= 'contacts';

    protected $primaryKey = 'id_contact';

    public $timestamps = false;

    protected $fillable = [
        'id_parent_contact',
        'id_job',
        'type',
        'name',
        'lastname',
        'identification_type',
        'identification_number',
        'email',
        'phones',
        'state',
        'locate',
        'city',
        'address',
        'web_site'
    ];
}


现在错误变了,因为他相信:
灯塔:接口触点
生成此文件的:

<?php

namespace App\GraphQL\Interfaces;

use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Schema\TypeRegistry;

class Contact
{
    /**
     * The type registry.
     *
     * @var \Nuwave\Lighthouse\Schema\TypeRegistry
     */
    protected $typeRegistry;

    /**
     * Constructor.
     *
     * @param  \Nuwave\Lighthouse\Schema\TypeRegistry  $typeRegistry
     * @return void
     */
    public function __construct(TypeRegistry $typeRegistry)
    {
        $this->typeRegistry = $typeRegistry;
    }

    /**
     * Decide which GraphQL type a resolved value has.
     *
     * @param  mixed  $rootValue The value that was resolved by the field. Usually an Eloquent model.
     * @param  \Nuwave\Lighthouse\Support\Contracts\GraphQLContext  $context
     * @param  \GraphQL\Type\Definition\ResolveInfo  $resolveInfo
     * @return \GraphQL\Type\Definition\Type
     */
    public function resolveType($rootValue, GraphQLContext $context, ResolveInfo $resolveInfo): Type
    {
        // Default to getting a type with the same name as the passed in root value
        // TODO implement your own resolver logic - if the default is fine, just delete this class

        return $this->typeRegistry->get(class_basename($rootValue));
    }
}
在我的模型中,我有:


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected  $table= 'contacts';

    protected $primaryKey = 'id_contact';

    public $timestamps = false;

    protected $fillable = [
        'id_parent_contact',
        'id_job',
        'type',
        'name',
        'lastname',
        'identification_type',
        'identification_number',
        'email',
        'phones',
        'state',
        'locate',
        'city',
        'address',
        'web_site'
    ];
}