首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Laravel中用lightHouse解决接口错误?

如何在Laravel中用lightHouse解决接口错误?
EN

Stack Overflow用户
提问于 2019-08-16 11:10:22
回答 2查看 1.2K关注 0票数 1

我有一个问题,我有联系人,但它被分成个人或公司,创建一个interface,但在执行它的时候,它会生成一个错误:

代码语言:javascript
复制
#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
}

查询:

代码语言:javascript
复制
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")

}

///

代码语言:javascript
复制
query{
  contacts{
    name
    type     
  }
}

结果:

代码语言:javascript
复制
{
  "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
      ]

我在拉威尔灯塔工作

EN

回答 2

Stack Overflow用户

发布于 2019-08-22 15:24:05

错误消息非常清楚。让我为你详细分析一下:

代码语言:javascript
复制
Abstract type Contact must resolve to an Object type at runtime for field Query.contacts

您将从您的字段返回一个接口Contact。GraphQL需要确定一个具体的对象类型,在本例中是CompanyPerson

代码语言:javascript
复制
Either the Contact type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.

这一部分告诉您需要发生什么:您需要某种方法来确定Contact条目是Company还是Person

通常,如果你使用不同的模型类,灯塔可以自动确定这一点。看起来您正在使用相同的类型解析器,因此您必须实现一个自定义类型解析器。

您可以使用以下命令简单地创建一个:

代码语言:javascript
复制
php artisan lighthouse:interface Contact

阅读有关灯塔中接口的更多信息:https://lighthouse-php.com/master/the-basics/types.html#interface

票数 0
EN

Stack Overflow用户

发布于 2019-08-27 06:13:09

现在错误改变了,因为他相信:lighthouse:interface contact和生成了这个文件的:

代码语言:javascript
复制
<?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));
    }
}

结果:

代码语言:javascript
复制
{
  "errors": [
    {
      "debugMessage": "Argument 2 passed to App\\GraphQL\\Interfaces\\Contact::resolveType() must be an instance of App\\GraphQL\\Interfaces\\GraphQLContext, instance of Nuwave\\Lighthouse\\Schema\\Context given, called in C:\\Users\\Guecha\\Laravel-projects\\APP_ControlIngenieria\\app\\vendor\\webonyx\\graphql-php\\src\\Type\\Definition\\InterfaceType.php on line 114",
      "message": "Internal server error",
      "extensions": {
        "category": "internal"
      },
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "contacts",
        0
      ],

在我的模型中,我有:

代码语言:javascript
复制
<?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'
    ];
}

我不知道错误是什么:(

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57518502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档