Home > Net >  Cannot return null for non-nullable field - rebing/graphql
Cannot return null for non-nullable field - rebing/graphql

Time:10-09

I have a problem with GraphQL(rebing-graphql)/Larvel app. App works fine when I query normal GraphQL query(single not nested), but when I query nested one, I face "debugMessage":"Cannot return null for non-nullable field \"Make Type.name\".

Normal query which works fine:

{model{id,name}}

Nested query that I want to execute:

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

Where am I made mistake?

Thanks in advance.

Make Model

<?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);
    }
}

Model Model

<?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 part)

<?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'
            ]
        ];
    }


}

ModelQuery

<?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();

    }
}

ModelType

<?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'
            ]
        ];
    }
}

CodePudding user response:

There are several things that you have to done to get your code works:

First: Be sure that your tables are full and have valid key relations.

Second: In ModelType change make_id to makeId.

Third: Reload composer autoload with composer dump-autoload.

Finally: In your Model Model it's better to define a column like below:

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

I hope these steps would help you.

  • Related