Skip to content

Commit

Permalink
Merge pull request #25 from JosephWoodward/ChangePath
Browse files Browse the repository at this point in the history
Few tweaks
  • Loading branch information
josephwoodward authored Oct 5, 2018
2 parents c9ffeeb + 44e3872 commit d548657
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,18 @@ By default GraphiQL lives on the aforementioned `/graphql` endpoint, however it
```csharp
app.UseGraphiQl('/whatever/graphiql');
```

You can also specify GraphiQl endpoint independent of your GraphQL API, this is especially useful if you're hosting in IIS in a virtual application (ie `myapp.com/1.0/...`) or hosting API and documentation separately.

```csharp
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseGraphiQl("/graphql", "/v1/yourapi");

app.UseMvc();
}
```

Now navigating to `/graphql` will display the GraphiQL UI, but your GraphQL API will live under the `/v1/yourapi` route.


2 changes: 1 addition & 1 deletion src/graphiql.example/Controllers/GraphQlController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class GraphQlController : Controller
public async Task<IActionResult> Post([FromBody] GraphQlQuery query)
{
var schema = new Schema {Query = new StarWarsQuery()};

var result = await new DocumentExecuter().ExecuteAsync(x =>
{
x.Schema = schema;
Expand Down
1 change: 1 addition & 0 deletions src/graphiql.example/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void ConfigureServices(IServiceCollection services)
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseGraphiQl(GraphQlPath);
/*app.UseGraphiQl(GraphQlPath, "/v1/something");*/
app.UseMvc();
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/graphiql/GraphiQlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ public static class GraphiQlExtensions
public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app)
=> UseGraphiQl(app, DefaultPath);

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

/// <param name="path"></param>
/// <param name="apiPath">In some scenarios it makes sense to specify the API path and file server path independently
/// Examples: hosting in IIS in a virtual application (myapp.com/1.0/...) or hosting API and documentation separately</param>
public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app, string fileServerPath, string apiPath = null)
public static IApplicationBuilder UseGraphiQl(this IApplicationBuilder app, string path, string apiPath)
{
if (string.IsNullOrWhiteSpace(fileServerPath))
throw new ArgumentException(nameof(fileServerPath));
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentException(nameof(path));

var p = fileServerPath.EndsWith("/") ? fileServerPath : $"{fileServerPath}/" + "graphql-path.js";
app.Map(p, x => WritePathJavaScript(x, apiPath ?? fileServerPath));
var filePath = path.EndsWith("/") ? path : $"{path}/" + "graphql-path.js";
var uri = !string.IsNullOrWhiteSpace(apiPath) ? apiPath : path;
app.Map(filePath, x => WritePathJavaScript(x, uri));

return UseGraphiQlImp(app, x => x.SetPath(fileServerPath));
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));
Expand Down
4 changes: 2 additions & 2 deletions src/graphiql/graphiql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<PackageTags>graphql;graphiql;asp.net core;.net core</PackageTags>
<title>GraphiQL ASP.NET Core Middleware</title>
<TargetFramework>netstandard2.0</TargetFramework>
<VersionPrefix>1.1.0</VersionPrefix>
<VersionPrefix>1.2.0-beta-2</VersionPrefix>
<Authors>Joseph Woodward</Authors>
<Description>GraphiQL available as middleware for ASP.NET Core</Description>
<PackageIconUrl>https://raw.githubusercontent.com/JosephWoodward/graphiql-dotnet/master/assets/logo_128_128.png</PackageIconUrl>
Expand All @@ -21,4 +21,4 @@
<ItemGroup>
<EmbeddedResource Include="./assets/**/*.*" />
</ItemGroup>
</Project>
</Project>

0 comments on commit d548657

Please sign in to comment.