我有问题,我有联系人,但将其分为“个人”或“公司”,创建了一个interface
,但在执行该操作时会产生错误:
#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
]
我正在使用laravel灯塔
答案 0 :(得分:0)
错误消息很清楚。让我为您分解一下:
Abstract type Contact must resolve to an Object type at runtime for field Query.contacts
您正在从您的字段返回接口Contact
。 GraphQL需要确定具体的对象类型,在您的情况下为Company
或Person
。
Either the Contact type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.
该部分告诉您需要发生什么:您需要某种方法来确定Contact
的条目是Company
还是Person
。
通常,如果您使用不同的模型类,Lighthouse可以自动确定。看来您使用的是同一设备,因此您必须实现一个自定义类型解析器。
您可以使用以下方法简单地创建一个:
php artisan lighthouse:interface Contact
详细了解Lighthouse中的接口:https://lighthouse-php.com/master/the-basics/types.html#interface
答案 1 :(得分:0)
现在错误已更改,因为他相信:
<?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));
}
}
然后生成了这个文件:
{
"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
],
结果:
<?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'
];
}
在我的模型中,我有:
{{1}}
我不知道错误是什么:(