【发布时间】:2021-04-02 17:03:24
【问题描述】:
我已经安装了一个包rebing/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,
]
我尝试通过邮递员提出请求
但是,当我尝试通过“slug”接收产品时,效果很好。我被我的问题困住了。似乎没问题,但可能我缺少一些东西。我是 graphql 的新手,我需要朝着正确的方向前进 :) 非常感谢。
更新:我知道问题不在缓存中。任何更改后,我都会做 php artisan config:cache。
【问题讨论】:
-
对产品查询有用吗?
-
@AkashSarode 是的,它适用于 slug 的产品。