Skip to content

Commit 89ab920

Browse files
authored
Upgrade to GraphiQL 4 (#56)
1 parent d160569 commit 89ab920

File tree

6 files changed

+66
-146
lines changed

6 files changed

+66
-146
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ See [GitHub releases](https://github.com/mll-lab/laravel-graphiql/releases).
99

1010
## Unreleased
1111

12+
## v4.0.0
13+
14+
### Changed
15+
16+
- Upgrade to GraphiQL 4
17+
- Remove deprecated aliases for methods in `GraphiQLAsset` from `DownloadAssetsCommand`
18+
1219
## v3.3.3
1320

1421
### Fixed

UPGRADE.md

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,13 @@ This document provides guidance for upgrading between major versions.
66

77
### View
88

9-
The URLs for the GraphiQL assets are now provided by class `MLL\GraphiQL\GraphiQLAsset` instead of `MLL\GraphiQL\DownloadAssetsCommand`.
10-
If you have published the view, update it according to this diff:
11-
12-
```diff
13-
@@ -1,6 +1,8 @@
14-
<!DOCTYPE html>
15-
<html lang="en">
16-
-@php use MLL\GraphiQL\DownloadAssetsCommand; @endphp
17-
+@php
18-
+use MLL\GraphiQL\GraphiQLAsset;
19-
+@endphp
20-
<head>
21-
<meta charset=utf-8/>
22-
<meta name="viewport"
23-
@@ -45,17 +47,17 @@
24-
margin-left: var(--px-12);
25-
}
26-
</style>
27-
- <script src="{{ DownloadAssetsCommand::reactPath() }}"></script>
28-
- <script src="{{ DownloadAssetsCommand::reactDOMPath() }}"></script>
29-
- <link rel="stylesheet" href="{{ DownloadAssetsCommand::cssPath() }}"/>
30-
- <link rel="shortcut icon" href="{{ DownloadAssetsCommand::faviconPath() }}"/>
31-
+ <script src="{{ GraphiQLAsset::reactJS() }}"></script>
32-
+ <script src="{{ GraphiQLAsset::reactDOMJS() }}"></script>
33-
+ <link rel="stylesheet" href="{{ GraphiQLAsset::graphiQLCSS() }}"/>
34-
+ <link rel="shortcut icon" href="{{ GraphiQLAsset::favicon() }}"/>
35-
</head>
36-
37-
<body>
38-
39-
<div id="graphiql">Loading...</div>
40-
-<script src="{{ DownloadAssetsCommand::jsPath() }}"></script>
41-
-<script src="{{ DownloadAssetsCommand::pluginExplorerPath() }}"></script>
42-
+<script src="{{ GraphiQLAsset::graphiQLJS() }}"></script>
43-
+<script src="{{ GraphiQLAsset::pluginExplorerJS() }}"></script>
44-
<script>
45-
const fetcher = GraphiQL.createFetcher({
46-
url: '{{ $url }}',
47-
```
9+
If you have published and/or customized the view, you need to republish or update it.
10+
See [views/index.blade.php](views/index.blade.php) for the latest version.
11+
12+
Major changes:
13+
14+
- The URLs for the GraphiQL assets are now provided by class `MLL\GraphiQL\GraphiQLAsset` instead of `MLL\GraphiQL\DownloadAssetsCommand`.
15+
- The setup now uses GraphiQL version 4.
4816

4917
## v1 to v2
5018

src/DownloadAssetsCommand.php

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ class DownloadAssetsCommand extends Command
1212

1313
public function handle(): void
1414
{
15-
$this->downloadFileFromCDN(GraphiQLAsset::REACT_JS_LOCAL_PATH, GraphiQLAsset::REACT_JS_SOURCE_URL);
16-
$this->downloadFileFromCDN(GraphiQLAsset::REACT_DOM_JS_LOCAL_PATH, GraphiQLAsset::REACT_DOM_JS_SOURCE_URL);
17-
$this->downloadFileFromCDN(GraphiQLAsset::GRAPHIQL_CSS_LOCAL_PATH, GraphiQLAsset::GRAPHIQL_CSS_SOURCE_URL);
18-
$this->downloadFileFromCDN(GraphiQLAsset::GRAPHIQL_JS_LOCAL_PATH, GraphiQLAsset::GRAPHIQL_JS_SOURCE_URL);
19-
$this->downloadFileFromCDN(GraphiQLAsset::PLUGIN_EXPLORER_JS_LOCAL_PATH, GraphiQLAsset::PLUGIN_EXPLORER_JS_SOURCE_URL);
20-
$this->downloadFileFromCDN(GraphiQLAsset::FAVICON_LOCAL_PATH, GraphiQLAsset::FAVICON_SOURCE_URL);
15+
$this->downloadFile(GraphiQLAsset::REACT_JS_LOCAL_PATH, GraphiQLAsset::REACT_JS_SOURCE_URL);
16+
$this->downloadFile(GraphiQLAsset::REACT_DOM_JS_LOCAL_PATH, GraphiQLAsset::REACT_DOM_JS_SOURCE_URL);
17+
$this->downloadFile(GraphiQLAsset::GRAPHIQL_CSS_LOCAL_PATH, GraphiQLAsset::GRAPHIQL_CSS_SOURCE_URL);
18+
$this->downloadFile(GraphiQLAsset::PLUGIN_EXPLORER_CSS_LOCAL_PATH, GraphiQLAsset::PLUGIN_EXPLORER_CSS_SOURCE_URL);
19+
$this->downloadFile(GraphiQLAsset::GRAPHIQL_JS_LOCAL_PATH, GraphiQLAsset::GRAPHIQL_JS_SOURCE_URL);
20+
$this->downloadFile(GraphiQLAsset::PLUGIN_EXPLORER_JS_LOCAL_PATH, GraphiQLAsset::PLUGIN_EXPLORER_JS_SOURCE_URL);
21+
$this->downloadFile(GraphiQLAsset::FAVICON_LOCAL_PATH, GraphiQLAsset::FAVICON_SOURCE_URL);
2122
}
2223

23-
protected function downloadFileFromCDN(string $localPath, string $cdnPath): void
24+
protected function downloadFile(string $localPath, string $sourceURL): void
2425
{
2526
$publicPath = GraphiQLAsset::publicPath($localPath);
2627

@@ -30,48 +31,12 @@ protected function downloadFileFromCDN(string $localPath, string $cdnPath): void
3031
mkdir($directory, 0777, true);
3132
}
3233

33-
$contents = file_get_contents($cdnPath);
34+
$contents = file_get_contents($sourceURL);
3435
if ($contents === false) {
3536
$error = error_get_last();
3637
throw new \ErrorException($error['message'] ?? 'An error occurred', 0, $error['type'] ?? 1);
3738
}
3839

3940
file_put_contents($publicPath, $contents);
4041
}
41-
42-
/** @deprecated use GraphiQLAsset::reactJS, this alias will be removed in the next major version */
43-
public static function reactPath(): string
44-
{
45-
return GraphiQLAsset::reactJS();
46-
}
47-
48-
/** @deprecated use GraphiQLAsset::reactDOMJS, this alias will be removed in the next major version */
49-
public static function reactDOMPath(): string
50-
{
51-
return GraphiQLAsset::reactDOMJS();
52-
}
53-
54-
/** @deprecated use GraphiQLAsset::graphiQLJS, this alias will be removed in the next major version */
55-
public static function jsPath(): string
56-
{
57-
return GraphiQLAsset::graphiQLJS();
58-
}
59-
60-
/** @deprecated use GraphiQLAsset::pluginExplorerJS, this alias will be removed in the next major version */
61-
public static function pluginExplorerPath(): string
62-
{
63-
return GraphiQLAsset::pluginExplorerJS();
64-
}
65-
66-
/** @deprecated use GraphiQLAsset::graphiQLCSS, this alias will be removed in the next major version */
67-
public static function cssPath(): string
68-
{
69-
return GraphiQLAsset::graphiQLCSS();
70-
}
71-
72-
/** @deprecated use GraphiQLAsset::favicon, this alias will be removed in the next major version */
73-
public static function faviconPath(): string
74-
{
75-
return GraphiQLAsset::favicon();
76-
}
7742
}

src/GraphiQLAsset.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@ class GraphiQLAsset
1515
public const REACT_DOM_JS_LOCAL_PATH = 'vendor/graphiql/react-dom.production.min.js';
1616
public const REACT_DOM_JS_SOURCE_URL = 'https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js';
1717

18-
public const GRAPHIQL_CSS_LOCAL_PATH = 'vendor/graphiql/graphiql.min.css';
19-
public const GRAPHIQL_CSS_SOURCE_URL = 'https://cdn.jsdelivr.net/npm/graphiql/graphiql.min.css';
18+
public const GRAPHIQL_CSS_LOCAL_PATH = 'vendor/graphiql/graphiql.css';
19+
public const GRAPHIQL_CSS_SOURCE_URL = 'https://cdn.jsdelivr.net/npm/graphiql@4/dist/style.css';
20+
21+
public const PLUGIN_EXPLORER_CSS_LOCAL_PATH = 'vendor/graphiql/graphiql-plugin-explorer.css';
22+
public const PLUGIN_EXPLORER_CSS_SOURCE_URL = 'https://cdn.jsdelivr.net/npm/@graphiql/plugin-explorer@4/dist/style.css';
2023

2124
public const FAVICON_LOCAL_PATH = 'vendor/graphiql/favicon.ico';
2225
public const FAVICON_SOURCE_URL = 'https://raw.githubusercontent.com/graphql/graphql.github.io/source/public/favicon.ico';
2326

24-
public const GRAPHIQL_JS_LOCAL_PATH = 'vendor/graphiql/graphiql.min.js';
25-
public const GRAPHIQL_JS_SOURCE_URL = 'https://cdn.jsdelivr.net/npm/graphiql/graphiql.min.js';
27+
public const GRAPHIQL_JS_LOCAL_PATH = 'vendor/graphiql/graphiql.js';
28+
public const GRAPHIQL_JS_SOURCE_URL = 'https://cdn.jsdelivr.net/npm/graphiql@4/dist/index.umd.js';
2629

27-
public const PLUGIN_EXPLORER_JS_LOCAL_PATH = 'vendor/graphiql/graphiql-plugin-explorer.umd.js';
28-
/** Pinned because the latest version broke, see https://github.com/mll-lab/laravel-graphiql/issues/25. */
29-
public const PLUGIN_EXPLORER_JS_SOURCE_URL = 'https://cdn.jsdelivr.net/npm/@graphiql/[email protected]/dist/index.umd.js';
30+
public const PLUGIN_EXPLORER_JS_LOCAL_PATH = 'vendor/graphiql/graphiql-plugin-explorer.js';
31+
public const PLUGIN_EXPLORER_JS_SOURCE_URL = 'https://cdn.jsdelivr.net/npm/@graphiql/plugin-explorer@4/dist/index.umd.js';
3032

3133
public static function reactJS(): string
3234
{
@@ -43,6 +45,11 @@ public static function graphiQLCSS(): string
4345
return self::availableURL(self::GRAPHIQL_CSS_LOCAL_PATH, self::GRAPHIQL_CSS_SOURCE_URL);
4446
}
4547

48+
public static function pluginExplorerCSS(): string
49+
{
50+
return self::availableURL(self::PLUGIN_EXPLORER_CSS_LOCAL_PATH, self::PLUGIN_EXPLORER_CSS_SOURCE_URL);
51+
}
52+
4653
public static function favicon(): string
4754
{
4855
return self::availableURL(self::FAVICON_LOCAL_PATH, self::FAVICON_SOURCE_URL);
@@ -73,10 +80,10 @@ public static function localURL(string $path): string
7380
return $url->asset($path);
7481
}
7582

76-
public static function availableURL(string $local, string $cdn): string
83+
public static function availableURL(string $local, string $sourceURL): string
7784
{
7885
return file_exists(self::publicPath($local))
7986
? self::localURL($local)
80-
: $cdn;
87+
: $sourceURL;
8188
}
8289
}

tests/DownloadAssetsCommandTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ protected function setUp(): void
1010
{
1111
parent::setUp();
1212

13-
foreach (self::paths() as [$localPath, $cdnPath]) {
13+
foreach (self::localPathToSourceURL() as [$localPath, $cdnPath]) {
1414
$publicPath = GraphiQLAsset::publicPath($localPath);
1515
if (file_exists($publicPath)) {
1616
unlink($publicPath);
@@ -20,26 +20,27 @@ protected function setUp(): void
2020

2121
public function testSuccessfulDownload(): void
2222
{
23-
foreach (self::paths() as [$localPath, $cdnPath]) {
23+
foreach (self::localPathToSourceURL() as [$localPath, $sourceURL]) {
2424
$this->assertFileDoesNotExist(GraphiQLAsset::publicPath($localPath));
25-
$this->assertSame($cdnPath, GraphiQLAsset::availableURL($localPath, $cdnPath));
25+
$this->assertSame($sourceURL, GraphiQLAsset::availableURL($localPath, $sourceURL));
2626
}
2727

2828
$this->artisan('graphiql:download-assets')
2929
->assertOk();
3030

31-
foreach (self::paths() as [$localPath, $cdnPath]) {
31+
foreach (self::localPathToSourceURL() as [$localPath, $sourceURL]) {
3232
$this->assertFileExists(GraphiQLAsset::publicPath($localPath));
33-
$this->assertSame(GraphiQLAsset::localURL($localPath), GraphiQLAsset::availableURL($localPath, $cdnPath));
33+
$this->assertSame(GraphiQLAsset::localURL($localPath), GraphiQLAsset::availableURL($localPath, $sourceURL));
3434
}
3535
}
3636

3737
/** @return iterable<array{string, string}> */
38-
private static function paths(): iterable
38+
private static function localPathToSourceURL(): iterable
3939
{
4040
yield [GraphiQLAsset::REACT_JS_LOCAL_PATH, GraphiQLAsset::REACT_JS_SOURCE_URL];
4141
yield [GraphiQLAsset::REACT_DOM_JS_LOCAL_PATH, GraphiQLAsset::REACT_DOM_JS_SOURCE_URL];
4242
yield [GraphiQLAsset::GRAPHIQL_CSS_LOCAL_PATH, GraphiQLAsset::GRAPHIQL_CSS_SOURCE_URL];
43+
yield [GraphiQLAsset::PLUGIN_EXPLORER_CSS_LOCAL_PATH, GraphiQLAsset::PLUGIN_EXPLORER_CSS_SOURCE_URL];
4344
yield [GraphiQLAsset::FAVICON_LOCAL_PATH, GraphiQLAsset::FAVICON_SOURCE_URL];
4445
yield [GraphiQLAsset::GRAPHIQL_JS_LOCAL_PATH, GraphiQLAsset::GRAPHIQL_JS_SOURCE_URL];
4546
yield [GraphiQLAsset::PLUGIN_EXPLORER_JS_LOCAL_PATH, GraphiQLAsset::PLUGIN_EXPLORER_JS_SOURCE_URL];

views/index.blade.php

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,54 @@
1-
<!DOCTYPE html>
2-
<html lang="en">
1+
{{-- See https://github.com/graphql/graphiql/blob/main/examples/graphiql-cdn/index.html. --}}
32
@php
43
use MLL\GraphiQL\GraphiQLAsset;
54
@endphp
5+
<!DOCTYPE html>
6+
<html lang="en">
67
<head>
7-
<meta charset=utf-8/>
8-
<meta name="viewport"
9-
content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
8+
<meta charset="UTF-8" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
1010
<title>GraphiQL</title>
1111
<style>
1212
body {
13-
height: 100%;
1413
margin: 0;
15-
width: 100%;
16-
overflow: hidden;
14+
overflow: hidden; /* in Firefox */
1715
}
1816
1917
#graphiql {
20-
height: 100vh;
21-
}
22-
23-
/* Make the explorer feel more integrated */
24-
.docExplorerWrap {
25-
overflow: auto !important;
26-
width: 100% !important;
27-
height: auto !important;
28-
}
29-
30-
.doc-explorer-title-bar {
31-
font-weight: var(--font-weight-medium);
32-
font-size: var(--font-size-h2);
33-
overflow-x: hidden;
34-
text-overflow: ellipsis;
35-
white-space: nowrap;
36-
}
37-
38-
.doc-explorer-rhs {
39-
display: none;
40-
}
41-
42-
.doc-explorer-contents {
43-
margin: var(--px-16) 0 0;
18+
height: 100dvh;
4419
}
4520
46-
.graphiql-explorer-actions select {
47-
margin-left: var(--px-12);
21+
#graphiql-loading {
22+
height: 100%;
23+
display: flex;
24+
align-items: center;
25+
justify-content: center;
26+
font-size: 4rem;
4827
}
4928
</style>
5029
<script src="{{ GraphiQLAsset::reactJS() }}"></script>
5130
<script src="{{ GraphiQLAsset::reactDOMJS() }}"></script>
5231
<link rel="stylesheet" href="{{ GraphiQLAsset::graphiQLCSS() }}"/>
32+
<link rel="stylesheet" href="{{ GraphiQLAsset::pluginExplorerCSS() }}"/>
5333
<link rel="shortcut icon" href="{{ GraphiQLAsset::favicon() }}"/>
5434
</head>
5535

5636
<body>
5737

58-
<div id="graphiql">Loading...</div>
38+
<div id="graphiql">
39+
<div id="graphiql-loading">Loading…</div>
40+
</div>
41+
5942
<script src="{{ GraphiQLAsset::graphiQLJS() }}"></script>
6043
<script src="{{ GraphiQLAsset::pluginExplorerJS() }}"></script>
6144
<script>
62-
const fetcher = GraphiQL.createFetcher({
63-
url: '{{ $url }}',
64-
subscriptionUrl: '{{ $subscriptionUrl }}',
65-
});
45+
const explorer = GraphiQLPluginExplorer.explorerPlugin();
6646
6747
function GraphiQLWithExplorer() {
68-
const [query, setQuery] = React.useState('');
69-
7048
return React.createElement(GraphiQL, {
7149
fetcher,
72-
query,
73-
onEditQuery: setQuery,
74-
defaultEditorToolsVisibility: true,
7550
plugins: [
76-
GraphiQLPluginExplorer.useExplorerPlugin({
77-
query,
78-
onEdit: setQuery,
79-
}),
51+
explorer,
8052
],
8153
// See https://github.com/graphql/graphiql/tree/main/packages/graphiql#props for available settings
8254
});

0 commit comments

Comments
 (0)