Я установил пакет ребинг / graphql-laravel версии 5.1.4. Когда я пытаюсь передать запрос через почтальона для получения всех продуктов, я получаю сообщение об ошибке
"Cannot query field \"products\" on type \"Query\". Did you mean \"product\"?"
Моя структура папок
Мой 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();
}
}
Мои 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();
}
}
Мой 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'
],
];
}
}
Моя конфигурация 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,
]
Пытаюсь сделать запрос через почтальона
Однако, когда я пытаюсь получить продукт через «слаг», это работает хорошо. Я застрял со своей проблемой. Вроде все в порядке, но, наверное, мне чего-то не хватает. Я новичок в graphql, и мне нужен толчок в правильном направлении :) Большое спасибо.
Обновлено: Я знаю, что проблема не в кеше. После любых изменений делаю php artisan config:cache.
@AkashSarode да, он работает для продукта по слагам.






Я столкнулся с той же проблемой, но с .Net Gql и angular, по сути, проблема заключается в типе результата, который вы пытаетесь запросить. Отметьте это решение.
Надеюсь, это вам поможет. В любом случае обновите свой статус, чтобы проверить, сохраняется ли проблема.
Ваше здоровье!
он работает для запроса продукта?