ggarri / graphql-api
数据库的只读 Graphql API。
Requires
- php: >=5.3.9
- doctrine/dbal: ^2.5
- doctrine/doctrine-bundle: ~1.4
- doctrine/orm: ^2.4.8
- incenteev/composer-parameter-handler: ~2.0
- opsway/doctrine-dbal-postgresql: ~0.1
- sensio/distribution-bundle: ~5.0
- sensio/framework-extra-bundle: ^3.0.2
- symfony/monolog-bundle: ~2.4
- symfony/swiftmailer-bundle: ~2.3
- symfony/symfony: ^2.8
- webonyx/graphql-php: ^0.6.4
Requires (Dev)
- sensio/generator-bundle: ~3.0
- symfony/phpunit-bridge: ~2.7
This package is not auto-updated.
Last update: 2024-09-18 18:47:59 UTC
README
描述
这个库提供了一个简单且单一的入口,用于使用 Graphql 语法读取所有数据库。为了实现这一点,我们需要将数据库模型导出为 PHP 实体,这些实体将由 doctrine 注解表示。利用这些 doctrine 注解,这个库将生成 GraphQL 模式,以便使用 Graphql 查询语法进行访问。
设置
将仓库包含在 composer.json
中
"repositories": [
....
{
"type": "git",
"url": "https://github.com/ggarri/symfony-doctrine-graphql"
},
....
"require": {
"ggarri/symfony-graphql-api": "^0.6-dev"
}
....
安装依赖
composer install/update
加载包
# AppKernel.php
use GraphqlApiBundle\GraphqlApiBundle;
...
public function registerBundles()
{
$bundles = array(
....
new GraphqlApiBundle(),
....
路由
在 'app/config/routing.yml' 中包含 graphql 入口
# Adding graphql routes (127.0.0.1:8000/graphql/api?query={graphql_query}
graphql_routing:
resource: .
type: extra
设置数据库连接
如果您尚未设置,则需要设置 doctrine 数据库连接。此库仅需要一个有效的 doctrine 连接,如上面所述
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
default_entity_manager: default
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
导入您的 DB 模型
一切设置完成后,您可以将数据库转换为 PHP 实体类。为此,正如上面所述,您的数据库模型将从 Doctrine.dbal.default 下定义的连接导入。
上述命令将为您完成所有工作。
php app/console graphql-api:graphql:generate-schema --namespace "AppBundle\\Entity" annotation ./src/AppBundle/Entity --from-database --force
() 我们使用 "AppBundle\\Entity"
作为要导入的模型的命名空间,并将 ./src/AppBundle/Entity
作为放置每个实体文件的文件夹位置。 () 可能出现的问题如上所示
使用
就是这样,现在是时候开始使用您的新定制的 graphql api 读取数据库了。
(我们使用 127.0.0.1:8000
作为服务器,您可能有不同的名称)
Curl 示例
curl -XPOST -H 'Content-Type:application/graphql' -d 'query={ boardtype(id: 2) { id, name, vatClass { amount } } }' http://127.0.0.1:8000/graphql/api
浏览器示例
http://127.0.0.1:8000/graphql/api?query={boardtype(id:2){id,name,vatClass{amount}}}
如果您想了解更多关于 Graphql 语法的信息,请访问: https://graphql.net.cn/learn/
故障排除
使用 POSTGRES 9.5
如果您的数据库是 Postgres 并且包含 Blob
或 jsonb
类型,您可能需要修补 postgres 驱动程序,因为最新版本的驱动程序中没有包含 Blob
或 jsonb
。
修补 Postgres 驱动程序以包含新类型
cp -r vendor/ggarri/graphql-api/src/Doctrine src/
patch vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php vendor/ggarri/graphql-api/src/Doctrine/patch/AbstractPostgreSQLDriver.patch
(*) 请确保正确应用了修补程序,因为有些类型它没有修补。在大约第 110 行,必须具有以下行
switch(true) {
case version_compare($version, '9.4', '>='):
return new PostgreSQL95Platform(); // THIS IS THE IMPORTANT ONE
case version_compare($version, '9.2', '>='):
return new PostgreSQL92Platform();
case version_compare($version, '9.1', '>='):
return new PostgreSQL91Platform();
default:
return new PostgreSqlPlatform();
}