mll-lab / laravel-graphql-playground
Requires
- php: ^7.1 || ^8
- illuminate/console: 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6 || ^7 || ^8 || ^9
- illuminate/contracts: 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6 || ^7 || ^8 || ^9
- illuminate/support: 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6 || ^7 || ^8 || ^9
Requires (Dev)
- laravel/lumen-framework: 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6 || ^7 || ^8 || ^9
- localheinz/composer-normalize: ^1.3
README
已弃用
该项目已被弃用,推荐使用https://github.com/mll-lab/laravel-graphiql。
轻松将GraphQL Playground集成到您的Laravel项目中。
请注意:这并非一个GraphQL服务器实现,而仅是用于测试和探索您模式的UI。对于服务器组件,我们推荐使用nuwave/lighthouse。
安装
composer require mll-lab/laravel-graphql-playground
如果您正在使用Lumen,请在bootstrap/app.php
中注册服务提供者
$app->register(MLL\GraphQLPlayground\GraphQLPlaygroundServiceProvider::class);
配置
默认情况下,playground可通过/graphql-playground
访问,并假定存在一个在/graphql
上运行的GraphQL端点。
要更改默认设置,使用以下命令发布配置
php artisan vendor:publish --tag=graphql-playground-config
您将在config/graphql-playground.php
中找到配置文件。
Lumen
如果您正在使用Lumen,请手动将其复制到该位置,并在您的boostrap/app.php
中加载配置
$app->configure('graphql-playground');
在代理后面的HTTPS
如果您的应用程序位于解析https的代理后面,端点的生成URL可能不会使用https://
,这可能导致Playground默认无法工作。为了解决这个问题,请配置TrustProxies
中间件以包含\Symfony\Component\HttpFoundation\Request::HEADER_X_FORWARDED_FOR
在$headers
中。
自定义
要进一步自定义Playground,发布视图
php artisan vendor:publish --tag=graphql-playground-view
您可以用它进行各种自定义。
更改playground实例的设置
在发布视图中调用GraphQLPlayground.init
时添加额外设置
GraphQLPlayground.init(document.getElementById('root'), { endpoint: "{{ url(config('graphql-playground.endpoint')) }}", subscriptionEndpoint: "{{ config('graphql-playground.subscriptionEndpoint') }}", // See https://github.com/graphql/graphql-playground#properties for available settings })
配置基于会话的身份验证
可以使用Laravel Sanctum配合基于会话的身份验证使用。如果您通过会话和CSRF使用GraphQL,请将以下内容添加到发布视图中的<head>
<meta name="csrf-token" content="{{ csrf_token() }}">
修改Playground配置
GraphQLPlayground.init(document.getElementById('root'), { endpoint: "{{ url(config('graphql-playground.endpoint')) }}", subscriptionEndpoint: "{{ config('graphql-playground.subscriptionEndpoint') }}", + settings: { + 'request.credentials': 'same-origin', + 'request.globalHeaders': { + 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content + } + } })
确保您的路由在config/graphql-playground.php
中包含web
中间件组
'route' => [
'uri' => '/graphql-playground',
'name' => 'graphql-playground',
+ 'middleware' => ['web']
]
本地资源
如果您想从自己的服务器提供资源,可以使用以下命令下载
php artisan graphql-playground:download-assets
这会将必要的CSS、JS和Favicon文件放入您的public
目录中。如果您已下载资源,将使用本地资源而非CDN上的在线版本。
安全
如果您不希望在生产环境中启用GraphQL playground,您可以在配置文件中禁用它。最简单的方法是设置环境变量GRAPHQL_PLAYGROUND_ENABLED=false
。
如果您想要保护GraphQL playground的访问路由,您可以在配置文件中添加自定义中间件。