【发布时间】:2019-09-16 22:09:31
【问题描述】:
我第一次在支持 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}
------------------------ -------- -------- ------ -------------------------------------
也许我遗漏了一些通常很明显的东西。
谢谢
【问题讨论】:
-
好吧,看来实际上不能禁用 REST github.com/api-platform/core/issues/2796
-
模型在哪里?不是资源,不是路线……阅读文档……graphql'explorer'应该在
/api/graphiql上可用@
标签: graphql symfony4 api-platform.com