繁体   English   中英

GraphQL 在我尝试获取所有记录时返回错误消息“无法查询字段...”

[英]GraphQL returns the error message "Cannot query field..." when I try to get all records

我已经安装了 package rebing/graphql-laravel版本 5.1.4。 当我尝试通过 postman 传递查询以获取所有产品时,我收到了错误

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

我的文件夹结构

在此处输入图像描述

我的产品查询.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();
    }
}

我的产品查询.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();
    }
}

我的产品类型.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,
    ]

我尝试通过 postman 提出请求

在此处输入图像描述

但是,当我尝试通过“slug”接收产品时,效果很好。 我被我的问题困住了。 似乎没问题,但可能我缺少一些东西。 我是 graphql 的新手,我需要朝着正确的方向努力:) 非常感谢。

更新:我知道问题不在缓存中。 进行任何更改后,我执行php artisan config:cache

利用

query {
products {
    id
    name
    ...
}}

代替

{
products {
   id
   name
   ...
}
}

我遇到了同样的问题,但是使用.Net Gql 和 angular,本质上问题在于您尝试查询的结果类型。 检查此解决方案

我希望这对你有帮助。 无论如何,请更新您的状态以检查问题是否仍然存在。

干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM