Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure graphql path #15

Merged
merged 1 commit into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/graphiql.example/Controllers/GraphQlController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace graphiql.example.Controllers
{
[Route("graphql")]
[Route(Startup.GraphQLPath)]
public class GraphQlController : Controller
{
[HttpPost]
Expand Down
11 changes: 4 additions & 7 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
{
public class Startup
{
public const string GraphQLPath = "/api/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(); // /api/graphql
app.UseMvc();
}
}
Expand Down
58 changes: 49 additions & 9 deletions src/graphiql/GraphiQlExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Linq;
using graphiql;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
Expand All @@ -8,9 +16,11 @@ namespace Microsoft.AspNetCore.Builder
{
public static class GraphiQlExtensions
{
internal const string DefaultGraphQLPath = "/graphql";

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

public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app, string path)
Expand All @@ -22,30 +32,60 @@ public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app, stri
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 = GetFileProvider(config.Path),
EnableDefaultFiles = true,
StaticFileOptions = {ContentTypeProvider = new FileExtensionContentTypeProvider()}
};

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

return app;
}

private static IFileProvider GetFileProvider(string graphqlPath)
{
IFileProvider fileProvider;

var assembly = typeof(Microsoft.AspNetCore.Builder.GraphiQlExtensions).GetTypeInfo().Assembly;
var embeddedFileProvider = new EmbeddedFileProvider(assembly, "graphiql.assets");

if (graphqlPath.Equals(DefaultGraphQLPath, StringComparison.OrdinalIgnoreCase))
{
fileProvider = embeddedFileProvider;
}
else
{
string javascriptCode = $"var graphqlPath='{graphqlPath}';";

string dir = Path.Combine(Path.GetTempPath(), "graphiql-dotnet");
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
string file = $"{dir}/graphql-path.js";

File.WriteAllText(file, javascriptCode, Encoding.UTF8);

var physicalFileProvider = new PhysicalFileProvider(dir);

fileProvider = new CompositeFileProvider(
embeddedFileProvider,
physicalFileProvider
);
}

return fileProvider;
}
}
}
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 || '/graphql', {
method: 'post',
headers: {
'Accept': 'application/json',
Expand Down
2 changes: 2 additions & 0 deletions src/graphiql/graphiql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<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.Physical" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Composite" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="./assets/**/*.*" />
Expand Down