Skip to content

Commit

Permalink
GraphQL Voyager Middleware (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
rstaib committed Mar 2, 2019
1 parent f7f2017 commit 3e54f68
Show file tree
Hide file tree
Showing 20 changed files with 1,736 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The stitching layer now batches requests to the remote schemas.
- Introspection schema serializer.
- Introduced auto-stitching capabilities with the new `StitchingBuilder`.
- _GraphQL_ _Voyager_. Special thanks to [@drowhunter](https://github.com/drowhunter) who contributed the middleware.

### Fixed

Expand Down
Expand Up @@ -27,6 +27,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Server\AspNetClassic.Voyager\AspNetClassic.Voyager.csproj" />
<ProjectReference Include="..\..\src\Server\AspNetClassic\AspNetClassic.csproj" />
<ProjectReference Include="..\..\src\Server\AspNetClassic.Authorization\AspNetClassic.Authorization.csproj" />
<ProjectReference Include="..\..\src\Server\AspNetClassic.GraphiQL\AspNetClassic.GraphiQL.csproj" />
Expand Down
4 changes: 3 additions & 1 deletion examples/AspNetClassic.StarWars/Startup.cs
Expand Up @@ -63,7 +63,9 @@ public void Configuration(IAppBuilder appBuilder)
appBuilder
.UseGraphQL(services, new PathString("/graphql"))
.UseGraphiQL(new PathString("/graphql"))
.UsePlayground(new PathString("/graphql"));
.UsePlayground(new PathString("/graphql"))
.UseVoyager(new PathString("/graphql"));
;
}
}
}
1 change: 1 addition & 0 deletions examples/AspNetCore.StarWars/AspNetCore.StarWars.csproj
Expand Up @@ -22,6 +22,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Core\Subscriptions.InMemory\Subscriptions.InMemory.csproj" />
<ProjectReference Include="..\..\src\Server\AspNetCore.Voyager\AspNetCore.Voyager.csproj" />
<ProjectReference Include="..\..\src\Server\AspNetCore\AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Server\AspNetCore.Authorization\AspNetCore.Authorization.csproj" />
<ProjectReference Include="..\..\src\Server\AspNetCore.GraphiQL\AspNetCore.GraphiQL.csproj" />
Expand Down
4 changes: 3 additions & 1 deletion examples/AspNetCore.StarWars/Startup.cs
@@ -1,6 +1,7 @@
using System.Security.Claims;
using HotChocolate;
using HotChocolate.AspNetCore;
using HotChocolate.AspNetCore.Voyager;
using HotChocolate.Execution.Configuration;
using HotChocolate.Subscriptions;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -74,7 +75,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
.UseWebSockets()
.UseGraphQL("/graphql")
.UseGraphiQL("/graphql")
.UsePlayground("/graphql");
.UsePlayground("/graphql")
.UseVoyager("/graphql");

/*
Note: comment app.UseGraphQL("/graphql"); and uncomment this
Expand Down
48 changes: 48 additions & 0 deletions src/Server/AspNetClassic.Voyager/AspNetClassic.Voyager.csproj
@@ -0,0 +1,48 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<AssemblyName>HotChocolate.AspNetClassic.Voyager</AssemblyName>
<RootNamespace>HotChocolate.AspNetClassic.Voyager</RootNamespace>
<PackageId>HotChocolate.AspNetClassic.Voyager</PackageId>
<Description>Contains a GraphQL Voyager for ASP.net classic that can be used with the Hot Chocolate GraphQL server.</Description>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>Full</DebugType>
<DebugSymbols>true</DebugSymbols>
<DefineConstants>TRACE;DEBUG;ASPNETCLASSIC</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<DefineConstants>TRACE;ASPNETCLASSIC</DefineConstants>
</PropertyGroup>

<ItemGroup>
<None Remove="..\AspNetCore.Voyager\**\*.css;..\AspNetCore.Voyager\**\*.html;..\AspNetCore.Voyager\**\*.js;..\AspNetCore.Voyager\**\*.png" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="..\AspNetCore.Voyager\**\*.css;..\AspNetCore.Voyager\**\*.html;..\AspNetCore.Voyager\**\*.js;..\AspNetCore.Voyager\**\*.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Owin" Version="4.0.0" />
<PackageReference Include="Microsoft.Owin.StaticFiles" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Core\Core\Core.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\AspNetCore.Voyager\**\*.cs" Exclude="..\AspNetCore.Voyager\bin\**\*.cs;..\AspNetCore.Voyager\obj\**\*.cs" />
</ItemGroup>

</Project>
150 changes: 150 additions & 0 deletions src/Server/AspNetCore.Voyager/ApplicationBuilderExtensions.cs
@@ -0,0 +1,150 @@
using System;

#if ASPNETCLASSIC
using HotChocolate.AspNetClassic.Voyager;
using Microsoft.Owin;
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.StaticFiles;
using Microsoft.Owin.StaticFiles.ContentTypes;
using Owin;
using IApplicationBuilder = Owin.IAppBuilder;
#else
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
#endif

#if ASPNETCLASSIC
namespace HotChocolate.AspNetClassic.Voyager
#else
namespace HotChocolate.AspNetCore.Voyager
#endif
{
public static class ApplicationBuilderExtensions
{
#if ASPNETCLASSIC
private const string _resourcesNamespace =
"HotChocolate.AspNetClassic.Voyager.Resources";
#else
private const string _resourcesNamespace =
"HotChocolate.AspNetCore.Voyager.Resources";
#endif

public static IApplicationBuilder UseVoyager(
this IApplicationBuilder applicationBuilder)
{
return applicationBuilder.UseVoyager(new VoyagerOptions());
}

public static IApplicationBuilder UseVoyager(
this IApplicationBuilder applicationBuilder,
PathString queryPath)
{
return applicationBuilder.UseVoyager(new VoyagerOptions
{
QueryPath = queryPath,
Path = queryPath + new PathString("/voyager")
});
}

public static IApplicationBuilder UseVoyager(
this IApplicationBuilder applicationBuilder,
PathString queryPath,
PathString uiPath)
{
return applicationBuilder.UseVoyager(new VoyagerOptions
{
QueryPath = queryPath,
Path = uiPath
});
}

public static IApplicationBuilder UseVoyager(
this IApplicationBuilder applicationBuilder,
VoyagerOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

return applicationBuilder
.UseVoyagerSettingsMiddleware(options)
.UseVoyagerFileServer(options.Path);
}

private static IApplicationBuilder UseVoyagerSettingsMiddleware(
this IApplicationBuilder applicationBuilder,
VoyagerOptions options)
{
return applicationBuilder.Map(
options.Path.Add(new PathString("/settings.js")),
#if ASPNETCLASSIC
app => app.Use<SettingsMiddleware>(options));
#else
app => app.UseMiddleware<SettingsMiddleware>(options));
#endif
}

#if ASPNETCLASSIC
private static IApplicationBuilder UseVoyagerFileServer(
this IApplicationBuilder applicationBuilder,
PathString route)
{
var fileServerOptions = new FileServerOptions
{
RequestPath = route,
FileSystem = CreateFileSystem(),
EnableDefaultFiles = true,
StaticFileOptions =
{
ContentTypeProvider =
new FileExtensionContentTypeProvider()
}
};

return applicationBuilder.UseFileServer(fileServerOptions);
}
#else
private static IApplicationBuilder UseVoyagerFileServer(
this IApplicationBuilder applicationBuilder,
string path)
{
var fileServerOptions = new FileServerOptions
{
RequestPath = path,
FileProvider = CreateFileProvider(),
EnableDefaultFiles = true,
StaticFileOptions =
{
ContentTypeProvider =
new FileExtensionContentTypeProvider()
}
};

return applicationBuilder.UseFileServer(fileServerOptions);
}
#endif

#if ASPNETCLASSIC
private static IFileSystem CreateFileSystem()
{
Type type = typeof(ApplicationBuilderExtensions);

return new EmbeddedResourceFileSystem(
type.Assembly,
_resourcesNamespace);
}
#else
private static IFileProvider CreateFileProvider()
{
Type type = typeof(ApplicationBuilderExtensions);

return new EmbeddedFileProvider(
type.Assembly,
_resourcesNamespace);
}
#endif
}
}
34 changes: 34 additions & 0 deletions src/Server/AspNetCore.Voyager/AspNetCore.Voyager.csproj
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>HotChocolate.AspNetCore.Voyager</AssemblyName>
<RootNamespace>HotChocolate.AspNetCore.Voyager</RootNamespace>
<PackageId>HotChocolate.AspNetCore.Voyager</PackageId>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>Full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\*.*" />
<EmbeddedResource Include="Resources\*.*" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions src/Server/AspNetCore.Voyager/HttpContextExtensions.cs
@@ -0,0 +1,28 @@
using System.Threading;

#if ASPNETCLASSIC
using Microsoft.Owin;
using HttpContext = Microsoft.Owin.IOwinContext;
#else
using Microsoft.AspNetCore.Http;
#endif

#if ASPNETCLASSIC
namespace HotChocolate.AspNetClassic.Voyager
#else
namespace HotChocolate.AspNetCore.Voyager
#endif
{
internal static class HttpContextExtensions
{
public static CancellationToken GetCancellationToken(
this HttpContext context)
{
#if ASPNETCLASSIC
return context.Request.CallCancelled;
#else
return context.RequestAborted;
#endif
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e54f68

Please sign in to comment.