Skip to content

Commit

Permalink
A few changes (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephwoodward authored Mar 12, 2020
1 parent c3f6411 commit b420013
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
48 changes: 23 additions & 25 deletions src/GraphiQl/GraphiQlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace GraphiQl
{
public static class GraphiQlExtensions
{
private const string DefaultGraphQlPath = "/graphql";

/*
public static IServiceCollection AddGraphiQl(this IServiceCollection services)
=> services.AddGraphiQl(null);
*/

public static IServiceCollection AddGraphiQl(this IServiceCollection services, Action<GraphiQlOptions> configure)
{
Expand All @@ -25,54 +25,52 @@ public static IServiceCollection AddGraphiQl(this IServiceCollection services, A
}

services.TryAdd(ServiceDescriptor.Transient<IConfigureOptions<GraphiQlOptions>, GraphiQlOptionsSetup>());

return services;
}

public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app)
{
var options = app.ApplicationServices.GetService<IOptions<GraphiQlOptions>>().Value;
=> app.UseGraphiQl(null);

var filePath = $"{options.GraphiQlPath.TrimEnd('/')}/graphql-path.js";
var graphQlPath = !string.IsNullOrWhiteSpace(options.GraphQlApiPath) ? options.GraphQlApiPath : DefaultGraphQlPath;
app.Map(filePath, x => WritePathJavaScript(x, graphQlPath));
public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app, string path)
=> app.UseGraphiQl(path, null);

return UseGraphiQlImp(app, options);
}
public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app, string path, string apiPath)
=> app.UseGraphiQlImp(path, apiPath);

private static IApplicationBuilder UseGraphiQlImp(this IApplicationBuilder app, GraphiQlOptions options)
private static IApplicationBuilder UseGraphiQlImp(this IApplicationBuilder app, string path, string apiPath )
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));

var provider = new EmbeddedFileProvider(typeof(GraphiQlExtensions).GetTypeInfo().Assembly, "GraphiQl.assets");
}

var options = app.ApplicationServices.GetService<IOptions<GraphiQlOptions>>().Value;

var graphiQlPath = !string.IsNullOrWhiteSpace(path) ? path : options.GraphiQlPath;
var graphQlApiPath = !string.IsNullOrWhiteSpace(apiPath) ? apiPath : options.GraphQlApiPath;

var fileServerOptions = new FileServerOptions
{
RequestPath = options.GraphiQlPath,
FileProvider = new AssetProvider(provider),
RequestPath = graphiQlPath,
FileProvider = new AssetProvider(new EmbeddedFileProvider(typeof(GraphiQlExtensions).GetTypeInfo().Assembly, "GraphiQl.assets")),
EnableDefaultFiles = true,
StaticFileOptions = {ContentTypeProvider = new FileExtensionContentTypeProvider()}
};

app.UseMiddleware<GraphiQlMiddleware>();

app.Map($"{graphiQlPath.TrimEnd('/')}/graphql-path.js", x => WritePathJavaScript(x, graphQlApiPath));
app.UseFileServer(fileServerOptions);

return app;
}

private static void WritePathJavaScript(IApplicationBuilder app, string path) =>
app.Run(h =>
private static void WritePathJavaScript(IApplicationBuilder app, string path)
=> app.Run(h =>
{
h.Response.ContentType = "application/javascript";
return h.Response.WriteAsync($"var graphqlPath='{path}';");
});

[Obsolete("This overload has been marked as obsolete, please configure via IServiceCollection.AddGraphiQl(..) instead or consult the documentation", true)]
public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app, string path)
=> throw new NotImplementedException();

[Obsolete("This overload has been marked as obsolete, please configure via IServiceCollection.AddGraphiQl(..) instead or consult the documentation", true)]
public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app, string path, string apiPath)
=> throw new NotImplementedException();
}
}
3 changes: 1 addition & 2 deletions tests/GraphiQl.Demo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public Startup(IConfiguration configuration)

public IConfiguration Configuration { get; }

public virtual void ConfigureGraphQl(IServiceCollection services)
=> services.AddGraphiQl();
public virtual void ConfigureGraphQl(IServiceCollection services) {}

public void ConfigureServices(IServiceCollection services)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using GraphiQl.Demo;
using Microsoft.AspNetCore;
Expand Down Expand Up @@ -51,9 +52,7 @@ public void CanOverrideGraphQlPath()
var button = driver.FindElementByClassName("execute-button");
button?.Click();

driver.Manage()
.Timeouts()
.ImplicitWait = TimeSpan.FromSeconds(2);
Thread.Sleep(2000);

// UGH!
result = driver
Expand Down
2 changes: 1 addition & 1 deletion tests/GraphiQl.Tests/SeleniumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GraphiQl.Tests
public abstract class SeleniumTest
{
private ChromeDriver Driver { get; }
protected bool RunHeadless { get; set; } = true;
protected bool RunHeadless { get; set; } = false;

protected SeleniumTest()
{
Expand Down

0 comments on commit b420013

Please sign in to comment.