2019-08-15 11:35:12 +08:00
|
|
|
# GraphQL
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
GraphQL 組件對 [thecodingmachine/graphqlite](https://github.com/thecodingmachine/graphqlite) 進行抽象。
|
2019-08-15 11:35:12 +08:00
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
## 安裝
|
2019-08-15 11:35:12 +08:00
|
|
|
|
|
|
|
```bash
|
|
|
|
composer require hyperf/graphql
|
|
|
|
```
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
## 快速開始
|
2019-08-15 14:17:03 +08:00
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
### 簡單查詢
|
2019-08-15 11:35:12 +08:00
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use GraphQL\GraphQL;
|
|
|
|
use GraphQL\Type\Schema;
|
|
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
use Hyperf\GraphQL\Annotation\Query;
|
|
|
|
use Hyperf\HttpServer\Annotation\Controller;
|
|
|
|
use Hyperf\HttpServer\Annotation\PostMapping;
|
|
|
|
use Hyperf\HttpServer\Contract\RequestInterface;
|
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
#[Controller]
|
2019-08-15 11:35:12 +08:00
|
|
|
class GraphQLController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Schema
|
|
|
|
*/
|
2021-12-01 16:19:47 +08:00
|
|
|
#[Inject]
|
2019-08-15 11:35:12 +08:00
|
|
|
protected $schema;
|
2021-12-01 16:19:47 +08:00
|
|
|
|
|
|
|
#[PostMapping(path: "/graphql")]
|
2019-08-15 11:35:12 +08:00
|
|
|
public function test(RequestInterface $request)
|
|
|
|
{
|
|
|
|
$rawInput = $request->getBody()->getContents();
|
|
|
|
$input = json_decode($rawInput, true);
|
|
|
|
$query = $input['query'];
|
|
|
|
$variableValues = isset($input['variables']) ? $input['variables'] : null;
|
|
|
|
return GraphQL::executeQuery($this->schema, $query, null, null, $variableValues)->toArray();
|
|
|
|
}
|
2021-12-01 16:19:47 +08:00
|
|
|
|
|
|
|
#[Query]
|
2019-08-15 11:35:12 +08:00
|
|
|
public function hello(string $name): string
|
|
|
|
{
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2019-12-12 16:24:04 +08:00
|
|
|
查詢:
|
2019-08-15 14:17:03 +08:00
|
|
|
```graphql
|
2019-08-15 11:35:12 +08:00
|
|
|
{
|
2019-08-15 14:17:03 +08:00
|
|
|
hello(name: "graphql")
|
2019-08-15 11:35:12 +08:00
|
|
|
}
|
|
|
|
```
|
2019-12-12 16:24:04 +08:00
|
|
|
響應:
|
2019-08-15 11:35:12 +08:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"hello": "graphql"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
### 類型映射
|
2019-08-15 14:17:03 +08:00
|
|
|
|
2019-08-15 11:35:12 +08:00
|
|
|
```php
|
|
|
|
<?php
|
2019-08-15 14:17:03 +08:00
|
|
|
namespace App\Model;
|
2019-08-15 11:35:12 +08:00
|
|
|
|
|
|
|
use Hyperf\GraphQL\Annotation\Type;
|
|
|
|
use Hyperf\GraphQL\Annotation\Field;
|
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
#[Type]
|
2019-08-15 11:35:12 +08:00
|
|
|
class Product
|
|
|
|
{
|
|
|
|
protected $name;
|
|
|
|
protected $price;
|
|
|
|
|
|
|
|
public function __construct(string $name, float $price)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
$this->price = $price;
|
|
|
|
}
|
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
#[Field]
|
2019-08-15 11:35:12 +08:00
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
#[Field]
|
2019-08-15 11:35:12 +08:00
|
|
|
public function getPrice(): ?float
|
|
|
|
{
|
|
|
|
return $this->price;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-08-15 14:17:03 +08:00
|
|
|
在 `GraphQLController` 中加入
|
|
|
|
|
2019-08-15 11:35:12 +08:00
|
|
|
```php
|
|
|
|
<?php
|
2019-08-15 14:17:03 +08:00
|
|
|
use App\Model\Product;
|
2021-12-01 16:19:47 +08:00
|
|
|
use Hyperf\GraphQL\Annotation\Query;
|
2019-08-15 11:35:12 +08:00
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
#[Query]
|
2019-08-15 14:17:03 +08:00
|
|
|
public function product(string $name, float $price): Product
|
|
|
|
{
|
|
|
|
return new Product($name, $price);
|
|
|
|
}
|
2019-08-15 11:35:12 +08:00
|
|
|
```
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
查詢:
|
2019-08-15 14:17:03 +08:00
|
|
|
```graphql
|
2019-08-15 11:35:12 +08:00
|
|
|
{
|
2019-08-15 14:17:03 +08:00
|
|
|
hello(name: "graphql")
|
|
|
|
product(name: "goods", price: 156.5) {
|
|
|
|
name
|
|
|
|
price
|
|
|
|
}
|
2019-08-15 11:35:12 +08:00
|
|
|
}
|
|
|
|
```
|
2019-08-15 14:17:03 +08:00
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
響應:
|
2019-08-15 11:35:12 +08:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"hello": "graphql",
|
|
|
|
"product": {
|
|
|
|
"name": "goods",
|
|
|
|
"price": 156.5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2019-08-15 14:17:03 +08:00
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
更多使用方法可以查看 [GraphQLite](https://graphqlite.thecodingmachine.io/docs/queries) 的文檔。
|