avris / graphql-bundle
使用类型提示和注解简化 GraphQL 配置。
v0.6
2020-03-22 16:46 UTC
Requires
- php: ^7.1
- doctrine/annotations: ^1.6
- symfony/dependency-injection: ^4.0|^5.0
- symfony/finder: ^4.0|^5.0
- symfony/http-kernel: ^4.0|^5.0
- symfony/security-bundle: ^4.0|^5.0
- webonyx/graphql-php: ^0.12.5
Requires (Dev)
- symfony/test-pack: ^1.0
- symfony/var-dumper: ^4.0|^5.0
README
使用类型提示和注解简化 GraphQL 配置。
此包是 webonyx/graphql-php
的包装。
免责声明
我是 GraphQL 的新手,目前正在使用它编写我的第一个项目。我相信包中缺少很多东西,我做出的许多假设都是错误的,所以请多多包涵,如果可能的话,请通过拉取请求提供帮助。
安装
$ composer require avris/graphql-bundle
然后添加路由
avris_graphql_main:
path: '/graph'
controller: Avris\GraphqlBundle\Controller\GraphqlController::main
示例
假设你应用中有一个如下类型
<?php
namespace App\Entity;
final class User
{
// properties, constructor, etc.
private static $type = null;
final public static function type(): ObjectType
{
return static::$type ?: static::$type = static::buildType();
}
private static function buildType(): ObjectType
{
return new ObjectType([
'name' => 'User',
'fields' => [
'id' => [
'type' => Type::id(),
'resolve' => function (User $user) {
return $user->getId();
},
],
'email' => [
'type' => Type::string(),
'resolve' => function (User $user) {
return $user->getEmail();
},
],
],
]);
}
public function getId(): string
{
return $this->id;
}
public function getEmail(): string
{
return $this->email;
}
}
以及以下 GraphQL 模式
new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'hello' => [
'type' => Type::string(),
'resolve' => function () {
return 'GraphQL API';
}
],
'user' => [
'type' => User::type(),
'args' => [
'id' => ['type' => Type::nonNull(Type::id())],
],
'resolve' => $this->accessControl->guard(function ($root, $args) {
return $this->repository->find($args['id']);
}, 'ROLE_ADMIN'),
],
'currentUser' => [
'type' => User::type(),
'resolve' => function ($root, $args) {
$user = $this->tokenStorage->getToken()->getUser();
return $user instanceof User ? $user : null;
},
],
'jwt' => [
'type' => Type::string(),
'args' => [
'login' => Type::nonNull(Type::string()),
'password' => Type::nonNull(Type::string()),
],
'resolve' => function ($root, $args) {
$user = $this->repository->findOneBy(['email' => $args['login']]);
if (!$user || !$this->encoder->isPasswordValid($user, $args['password'])) {
return null;
}
return (string) $this->jwtManager->issue($user);
},
]
],
]),
'mutation' => new ObjectType([
'name' => 'Mutation',
'fields' => [
'userRegistered' => [
'type' => User::type(),
'args' => [
'email' => Type::nonNull(Type::string()),
'password' => Type::nonNull(Type::string()),
],
'resolve' => function ($root, $args) {
return $this->eventDispatcher->dispatch(new UserRegisteredEvent($args['email'], $args['password']));
},
]
],
]),
]);
使用此包,你可以像这样重写实体/类型
<?php
namespace App\Entity;
use Avris\GraphqlBundle\Annotation as Graphql;
/**
* @Graphql\Type
*/
final class User
{
// properties, constructor, etc.
/**
* @Graphql\Query
*/
public function getId(): string
{
return $this->id;
}
/**
* @Graphql\Query
*/
public function getEmail(): string
{
return $this->email;
}
}
以及你的模式/控制器像这样
<?php
namespace App\Controller;
use Avris\GraphqlBundle\Annotation as Graphql;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
final class HomeController extends AbstractController
{
/**
* @Graphql\Query
*/
public function hello(): string
{
return 'GraphQL API';
}
}
和
<?php
namespace App\Controller;
use Avris\GraphqlBundle\Annotation as Graphql;
use App\Entity\User;
use App\Events\UserRegistered;
final class UserController extends BaseController
{
// dependecies...
/**
* @Graphql\Query
* @Graphql\Security("ROLE_ADMIN")
* @Graphql\ParamType("id", var="id")
*/
public function user(string $id): ?User
{
return $this->repository->find($id);
}
/**
* @Graphql\Query
*/
public function currentUser(): ?User
{
$user = $this->tokenStorage->getToken()->getUser();
return $user instanceof User ? $user : null;
}
/**
* @Graphql\Query
*/
public function jwt(string $login, string $password): ?string
{
$user = $this->repository->findOneBy(['email' => $login]);
if (!$user || !$this->encoder->isPasswordValid($user, $password)) {
return null;
}
return (string) $this->jwtManager->issue($user);
}
/**
* @Graphql\Query("mutation")
*/
public function userRegistered(string $email, string $password): User
{
return $this->eventDispatcher->dispatch(new UserRegisteredEvent($email, $password));
}
}
该包将解析 src/Controller
和 src/Entity
中的所有类(此列表可以在配置密钥 avris_graphql.load
中配置),寻找 Graphql
注解。
查询方法的全部参数以及其返回类型都必须进行类型提示。支持内置类型 string
、bool
、int
和 float
。您还可以类型提示 json
以及您使用 @Graphql\Type
注册的任何类型。
此外,您还可以使用 id
、列表(int[]
)和联合(int|float
),但您需要为这些使用 @Graph\ParamType
或 @Graph\ReturnType
注解。
对于调试,您可能会发现函数 dqd
(代表 "在查询中转储并终止")很有用。它会转储变量(使用 symfony/var-dumper
),将 HTTP 响应代码设置为 500(对于某些 GraphQL 客户端是必需的)并终止。
版权
- 作者: Andre Prusinowski (Avris.it)
- 许可证: MIT