Skip to content
This repository was archived by the owner on Feb 14, 2023. It is now read-only.

Commit 97e3d44

Browse files
authored
Add option to download assets to serve them locally (#8)
Resolves #5
1 parent b2fb093 commit 97e3d44

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ By default, the playground is reachable at `/graphql-playground`
3434
It assumes a running GraphQL endpoint at `/graphql`. You can enter another URL in the
3535
UI or change the default setting in the configuration file.
3636

37+
### Local assets
38+
39+
If you want to serve the assets from your own server, you can download them with the command
40+
41+
php artisan graphql-playground:download-assets
42+
43+
This puts the necessary CSS, JS and Favicon into your `public` directory. If you have
44+
the assets downloaded, they will be used instead of the online version from the CDN.
45+
3746
## Security
3847

3948
If you do not want to enable the GraphQL playground in production, you can disable it in the config file.

src/DownloadAssetsCommand.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace MLL\GraphQLPlayground;
4+
5+
use Illuminate\Console\Command;
6+
7+
class DownloadAssetsCommand extends Command
8+
{
9+
const JS_PATH_LOCAL = 'vendor/graphql-playground/middleware.js';
10+
const JS_PATH_CDN = '//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/js/middleware.js';
11+
const CSS_PATH_LOCAL = 'vendor/graphql-playground/index.css';
12+
const CSS_PATH_CDN = '//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/css/index.css';
13+
const FAVICON_PATH_LOCAL = 'vendor/graphql-playground/favicon.png';
14+
const FAVICON_PATH_CDN = '//cdn.jsdelivr.net/npm/graphql-playground-react/build/favicon.png';
15+
16+
protected $signature = 'graphql-playground:download-assets';
17+
protected $description = 'Download the newest version of the GraphQLPlayground assets to serve them locally.';
18+
19+
public function handle()
20+
{
21+
$this->fileForceContents(
22+
public_path(self::CSS_PATH_LOCAL),
23+
file_get_contents('https:' . self::CSS_PATH_CDN)
24+
);
25+
$this->fileForceContents(
26+
public_path(self::JS_PATH_LOCAL),
27+
file_get_contents('https:' . self::JS_PATH_CDN)
28+
);
29+
$this->fileForceContents(
30+
public_path(self::FAVICON_PATH_LOCAL),
31+
file_get_contents('https:' . self::FAVICON_PATH_CDN)
32+
);
33+
}
34+
35+
protected function fileForceContents(string $path, string $contents)
36+
{
37+
$parts = explode('/', $path);
38+
$file = array_pop($parts);
39+
$path = '';
40+
foreach ($parts as $part) {
41+
if (!is_dir($path .= "/$part")) {
42+
mkdir($path);
43+
}
44+
}
45+
file_put_contents("$path/$file", $contents);
46+
}
47+
}

src/GraphQLPlaygroundServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,11 @@ public function boot()
4747
public function register()
4848
{
4949
$this->mergeConfigFrom(self::CONFIG_PATH, 'graphql-playground');
50+
51+
if ($this->app->runningInConsole()) {
52+
$this->commands([
53+
\MLL\GraphQLPlayground\DownloadAssetsCommand::class,
54+
]);
55+
}
5056
}
5157
}

views/index.blade.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,29 @@
66
<meta charset=utf-8 />
77
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
88
<title>GraphQL Playground</title>
9-
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/css/index.css" />
10-
<link rel="shortcut icon" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/favicon.png" />
11-
<script src="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/js/middleware.js"></script>
9+
10+
<link rel="stylesheet"
11+
href="{{
12+
file_exists(public_path(\MLL\GraphQLPlayground\DownloadAssetsCommand::CSS_PATH_LOCAL))
13+
? asset(\MLL\GraphQLPlayground\DownloadAssetsCommand::CSS_PATH_LOCAL)
14+
: \MLL\GraphQLPlayground\DownloadAssetsCommand::CSS_PATH_CDN
15+
}}"
16+
/>
17+
18+
<link rel="shortcut icon"
19+
href="{{
20+
file_exists(public_path(\MLL\GraphQLPlayground\DownloadAssetsCommand::FAVICON_PATH_LOCAL))
21+
? asset(\MLL\GraphQLPlayground\DownloadAssetsCommand::FAVICON_PATH_LOCAL)
22+
: \MLL\GraphQLPlayground\DownloadAssetsCommand::FAVICON_PATH_CDN
23+
}}"
24+
/>
25+
26+
<script src="{{
27+
file_exists(public_path(\MLL\GraphQLPlayground\DownloadAssetsCommand::JS_PATH_LOCAL))
28+
? asset(\MLL\GraphQLPlayground\DownloadAssetsCommand::JS_PATH_LOCAL)
29+
: \MLL\GraphQLPlayground\DownloadAssetsCommand::JS_PATH_CDN
30+
}}"
31+
></script>
1232

1333
</head>
1434

0 commit comments

Comments
 (0)