对于不可为 null 的字段,不能 return null - rebing/graphql

Cannot return null for non-nullable field - rebing/graphql

我在使用 GraphQL(rebing-graphql)/Larvel 应用程序时遇到问题。当我查询普通的 GraphQL 查询(单个非嵌套)时,应用程序工作正常,但是当我查询嵌套查询时,我面临 "debugMessage":"Cannot return null for non-nullable field \"Make Type.name\".

工作正常的正常查询:

{model{id,name}}

我要执行的嵌套查询:

{model{id,name,make_id{id,name}}

我哪里弄错了?

提前致谢。

制作模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;

class Make extends Model
{
    use HasFactory;
    protected $fillable = [
        'name',
        'logo',
        'website',
    ];

    public function models()
    {
        return $this->hasMany(\App\Models\Model::class);
    }
}

模特模特

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as MModel;

class Model extends MModel
{
    use HasFactory;
    protected $fillable = [
        'make_id',
        'name',
        'website',
    ];

    public function make()
    {
        return $this->belongsTo(Make::class);
    }
}

MakeQuery(Graphql部分)

<?php

namespace App\GraphQL\Queries;

use App\Models\Make;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Query;

class MakeQuery extends Query
{
    protected $attributes = [
        'name' => 'Make Type',
        'description' => 'Fetch Make Query'
    ];

    public function args(): array
    {
        return ["id" => ['type' => Type::int()]];
    }

    public function type(): type
    {
        return Type::listOf(GraphQL::type('make'));        
    }

    public function resolve($root, $args)
    {
        if (isset($args['id'])) {
            return Make::where("id",$args['id'])->get();
        }
        return Make::all();

    }
}

MakeType

<?php

namespace App\GraphQL\Types;

use App\Models\Make;
use Rebing\GraphQL\Support\Facades\GraphQL;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;

class MakeType extends GraphQLType
{
    protected $attributes = [
        'name' => 'Make Type',
        'description' => 'Make API Type',
        'model' => Make::class
    ];

    public function fields(): array
    {
        return [
            "id" => [
                'type' => Type::nonNull(Type::int()),
                'description' => 'Make ID'
            ],
            "name" => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'Make ID'
            ],
            "logo" => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'Make ID'
            ],
            "website" => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'Make ID'
            ]
        ];
    }


}

模型查询

<?php

namespace App\GraphQL\Queries;

use App\Models\Model;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Support\Query;

class ModelQuery extends Query
{
    protected $attributes = [
        'name' => 'Model Type',
        'description' => 'Fetch Model Query'
    ];

    public function args(): array
    {
        return [
            "id" => ['type' => Type::int()]
        ];
    }

    public function type(): type
    {
        return Type::listOf(GraphQL::type('model'));
    }

    public function resolve($root, $args)
    {
        if (isset($args['id'])) {
            return Model::where("id", $args['id'])->get();
        }
        return Model::all();

    }
}

模型类型

<?php

namespace App\GraphQL\Types;

use App\Models\Make;
use App\Models\Model;
use Rebing\GraphQL\Support\Facades\GraphQL;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;

class ModelType extends GraphQLType
{
    protected $attributes = [
        'name' => 'Model Type',
        'description' => 'Model API Type',
        'model' => Model::class
    ];

    public function fields(): array
    {
        return [
            "id" => [
                'type' => Type::nonNull(Type::int()),
                'description' => 'Model ID'
            ],
            "make_id" => [
                'type' => GraphQL::type('make'),
                'description' => 'Model_ID'
            ],
            "name" => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'Model Name'
            ],
            "website" => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'Model website'
            ]
        ];
    }
}

要使您的代码正常工作,您必须做几件事:

首先:确保您的表已满并且具有有效的键关系。

其次:ModelType中将make_id改为makeId

第三: 使用 composer dump-autoload.

重新加载作曲家自动加载

最后: 在你的 Model Model 中最好定义如下列:

public function makeId()
    {
        return $this->belongsTo(Make::class, 'make_id', 'id');
    }

希望这些步骤对您有所帮助。