Skip to content

Commit abe85c6

Browse files
committed
Replace GraphiQL with GraphQL Playground
1 parent f3f57e6 commit abe85c6

File tree

6 files changed

+93
-2297
lines changed

6 files changed

+93
-2297
lines changed

src/Our.Umbraco.GraphQL/CompositionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static void RegisterGraphQLServices(this Composition composition)
1919
composition.Register<IDocumentExecuter, DocumentExecuter>(Lifetime.Singleton);
2020
composition.Register<IDocumentWriter, DocumentWriter>(Lifetime.Singleton);
2121

22+
composition.Register<GraphQLPlaygroundMiddleware>(Lifetime.Singleton);
2223
composition.Register<GraphQLRequestParserMiddleware>(Lifetime.Singleton);
2324
composition.Register<GraphQLMiddleware>(Lifetime.Singleton);
2425

src/Our.Umbraco.GraphQL/Resources/graphiql.html

Lines changed: 0 additions & 145 deletions
This file was deleted.

src/Our.Umbraco.GraphQL/Resources/graphiql.js

Lines changed: 0 additions & 2132 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset=utf-8/>
6+
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
7+
<title>GraphQL Playground</title>
8+
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/css/index.css" />
9+
<link rel="shortcut icon" href="//cdn.jsdelivr.net/npm/graphql-playground-react/build/favicon.png" />
10+
<script src="//cdn.jsdelivr.net/npm/graphql-playground-react/build/static/js/middleware.js"></script>
11+
</head>
12+
13+
<body>
14+
<div id="root">
15+
<style>
16+
body {
17+
background-color: rgb(23, 42, 58);
18+
font-family: Open Sans, sans-serif;
19+
height: 90vh;
20+
}
21+
#root {
22+
height: 100%;
23+
width: 100%;
24+
display: flex;
25+
align-items: center;
26+
justify-content: center;
27+
}
28+
.loading {
29+
font-size: 32px;
30+
font-weight: 200;
31+
color: rgba(255, 255, 255, .6);
32+
margin-left: 20px;
33+
}
34+
img {
35+
width: 78px;
36+
height: 78px;
37+
}
38+
.title {
39+
font-weight: 400;
40+
}
41+
</style>
42+
<img src='//cdn.jsdelivr.net/npm/graphql-playground-react/build/logo.png' alt=''>
43+
<div class="loading"> Loading
44+
<span class="title">GraphQL Playground</span>
45+
</div>
46+
</div>
47+
<script>window.addEventListener('load', function (event) {
48+
GraphQLPlayground.init(document.getElementById('root'), {
49+
// options as 'endpoint' belong here
50+
})
51+
})</script>
52+
</body>
53+
54+
</html>

src/Our.Umbraco.GraphQL/Web/AppBuilderExtensions.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,18 @@ public static IAppBuilder UseUmbracoGraphQL(this IAppBuilder app, string rootPat
1818

1919
public static IAppBuilder UseUmbracoGraphQL(this IAppBuilder app, string rootPath, IFactory factory, GraphQLServerOptions options)
2020
{
21-
string graphiQLPath = $"/{rootPath}/graphiql";
2221
string graphQLPath = $"/{rootPath}/graphql";
2322

24-
return app.Map(graphiQLPath, subApp =>
25-
{
26-
string html;
27-
28-
using (var stream = typeof(AppBuilderExtensions).Assembly.GetManifestResourceStream("Our.Umbraco.GraphQL.Resources.graphiql.html"))
29-
{
30-
using (var reader = new StreamReader(stream))
31-
{
32-
html = reader.ReadToEnd().Replace("${endpointURL}", graphQLPath);
33-
}
34-
}
35-
subApp.Run(async ctx =>
36-
{
37-
ctx.Response.ContentType = "text/html";
38-
await ctx.Response.WriteAsync(html);
39-
});
40-
})
41-
.Map(graphQLPath, subApp =>
23+
return app.Map(graphQLPath, subApp =>
4224
{
4325
var corsOptions = new CorsOptions
4426
{
4527
PolicyProvider = options.CorsPolicyProvder
4628
};
4729

48-
4930
subApp.UseCors(corsOptions)
5031
.Use<FactoryMiddleware>(factory)
32+
.Use((ctx, next) => ctx.Get<IFactory>("umbraco:factory").GetInstance<GraphQLPlaygroundMiddleware>().Invoke(ctx, next))
5133
.Use((ctx, next) => ctx.Get<IFactory>("umbraco:factory").GetInstance<GraphQLRequestParserMiddleware>().Invoke(ctx, next))
5234
.Use((ctx, next) => ctx.Get<IFactory>("umbraco:factory").GetInstance<GraphQLMiddleware>().Invoke(ctx, options));
5335
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.Owin;
2+
using System;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
6+
namespace Our.Umbraco.GraphQL.Web.Middleware
7+
{
8+
internal class GraphQLPlaygroundMiddleware
9+
{
10+
private readonly string _html;
11+
12+
public GraphQLPlaygroundMiddleware()
13+
{
14+
using (var stream = typeof(AppBuilderExtensions).Assembly.GetManifestResourceStream("Our.Umbraco.GraphQL.Resources.playground.html"))
15+
{
16+
using (var reader = new StreamReader(stream))
17+
{
18+
_html = reader.ReadToEnd(); //.Replace("${endpointURL}", graphQLPath);
19+
}
20+
}
21+
}
22+
23+
public async Task Invoke(IOwinContext context, Func<Task> next)
24+
{
25+
if (context.Request.Method == "GET")
26+
{
27+
context.Response.ContentType = "text/html";
28+
await context.Response.WriteAsync(_html);
29+
}
30+
else
31+
{
32+
await next();
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)