3

I have installed a package rebing/graphql-laravel version 5.1.4. When I'm trying to pass query through postman for getting all Products I'm getting the error

"Cannot query field \"products\" on type \"Query\". Did you mean \"product\"?"

My structure of folders

enter image description here

My ProductQuery.php

namespace App\GraphQL\Queries;

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

class ProductQuery extends Query
{
    protected $attributes = [
        'name' => 'product',
        'description' => 'A query'
    ];

    /**
     * Return type for these query
     * @return Type
     */
    public function type(): Type
    {
        return GraphQL::type('Product');
    }

    /**
     * Passed arguments for this query
     * @return array
     */
    public function args(): array
    {
        return [
            'slug' => [
                'name' => 'slug',
                'type' => Type::nonNull(Type::string()),
                'rules' => ['required']
            ],
        ];
    }

    /**
     * Resolve Query to get pass an information
     * @param mixed $root
     * @param array $args
     * @return Product
     */
    public function resolve($root, array $args): Product
    {
        return Product::where('slug', $args['slug'])->first();
    }
}

My ProductsQuery.php

<?php

namespace App\GraphQL\Queries;

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

class ProductsQuery extends Query
{
    protected $attributes = [
        'name' => 'products',
    ];


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


    public function resolve($root, $args)
    {
        return Product::all();
    }
}

My ProductType.php

<?php
namespace App\GraphQL\Types;

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

class ProductType extends GraphQLType
{
    protected $attributes = [
        'name' => 'Product',
        'description' => 'A type',
        'model' => Product::class
    ];

    public function fields(): array
    {
        return [
            'id' => [
                'type' => Type::nonNull(Type::int()),
                'description' => 'Product Id'
            ],
            'name' => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'Product Name'
            ],
            'type' => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'Product Type'
            ],
            'slug' => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'Product Slug'
            ],
            'sku' => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'Product SKU'
            ],
            'barcode' => [
                'type' => Type::string(),
                'description' => 'Product Barcode'
            ],
            'description' => [
                'type' => Type::string(),
                'description' => 'Product Description'
            ],
            'status' => [
                'type' => Type::boolean(),
                'description' => 'Product Status'
            ],
            'in_stock' => [
                'type' => Type::boolean(),
                'description' => 'Product In Stock'
            ],
            'track_stock' => [
                'type' => Type::boolean(),
                'description' => 'Product Track Stock'
            ],
            'is_taxable' => [
                'type' => Type::boolean(),
                'description' => 'Product Is Taxable'
            ],
            'qty' => [
                'type' => Type::float(),
                'description' => 'Product Qty'
            ],
            'price' => [
                'type' => Type::float(),
                'description' => 'Product Price'
            ],
            'cost_price' => [
                'type' => Type::float(),
                'description' => 'Product Cost Price'
            ],
            'weight' => [
                'type' => Type::float(),
                'description' => 'Product Weight'
            ],
            'height' => [
                'type' => Type::float(),
                'description' => 'Product Height'
            ],
            'length' => [
                'type' => Type::float(),
                'description' => 'Product Length'
            ],
            'width' => [
                'type' => Type::float(),
                'description' => 'Product Width'
            ],
            'meta_title' => [
                'type' => Type::string(),
                'description' => 'Product Meta Title'
            ],
            'meta_description' => [
                'type' => Type::string(),
                'description' => 'Product Meta Description'
            ],
            'created_at' => [
                'type' => Type::string(),
                'description' => 'Product Created At'
            ],
            'updated_at' => [
                'type' => Type::string(),
                'description' => 'Product Updated At'
            ],
        ];
    }
}

My config graphql.php

    'schemas' => [
        'default' => [
            'query' => [
                'product' => App\GraphQL\Queries\ProductQuery::class,
                'products' => App\GraphQL\Queries\ProductsQuery::class
            ],
            'mutation' => [
            ],
            'middleware' => [],
            'method' => ['get', 'post'],
        ],
    ],

    'types' => [
        'Product' => App\GraphQL\Types\ProductType::class,
    ]

I try to make a request through the postman

enter image description here

However, when I'm trying to receive the product through "slug" it works well. I'm stuck with my problem. It seems all right but probably I something missing. I am new in graphql and I need a push in the right direction :) Thanks so much.

Updated: I know the problem not in the cache. After any changes, I do php artisan config:cache.

JustName
  • 111
  • 4
  • 11

2 Answers2

0

Use

query {
products {
    id
    name
    ...
}}

instead of

{
products {
   id
   name
   ...
}
}
Vitaliy
  • 1
  • 2
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '22 at 05:43
-2

I was facing the same issue but with .Net Gql and angular, essentially the issue is in the result type that you are trying to query. Check this solution.

I hope this help you. Anyway, update your status to check if the issue continue.

Cheers!