Skip to content

Commit

Permalink
Merge pull request #23 from JosephWoodward/ChangePath
Browse files Browse the repository at this point in the history
Add ability to set the graphql path
  • Loading branch information
josephwoodward authored Aug 20, 2018
2 parents 979199d + ca285b0 commit 571732a
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 43 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
}
```

After that simply navigate to `/graphql` in your browser to start using GraphiQL.
After that simply navigate to `/graphql` in your browser to start using GraphiQL.

## Configuration

By default GraphiQL lives on the aforementioned `/graphql` endpoint, however it can be changed by passing your chosen path to the `app.UseGraphiQl();` entry point method:

```csharp
app.UseGraphiQl('/whatever/graphiql');
```
9 changes: 5 additions & 4 deletions src/graphiql.example/Controllers/GraphQlController.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Threading.Tasks;
using graphiql.example.GraphQl;
using graphiql.example.GraphQl.Models;
using GraphiQl.example.GraphQl;
using GraphiQl.example.GraphQl.Models;
using GraphQL;
using GraphQL.Types;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;

namespace graphiql.example.Controllers
namespace GraphiQl.example.Controllers
{
[Route("graphql")]
[Route(Startup.GraphQlPath)]
public class GraphQlController : Controller
{
[HttpPost]
Expand Down
2 changes: 1 addition & 1 deletion src/graphiql.example/GraphQl/Droid.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace graphiql.example.GraphQl
namespace GraphiQl.example.GraphQl
{
public class Droid
{
Expand Down
2 changes: 1 addition & 1 deletion src/graphiql.example/GraphQl/GraphQlQuery.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GraphQL;

namespace graphiql.example.GraphQl
namespace GraphiQl.example.GraphQl
{
public class GraphQlQuery
{
Expand Down
2 changes: 1 addition & 1 deletion src/graphiql.example/GraphQl/Models/DroidType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GraphQL.Types;

namespace graphiql.example.GraphQl.Models
namespace GraphiQl.example.GraphQl.Models
{
public class DroidType : ObjectGraphType<Droid>
{
Expand Down
2 changes: 1 addition & 1 deletion src/graphiql.example/GraphQl/Models/StarWarsQuery.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GraphQL.Types;

namespace graphiql.example.GraphQl.Models
namespace GraphiQl.example.GraphQl.Models
{
public class StarWarsQuery : ObjectGraphType
{
Expand Down
11 changes: 2 additions & 9 deletions src/graphiql.example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace graphiql.example
namespace GraphiQl.example
{
public class Program
{
Expand Down
13 changes: 5 additions & 8 deletions src/graphiql.example/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace graphiql.example
namespace GraphiQl.example
{
public class Startup
{
public const string GraphQlPath = "/graphql";

public Startup(IConfiguration configuration)
{
Configuration = configuration;
Expand All @@ -29,7 +26,7 @@ public void ConfigureServices(IServiceCollection services)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseGraphiQl();
app.UseGraphiQl(GraphQlPath);
app.UseMvc();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/graphiql.example/graphiql.example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<UserSecretsId>aspnet-graphiql.example-D0705FC5-6A16-43BB-AE45-3C609BE0FE6A</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot" />
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/graphiql.tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Xunit;

namespace graphiql.tests
namespace GraphiQl.tests
{
public class UnitTest1
{
Expand Down
2 changes: 1 addition & 1 deletion src/graphiql/GraphiQlConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Net;

namespace graphiql
namespace GraphiQl
{
public class GraphiQlConfig
{
Expand Down
53 changes: 39 additions & 14 deletions src/graphiql/GraphiQlExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,76 @@
using System;
using System.Reflection;
using graphiql;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;

namespace Microsoft.AspNetCore.Builder
namespace GraphiQl
{
public static class GraphiQlExtensions
{
private const string DefaultPath = "/graphql";

public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app)
{
return UseGraphiQl(app, "/graphql");
}
=> UseGraphiQl(app, DefaultPath);

public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app, string path)
=> UseGraphiQlIml(app, path);

private static IApplicationBuilder UseGraphiQlIml(this IApplicationBuilder app, string path)
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentException(nameof(path));

path = path.StartsWith("/") ? path : "/" + path;
var p = path.EndsWith("/") ? path : $"{path}/" + "graphql-path.js";
app.Map(p, x => WritePathJavaScript(x, path));

return UseGraphiQlImp(app, x => x.SetPath(path));
}

private static IApplicationBuilder UseGraphiQlImp(this IApplicationBuilder app, Action<GraphiQlConfig> setConfig)
private static IApplicationBuilder UseGraphiQlImp(this IApplicationBuilder app,
Action<GraphiQlConfig> setConfig)
{
if (app == null)
throw new ArgumentNullException(nameof(app));
if (setConfig == null)
throw new ArgumentNullException(nameof(setConfig));

var config = new GraphiQlConfig();
setConfig(config);

var assembly = typeof(Microsoft.AspNetCore.Builder.GraphiQlExtensions).GetTypeInfo().Assembly;
string[] names = assembly.GetManifestResourceNames();

var fileServerOptions = new FileServerOptions
{
RequestPath = config.Path,
FileProvider = new EmbeddedFileProvider(assembly, "graphiql.assets"),
EnableDefaultFiles = true
FileProvider = BuildFileProvider(),
EnableDefaultFiles = true,
StaticFileOptions = {ContentTypeProvider = new FileExtensionContentTypeProvider()}
};

fileServerOptions.StaticFileOptions.ContentTypeProvider = new FileExtensionContentTypeProvider();
app.UseFileServer(fileServerOptions);

return app;
}

private static IFileProvider BuildFileProvider()
{
var assembly = typeof(GraphiQlExtensions).GetTypeInfo().Assembly;
var embeddedFileProvider = new EmbeddedFileProvider(assembly, "graphiql.assets");

var fileProvider = new CompositeFileProvider(
embeddedFileProvider
);

return fileProvider;
}

private static void WritePathJavaScript(IApplicationBuilder app, string path)
{
app.Run(h =>
{
h.Response.ContentType = "application/javascript";
return h.Response.WriteAsync($"var graphqlPath='{path}';");
});
}
}
}
3 changes: 2 additions & 1 deletion src/graphiql/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
-->
<link rel="stylesheet" href="./graphiql.css" />
<script src="./graphiql.js"></script>
<script src="./graphql-path.js"></script>

</head>
<body>
Expand Down Expand Up @@ -109,7 +110,7 @@
function graphQLFetcher(graphQLParams) {
// This example expects a GraphQL server at the path /graphql.
// Change this to point wherever you host your GraphQL server.
return fetch('/graphql', {
return fetch(graphqlPath, {
method: 'post',
headers: {
'Accept': 'application/json',
Expand Down
1 change: 1 addition & 0 deletions src/graphiql/graphiql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.1" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Composite" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="./assets/**/*.*" />
Expand Down

0 comments on commit 571732a

Please sign in to comment.