我第一次在具有Graphql支持的Symfony 4应用程序上使用API平台(api-platform/api-pack:v1.2.0
,api-platform/core:v2.4.3
)。我正在尝试从graphql客户端以这种方式调用资源
{
menu(id: "/menus/1") {
type
}
}
这是我收到的错误
"No route matches "/menus/1",
我试图修改IRI,但是没有运气。这是我的完整配置
config/packages/api_platform.yaml
api_platform:
title: 'API'
version: '0.0.1'
allow_plain_identifiers: true
graphql:
enabled: true
graphiql:
enabled: true
mapping:
paths: ['%kernel.project_dir%/src/Entity']
config/routes/api_platform.yaml
api_platform:
resource: .
type: api_platform
prefix: /api
src/Entity/resources.yaml
我只想使用Graphql而不使用REST端点resources:
App\Request\Menu:
attributes:
messenger: true
graphql:
- query
itemOperations: []
collectionOperations: []
src/Request/Menu.php
final class Menu
{
/**
* @var string Menu type
*/
private $type;
/**
* Menu constructor.
*
* @param string $type
*/
public function __construct(string $type)
{
$this->type = $type;
}
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
}
src/Handler/MenuRequestHandler.php
final class MenuRequestHandler implements MessageHandlerInterface
{
/**
* @var \App\Repository\MenuInterfaceRepository
*/
private $repository;
public function __construct(MenuInterfaceRepository $repository
)
{
$this->repository = $repository;
}
public function __invoke(Menu $menuRequest): array
{
return $this->repository->getByType($menuRequest->getType());
}
}
bin/console debug:router
------------------------ -------- -------- ------ -------------------------------------
Name Method Scheme Host Path
------------------------ -------- -------- ------ -------------------------------------
_twig_error_test ANY ANY ANY /_error/{code}.{_format}
app_home_home ANY ANY ANY /test
api_entrypoint ANY ANY ANY /api/{index}.{_format}
api_doc ANY ANY ANY /api/docs.{_format}
api_graphql_entrypoint ANY ANY ANY /api/graphql
api_jsonld_context ANY ANY ANY /api/contexts/{shortName}.{_format}
------------------------ -------- -------- ------ -------------------------------------
也许我缺少通常很明显的东西。
谢谢
答案 0 :(得分:1)
尝试IRI“ / api / menus / 1” 或者,您可以在routes / api_platform.yaml中更改前缀