From 4b3189b5ad3c49812320199c076444bb726cfef9 Mon Sep 17 00:00:00 2001 From: Poulad Date: Mon, 19 Feb 2018 17:10:54 -0500 Subject: [PATCH 1/4] Configure graphql path --- .../Controllers/GraphQlController.cs | 2 +- src/graphiql.example/Startup.cs | 11 ++-- src/graphiql/GraphiQlExtensions.cs | 58 ++++++++++++++++--- src/graphiql/assets/index.html | 3 +- src/graphiql/graphiql.csproj | 2 + 5 files changed, 58 insertions(+), 18 deletions(-) diff --git a/src/graphiql.example/Controllers/GraphQlController.cs b/src/graphiql.example/Controllers/GraphQlController.cs index 147f74d..981d182 100644 --- a/src/graphiql.example/Controllers/GraphQlController.cs +++ b/src/graphiql.example/Controllers/GraphQlController.cs @@ -7,7 +7,7 @@ namespace graphiql.example.Controllers { - [Route("graphql")] + [Route(Startup.GraphQLPath)] public class GraphQlController : Controller { [HttpPost] diff --git a/src/graphiql.example/Startup.cs b/src/graphiql.example/Startup.cs index fe44015..04fa606 100644 --- a/src/graphiql.example/Startup.cs +++ b/src/graphiql.example/Startup.cs @@ -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; @@ -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(); } } diff --git a/src/graphiql/GraphiQlExtensions.cs b/src/graphiql/GraphiQlExtensions.cs index 34138fa..36821b6 100644 --- a/src/graphiql/GraphiQlExtensions.cs +++ b/src/graphiql/GraphiQlExtensions.cs @@ -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; @@ -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) @@ -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 setConfig) + private static IApplicationBuilder UseGraphiQlImp(this IApplicationBuilder app, + Action 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; + } } } \ No newline at end of file diff --git a/src/graphiql/assets/index.html b/src/graphiql/assets/index.html index 0fb342e..804a7c7 100644 --- a/src/graphiql/assets/index.html +++ b/src/graphiql/assets/index.html @@ -39,6 +39,7 @@ --> + @@ -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', diff --git a/src/graphiql/graphiql.csproj b/src/graphiql/graphiql.csproj index 7a47ed0..a9ac51b 100644 --- a/src/graphiql/graphiql.csproj +++ b/src/graphiql/graphiql.csproj @@ -16,6 +16,8 @@ + + From b27461ff55af43389faadb658b95a7ebc34eb4f3 Mon Sep 17 00:00:00 2001 From: Joseph Woodward Date: Thu, 9 Aug 2018 18:00:16 +0100 Subject: [PATCH 2/4] Enable ability to change path --- .../Controllers/GraphQlController.cs | 9 +-- src/graphiql.example/GraphQl/Droid.cs | 2 +- src/graphiql.example/GraphQl/GraphQlQuery.cs | 2 +- .../GraphQl/Models/DroidType.cs | 2 +- .../GraphQl/Models/StarWarsQuery.cs | 2 +- src/graphiql.example/Program.cs | 11 +-- src/graphiql.example/Startup.cs | 6 +- src/graphiql.example/graphiql.example.csproj | 1 + src/graphiql.tests/UnitTest1.cs | 2 +- src/graphiql/GraphiQlConfig.cs | 2 +- src/graphiql/GraphiQlExtensions.cs | 69 ++++++++----------- src/graphiql/assets/index.html | 2 +- 12 files changed, 45 insertions(+), 65 deletions(-) diff --git a/src/graphiql.example/Controllers/GraphQlController.cs b/src/graphiql.example/Controllers/GraphQlController.cs index 981d182..cb82d9b 100644 --- a/src/graphiql.example/Controllers/GraphQlController.cs +++ b/src/graphiql.example/Controllers/GraphQlController.cs @@ -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(Startup.GraphQLPath)] + [Route(Startup.GraphQlPath)] public class GraphQlController : Controller { [HttpPost] diff --git a/src/graphiql.example/GraphQl/Droid.cs b/src/graphiql.example/GraphQl/Droid.cs index 0d1cbc3..201c76f 100644 --- a/src/graphiql.example/GraphQl/Droid.cs +++ b/src/graphiql.example/GraphQl/Droid.cs @@ -1,4 +1,4 @@ -namespace graphiql.example.GraphQl +namespace GraphiQl.example.GraphQl { public class Droid { diff --git a/src/graphiql.example/GraphQl/GraphQlQuery.cs b/src/graphiql.example/GraphQl/GraphQlQuery.cs index ffc1a17..2199dd0 100644 --- a/src/graphiql.example/GraphQl/GraphQlQuery.cs +++ b/src/graphiql.example/GraphQl/GraphQlQuery.cs @@ -1,6 +1,6 @@ using GraphQL; -namespace graphiql.example.GraphQl +namespace GraphiQl.example.GraphQl { public class GraphQlQuery { diff --git a/src/graphiql.example/GraphQl/Models/DroidType.cs b/src/graphiql.example/GraphQl/Models/DroidType.cs index 2ecd949..0870749 100644 --- a/src/graphiql.example/GraphQl/Models/DroidType.cs +++ b/src/graphiql.example/GraphQl/Models/DroidType.cs @@ -1,6 +1,6 @@ using GraphQL.Types; -namespace graphiql.example.GraphQl.Models +namespace GraphiQl.example.GraphQl.Models { public class DroidType : ObjectGraphType { diff --git a/src/graphiql.example/GraphQl/Models/StarWarsQuery.cs b/src/graphiql.example/GraphQl/Models/StarWarsQuery.cs index 344b67e..6cc46a0 100644 --- a/src/graphiql.example/GraphQl/Models/StarWarsQuery.cs +++ b/src/graphiql.example/GraphQl/Models/StarWarsQuery.cs @@ -1,6 +1,6 @@ using GraphQL.Types; -namespace graphiql.example.GraphQl.Models +namespace GraphiQl.example.GraphQl.Models { public class StarWarsQuery : ObjectGraphType { diff --git a/src/graphiql.example/Program.cs b/src/graphiql.example/Program.cs index 42e82d3..5788b67 100644 --- a/src/graphiql.example/Program.cs +++ b/src/graphiql.example/Program.cs @@ -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 { diff --git a/src/graphiql.example/Startup.cs b/src/graphiql.example/Startup.cs index 04fa606..ac3f528 100644 --- a/src/graphiql.example/Startup.cs +++ b/src/graphiql.example/Startup.cs @@ -4,11 +4,11 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -namespace graphiql.example +namespace GraphiQl.example { public class Startup { - public const string GraphQLPath = "/api/graphql"; + public const string GraphQlPath = "/graphql"; public Startup(IConfiguration configuration) { @@ -26,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(); // /api/graphql + app.UseGraphiQl(GraphQlPath); app.UseMvc(); } } diff --git a/src/graphiql.example/graphiql.example.csproj b/src/graphiql.example/graphiql.example.csproj index 5649964..624ec8f 100644 --- a/src/graphiql.example/graphiql.example.csproj +++ b/src/graphiql.example/graphiql.example.csproj @@ -4,6 +4,7 @@ aspnet-graphiql.example-D0705FC5-6A16-43BB-AE45-3C609BE0FE6A + diff --git a/src/graphiql.tests/UnitTest1.cs b/src/graphiql.tests/UnitTest1.cs index e402061..fca08fa 100644 --- a/src/graphiql.tests/UnitTest1.cs +++ b/src/graphiql.tests/UnitTest1.cs @@ -1,7 +1,7 @@ using System; using Xunit; -namespace graphiql.tests +namespace GraphiQl.tests { public class UnitTest1 { diff --git a/src/graphiql/GraphiQlConfig.cs b/src/graphiql/GraphiQlConfig.cs index da76c59..dbd76d7 100644 --- a/src/graphiql/GraphiQlConfig.cs +++ b/src/graphiql/GraphiQlConfig.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Net; -namespace graphiql +namespace GraphiQl { public class GraphiQlConfig { diff --git a/src/graphiql/GraphiQlExtensions.cs b/src/graphiql/GraphiQlExtensions.cs index 36821b6..33a601d 100644 --- a/src/graphiql/GraphiQlExtensions.cs +++ b/src/graphiql/GraphiQlExtensions.cs @@ -1,34 +1,30 @@ 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.Builder; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.FileProviders; -namespace Microsoft.AspNetCore.Builder +namespace GraphiQl { public static class GraphiQlExtensions { - internal const string DefaultGraphQLPath = "/graphql"; - + private const string DefaultPath = "/graphql"; + public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app) - { - return UseGraphiQl(app, DefaultGraphQLPath); - } + => 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)); } @@ -46,7 +42,7 @@ private static IApplicationBuilder UseGraphiQlImp(this IApplicationBuilder app, var fileServerOptions = new FileServerOptions { RequestPath = config.Path, - FileProvider = GetFileProvider(config.Path), + FileProvider = BuildFileProvider(), EnableDefaultFiles = true, StaticFileOptions = {ContentTypeProvider = new FileExtensionContentTypeProvider()} }; @@ -56,36 +52,25 @@ private static IApplicationBuilder UseGraphiQlImp(this IApplicationBuilder app, return app; } - private static IFileProvider GetFileProvider(string graphqlPath) + private static IFileProvider BuildFileProvider() { - IFileProvider fileProvider; - - var assembly = typeof(Microsoft.AspNetCore.Builder.GraphiQlExtensions).GetTypeInfo().Assembly; + var assembly = typeof(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 - ); - } + 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}';"); + }); + } } } \ No newline at end of file diff --git a/src/graphiql/assets/index.html b/src/graphiql/assets/index.html index 804a7c7..6c5e0f0 100644 --- a/src/graphiql/assets/index.html +++ b/src/graphiql/assets/index.html @@ -110,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(graphqlPath || '/graphql', { + return fetch(graphqlPath, { method: 'post', headers: { 'Accept': 'application/json', From 6981f337c7c7ddac85a981a75ddc274fe710170a Mon Sep 17 00:00:00 2001 From: Joseph Woodward Date: Mon, 20 Aug 2018 21:48:41 +0100 Subject: [PATCH 3/4] Updated readme --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9416e41..f30cee6 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +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'); +``` From ca285b05ee31877fac4f7f4ce8faea1c9162e0af Mon Sep 17 00:00:00 2001 From: Joseph Woodward Date: Mon, 20 Aug 2018 21:51:47 +0100 Subject: [PATCH 4/4] Remove unused dependency --- src/graphiql/graphiql.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/graphiql/graphiql.csproj b/src/graphiql/graphiql.csproj index a9ac51b..800e424 100644 --- a/src/graphiql/graphiql.csproj +++ b/src/graphiql/graphiql.csproj @@ -16,7 +16,6 @@ -