PHPackages                             bowens-h/laravel-graphql-extend - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. bowens-h/laravel-graphql-extend

ActiveLibrary[API Development](/categories/api)

bowens-h/laravel-graphql-extend
===============================

Folkloreatelier/laravel-graphql 包的功能扩展。

v1.0.5(7y ago)455MITPHPPHP &gt;=5.5.9

Since Oct 3Pushed 7y agoCompare

[ Source](https://github.com/bowens-h/laravel-graphql-extend)[ Packagist](https://packagist.org/packages/bowens-h/laravel-graphql-extend)[ RSS](/packages/bowens-h-laravel-graphql-extend/feed)WikiDiscussions develop Synced yesterday

READMEChangelog (6)Dependencies (4)Versions (9)Used By (0)

Laravel GraphQL Extend
======================

[](#laravel-graphql-extend)

Folkloreatelier/laravel-graphql 包的功能扩展。扩展功能如下：

1. 新增 Column 类型文件，用于映射数据表字段，输出的数据可以被 Type 类型文件的 fields 方法使用，主要避免数据表字段的重复定义；
2. Type 类型文件的 fields 方法中字段可以根据表字段自动生成；

安装
--

[](#安装)

#### 依赖:

[](#依赖)

- [Laravel 5.x](https://github.com/laravel/laravel)
- [laravel graphql 1.\*](https://github.com/Folkloreatelier/laravel-graphql)
- [doctrine dbal &gt;=2.6](https://github.com/doctrine/dbal)

**1-** [安装 Folkloreatelier/laravel-graphql 包](https://github.com/Folkloreatelier/laravel-graphql)

**2-** `composer.json` 添加包

```
{
  "require": {
    "bowens-h/laravel-graphql-extent": "~1.0.0"
  }
}
```

**3-** 进行 composer 安装

```
composer install
```

### Laravel &gt;= 5.5.x

[](#laravel--55x)

**1-** 发布配置文件

```
$ php artisan vendor:publish --provider="BowensH\LaravelGraphQLExtend\ServiceProvider"
```

**2-** 查看配置文件

```
config/graphql_extend.php

```

### Laravel &lt;= 5.4.x

[](#laravel--54x)

**1-** 向 `config/app.php` 文件添加服务提供者

```
BowensH\LaravelGraphQLExtend\ServiceProvider::class,
```

**2-** 发布配置文件

```
$ php artisan vendor:publish --provider="BowensH\LaravelGraphQLExtend\ServiceProvider"
```

**3-** 查看配置文件

```
config/graphql_extend.php

```

命令
--

[](#命令)

- [创建 Column 类型文件](#make-column)
- [创建 Type 类型文件](#make-type)

#### 创建 Column 类型文件

[](#创建-column-类型文件)

```
php artisan make:graphql:column TestColumn --table=test --force
```

如果某表字段为 json 类型，则该字段的 type 属性 命令默认生成为空数组（\[\]），需要自己手动写入对应键值，如果是数组，请用中括号包裹。例如

```
public function columns()
{
    return [
        'test' => [
            'name'  => [
                'type'        => Type::string(),
                'description' => '类型',
            ]
        ],
        'description' => '测试',
    ];
}
```

```
public function columns()
{
    return [
        'test' => [
            [//数组类型
                'name'  => [
                    'type'        => Type::string(),
                    'description' => '类型',
                ]
            ]
        ],
        'description' => '测试',
    ];
}
```

#### Type 类型文件生成命令

[](#type-类型文件生成命令)

在 folklore/graphql 包的原命令基础上，增加了 「--table=」 参数，可以根据表生成对应字段，当 Type 类型文件中的字段不需要复用时，可以使用这个命令快速根据表生成字段。

```
php artisan make:graphql:type TestType --table=test --force
```

用法
--

[](#用法)

- [配置](#config)
- [Column](#column)

### 配置

[](#配置)

- type\_map: 数据表字段类型与 GraphQL 的类型映射。

> doctrine/dbal 包自动识别 mysql 中的 TinyInt 类型为 Boolean，因此该类型对应 graphql 类型为 Type::boolean()。

### Column

[](#column)

- [make](#make)
- [append](#append)
- [only](#only)
- [except](#except)
- [nonNull](#nonNull)
- [result](#result)

> Column 类最后输出的结果，专用于 Type 类型的 fields 方法中。

#### make

[](#make)

根据 TestColumn 生成 Column 实例，第一个参数为通过 make:graphql:column 生成的 Column 类型文件，当该 Column 被用于包含 InputObject 属性的 Type 类型文件中，第二个参数则需为 true（默认 false）。只有当调用 Column::make 后才可以使用其他方法。

```
Column::make(TestColumn::class, true);
```

#### append

[](#append)

新增字段。

```
Column::make(TestColumn::class)
    ->append([
        'test' => [
            'type' => Type:int(),
            'description' => '描述'
        ]
    ]);
```

#### only

[](#only)

只保留 TestColumn 中的 id 字段。

```
//支持字符串与数组
Column::make(TestColumn::class, true)->only(['id'])
Column::make(TestColumn::class, true)->only('id')
```

#### except

[](#except)

排除 TestColumn 中的 id 字段。

```
//支持字符串与数组
Column::make(TestColumn::class, true)->except(['id'])
Column::make(TestColumn::class, true)->except('id')
```

#### nonNull

[](#nonnull)

将 TestColumn 中的 某些字段设置为 Type::nonNull。

```
//支持字符串与数组
Column::make(TestColumn::class, true)->nonNull(['title'])
Column::make(TestColumn::class, true)->nonNull('title')
```

#### result

[](#result)

输出最终结果。

```
