hyperf/docs/zh-hk/graphql.md

142 lines
2.4 KiB
Markdown
Raw Normal View History

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;
#[Controller]
2019-08-15 11:35:12 +08:00
class GraphQLController
{
/**
* @var Schema
*/
#[Inject]
2019-08-15 11:35:12 +08:00
protected $schema;
#[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();
}
#[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;
#[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;
}
#[Field]
2019-08-15 11:35:12 +08:00
public function getName(): string
{
return $this->name;
}
#[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;
use Hyperf\GraphQL\Annotation\Query;
2019-08-15 11:35:12 +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) 的文檔。