avris/graphql-bundle
Composer 安装命令:
composer require avris/graphql-bundle
包简介
Simplify Graphql configuration using plain methods with typehints and annotations.
关键字:
README 文档
README
Simplify Graphql configuration using plain methods with typehints and annotations.
This bundle is a wrapper on webonyx/graphql-php.
Disclaimer
I'm new to GraphQL, currently writing my first project using it. I'm sure that many things are missing from the bundle and many assumptions I made are wrong, so please be understanding, and if possible help out with a pull request.
Instalation
$ composer require avris/graphql-bundle
Then add a route:
avris_graphql_main:
path: '/graph'
controller: Avris\GraphqlBundle\Controller\GraphqlController::main
Example
Let's say you have the following type in your app:
<?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;
}
}
And the following GraphQL schema:
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']));
},
]
],
]),
]);
Using this bundle, you can rewrite the entity/type like this:
<?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;
}
}
And your schema/controllers like this:
<?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';
}
}
and:
<?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));
}
}
The bundle will parse all the classes in src/Controller and src/Entity
(that list can be configured in the config key avris_graphql.load)
looking for the Graphql annotations.
All parameters of a query-method, as well as its return type have to be typehinted.
Supported built-in types are string, bool, int and float.
You can also typehint json and any type you registered with @Graphql\Type.
Additionally, you can use id, list (int[]) and union (int|float),
but for that you need an @Graph\ParamType or @Graph\ReturnType annotation for that.
For debugging you might find the function dqd useful (it stands for "dump in query and die").
It dumps vars (with symfony/var-dumper), sets HTTP response code to 500 (required for some GraphQL clients) and dies.
Copyright
- Author: Andre Prusinowski (Avris.it)
- Licence: MIT
avris/graphql-bundle 适用场景与选型建议
avris/graphql-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 816 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「annotations」 「symfony」 「graphql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 avris/graphql-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 avris/graphql-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 avris/graphql-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A collection of helpers for PHPUnit to ease testing Tarantool libraries.
GraphQL authentication for your headless Craft CMS applications.
The bundle for easy using json-rpc api on your project
Authorize introspection documentarion for rebing/graphql-laravel
Sylius backend integration for Vue Storefront 2
GraphQL client and query builder.
统计信息
- 总下载量: 816
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-27