-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from JosephWoodward/ChangePath
Add ability to set the graphql path
- Loading branch information
Showing
14 changed files
with
70 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}';"); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters