youshido / graphql-bundle
Symfony GraphQl Bundle
Requires
- php: >=5.6
- youshido/graphql: ~1.4
Requires (Dev)
- composer/composer: ~1.2
- phpunit/phpunit: ~4.7
- symfony/framework-bundle: ~2.7|~3.0
- dev-master
- 2.0.x-dev
- v1.4.1
- v1.4
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2.2
- v1.1.2.1
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-develop
This package is not auto-updated.
Last update: 2024-09-09 09:16:25 UTC
README
这是一个基于纯 PHP GraphQL Server 实现的捆绑包
此捆绑包为您提供
- 与 GraphQL RFC 规范 完全兼容
- 敏捷的面向对象结构,用于构建您的 GraphQL 模式
- 直观的类型系统,让您能够更快地构建项目并保持一致性
- 为开发的 GraphQL 模式提供内置验证
- 具有大量示例的文档良好的类
- 自动创建的端点 /graphql 以处理请求
有简单的演示应用程序来演示我们如何构建我们的 API,请参阅 GraphQLDemoApp。
目录
安装
我们假设您有 composer
,如果没有 – 请从 官方网站 安装。
如果您需要安装 Symfony 框架的帮助,请参阅此链接 https://symfony.ac.cn/doc/current/book/installation.html。
安装 Symfony 的快捷方式:
composer create-project symfony/framework-standard-edition my_project_name
一旦您的 composer 运行正常 – 您就可以安装 GraphQL Bundle。
转到您的项目文件夹并运行
composer require youshido/graphql-bundle
然后在您的 app/AppKernel.php
中启用捆绑包
new Youshido\GraphQLBundle\GraphQLBundle(),
将路由引用添加到 app/config/routing.yml
graphql: resource: "@GraphQLBundle/Controller/"
或者
graphql: resource: "@GraphQLBundle/Resources/config/route.xml"
如果您尚未配置 Web 服务器,可以使用捆绑的版本,只需运行 php bin/console server:run
。
让我们检查您是否已经正确完成了一切 – 尝试访问 URL localhost:8000/graphql
。
您应该会收到以下错误的 JSON 响应
{"errors":[{"message":"Schema class does not exist"}]}
这是因为处理器尚未指定 GraphQL 模式。您需要创建一个 GraphQL 模式类,并将其设置在您的 app/config/config.yml
文件中。
有一种方法可以使用内联方法而不创建模式类,为了做到这一点,您必须定义自己的 GraphQL 控制器,并使用处理器的
->setSchema
方法设置模式。
创建模式类的最快方法是使用此捆绑包中提供的生成器
php bin/console graphql:configure AppBundle
这里 AppBundle 是将在其中生成类的捆绑包的名称。
您将需要确认创建一个类。
添加配置文件中的参数后,尝试在浏览器中访问以下链接 – http://localhost:8000/graphql?query={hello(name:World)}
或者,您可以使用控制台中的 CURL 客户端执行相同的请求
curl http://localhost:8000/graphql --data "query={ hello(name: \"World\") }"
成功响应将显示测试模式的结果
{"data":{"hello":"world!"}}
这意味着您已为 Symfony 框架配置了 GraphQL Bundle,现在可以构建您的 GraphQL 模式
下一步是执行以下命令以链接 GraphiQL 探索器的资产
php bin/console assets:install --symlink
现在您可以通过 http://localhost:8000/graphql/explorer
访问它
Symfony 功能
抽象容器感知字段类
用于自动将容器传递给字段的AbstractContainerAwareField类,增加了在解析函数中使用容器的能力
class RootDirField extends AbstractContainerAwareField { /** * @inheritdoc */ public function getType() { return new StringType(); } /** * @inheritdoc */ public function resolve($value, array $args, ResolveInfo $info) { return $this->container->getParameter('kernel.root_dir'); } /** * @inheritdoc */ public function getName() { return 'rootDir'; }
服务方法作为可调用对象
可以将服务方法作为解析调用者传递
$config->addField(new Field([ 'name' => 'cacheDir', 'type' => new StringType(), 'resolve' => ['@resolve_service', 'getCacheDir'] ]))
事件
您可以使用Symfony事件分发器来控制在解析GraphQL查询时发生的特定事件。
namespace ...\...\..; use Youshido\GraphQL\Event\ResolveEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class MyGraphQLResolveEventSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return [ 'graphql.pre_resolve' => 'onPreResolve', 'graphql.post_resolve' => 'onPostResolve' ]; } public function onPreResolve(ResolveEvent $event) { //$event->getFields / $event->getAstFields().. } public function onPostResolve(ResolveEvent $event) { //$event->getFields / $event->getAstFields().. } }
配置
现在配置您的订阅者,以便捕获事件。在Symfony中,这可以通过XML、Yaml或PHP完成。
<service id="my_own_bundle.event_subscriber.my_graphql_resolve_event_subscriber" class="...\...\...\MyGraphQLResolveEventSubscriber"> <tag name="graphql.event_subscriber" /> </service>
安全
Bundle提供了两种保护应用程序的方法:使用黑白名单操作列表或使用安全投票者。
黑白名单
用于保护某些根操作。要启用它,您需要在config.yml文件中写入以下内容
graphql: #... security: black_list: ['hello'] # or white_list: ['hello']
使用安全投票者
用于保护任何字段解析,支持两种类型的保护:根操作和任何其他字段解析(包括内部字段、标量类型字段、根操作)。要使用指定的逻辑保护根操作,您需要在配置中启用它并使用SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE
属性。同样,要启用字段保护,也需要进行相同的操作,但在这个情况下使用SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE
属性。关于投票者的官方文档。
注意:启用字段安全会导致性能显著降低
配置示例
graphql: security: guard: field: true # for any field security operation: true # for root level security
投票者示例(添加到您的services.yml文件中,并带有tag security.voter
)
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authorization\Voter\Voter; use Youshido\GraphQL\Execution\ResolveInfo; use Youshido\GraphQLBundle\Security\Manager\SecurityManagerInterface; class GraphQLVoter extends Voter { /** * @inheritdoc */ protected function supports($attribute, $subject) { return in_array($attribute, [SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE, SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE]); } /** * @inheritdoc */ protected function voteOnAttribute($attribute, $subject, TokenInterface $token) { // your own validation logic here if (SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE == $attribute) { /** @var $subject ResolveInfo */ if ($subject->getField()->getName() == 'hello') { return false; } return true; } elseif (SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE == $attribute) { /** @var $subject Query */ if ($subject->getName() == '__schema') { return true; } } } }
GraphiQL扩展
要运行graphiql扩展,只需尝试访问http://your_domain/graphql/explorer
文档
所有详细文档可在主GraphQL仓库中找到 – http://github.com/youshido/graphql/。