bastsys / graphql-bundle
Symfony GraphQl Bundle
Requires
- php: ^7.4
- ext-json: *
- bastsys/locale-bundle: ^1.1.0
- bastsys/utils-bundle: ^1.3.3
- symfony/config: ^5.1
- symfony/dependency-injection: ^5.1
- symfony/event-dispatcher: ^5.1
- symfony/security-bundle: ^5.1
- youshido/graphql: ~1.4
Requires (Dev)
- composer/composer: ~1.2
- phpunit/phpunit: *
- symfony/framework-bundle: ^5.0.0
- dev-master
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.0
- 2.0.x-dev
- 2.0.12
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 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-patch-1
- dev-develop
This package is auto-updated.
Last update: 2024-09-29 05:48:51 UTC
README
这是一个基于纯PHP GraphQL 服务器实现的Bundle
此Bundle为您提供了以下功能
- 与GraphQL RFC规范完全兼容
- 敏捷的面向对象结构,用于构建您的GraphQL模式
- 直观的类型系统,让您能够更快地构建项目并保持一致性
- 为开发中的GraphQL模式内置验证
- 带有大量示例的文档良好的类
- 自动创建端点/graphql来处理请求
有一个简单的演示应用程序,展示我们如何构建API,请参阅GraphQLDemoApp。
目录
安装
我们假设您已经安装了composer
,如果没有,请从官方网站安装。
如果您需要安装Symfony框架的帮助,请参阅https://symfony.com.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
中启用bundle
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
文件中。
有一种方法可以使用内联方法而不创建Schema类,为此,您必须定义自己的GraphQL控制器并使用处理器的
->setSchema
方法来设置Schema。
创建Schema类的最快方法是使用此Bundle附带的自带生成器
php bin/console graphql:configure AppBundle
在这里,AppBundle是类将被生成的Bundle的名称。
您将被要求确认创建类。
在您向配置文件添加参数后,请在浏览器中尝试访问以下链接——https://:8000/graphql?query={hello(name:World)}
或者,您可以在控制台使用CURL客户端执行相同的请求
curl https://:8000/graphql --data "query={ hello(name: \"World\") }"
测试Schema的成功响应将显示
{"data":{"hello":"world!"}}
这意味着您已配置好Symfony框架的GraphQL Bundle,现在可以构建您的GraphQL模式了
下一步是执行以下操作以链接GraphiQL Explorer的资产
php bin/console assets:install --symlink
现在您可以在https://:8000/graphql/explorer
访问它
Symfony特性
类AbstractContainerAwareField
AbstractContainerAwareField 类用于自动将容器传递给字段,并在 resolve 函数中添加使用容器的能力
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'; }
服务方法作为可调用对象
可以将服务方法作为 resolve 可调用传递
$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
文件中,并带有 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/。