Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to inherit from GraphQLHttpMiddleware #30

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ jobs:
- uses: actions/checkout@v3

- name: Setup .NET Core SDKs
uses: actions/setup-dotnet@v2
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
3.1.x
5.0.x
6.0.x
8.0.x

- name: Restore
run: |
Expand Down
5 changes: 3 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
<PropertyGroup>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<GraphQLVersion>7.1.1</GraphQLVersion>
<GraphQLServerVersion>7.1.1</GraphQLServerVersion>
<GraphQLVersion>7.6.0</GraphQLVersion>
<GraphQLServerVersion>7.6.0</GraphQLServerVersion>
<MicrosoftAspNetCoreHttpAbstractionsVersion>2.2.0</MicrosoftAspNetCoreHttpAbstractionsVersion>
<MicrosoftExtensionsLoggingVersion>6.0.0</MicrosoftExtensionsLoggingVersion>
<MicrosoftExtensionsOptionsVersion>6.0.0</MicrosoftExtensionsOptionsVersion>
Expand Down
11 changes: 9 additions & 2 deletions GraphQL.Upload.AspNetCore.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.329
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileUploadSample", "samples\FileUploadSample\FileUploadSample.csproj", "{58C0B73A-2436-4F56-89E0-BD5D22E00047}"
EndProject
Expand All @@ -13,6 +13,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{237BB3B8
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{CC98F604-7F80-4718-A307-7AA63FF55361}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{0D65FAD1-1899-4283-B1BB-6EAC4A097AA5}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
LICENSE = LICENSE
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ Preview versions from the develop branch are available via [GitHub Packages](htt

Register the middleware in your Startup.cs.

This middleware implementation **only** parses multipart requests. That's why we're using additional middleware ([graphql-dotnet/server](https://github.com/graphql-dotnet/server)) to handle other request types.
This middleware inherits from `GraphQLHttpMiddleware` and as such supports all of the base functionality provied by `GraphQL.Server.Transports.AspNetCore`.

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<MySchema>()
.AddGraphQLUpload()
.AddGraphQL();
services.AddGraphQL(b => b
.AddSchema<MySchema>()
.AddSystemTextJson()
.AddGraphQLUpload());
}

public void Configure(IApplicationBuilder app)
{
app.UseGraphQLUpload<MySchema>()
.UseGraphQL<MySchema>();
app.UseGraphQLUpload();
}
```

Expand Down
1 change: 1 addition & 0 deletions samples/FileUploadSample/FileUploadSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
Expand Down
48 changes: 34 additions & 14 deletions samples/FileUploadSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
using Microsoft.AspNetCore;
using FileUploadSample;
using GraphQL;
using GraphQL.Types;

namespace FileUploadSample
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<UploadRepository>();

builder.Services.AddGraphQL(builder => builder
.AddSchema<SampleSchema>()
.AddGraphTypes()
.AddGraphQLUpload()
.AddErrorInfoProvider(opt => opt.ExposeExceptionDetails = true)
.AddSystemTextJson());

builder.Services.AddCors();


var app = builder.Build();

if (app.Environment.IsDevelopment())
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseStartup<Startup>();
}
app.UseDeveloperExceptionPage();
}

app.UseStaticFiles();

app.UseCors(b => b
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());

// register the middleware
app.UseGraphQLUpload();
app.UseGraphQLPlayground("/");

await app.RunAsync();
43 changes: 0 additions & 43 deletions samples/FileUploadSample/Startup.cs

This file was deleted.

95 changes: 36 additions & 59 deletions src/GraphQL.Upload.AspNetCore/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,48 @@
using GraphQL.Upload.AspNetCore;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Builder
namespace Microsoft.AspNetCore.Builder;

/// <summary>
/// Extension methods for adding <see cref="GraphQLUploadMiddleware{TSchema}"/> to an application.
/// </summary>
public static class ApplicationBuilderExtensions
{
/// <summary>
/// Extension methods for adding <see cref="GraphQLUploadMiddleware{TSchema}"/> to an application.
/// Adds the <see cref="GraphQLUploadMiddleware{TSchema}"/> to handle file uploads in GraphQL requests.
/// </summary>
public static class ApplicationBuilderExtensions
/// <typeparam name="TSchema">The implementation of <see cref="ISchema"/> to use</typeparam>
/// <param name="builder">The application builder</param>
/// <param name="path">The path to the GraphQL endpoint which defaults to '/graphql'</param>
/// <returns>The <see cref="IApplicationBuilder"/> received as parameter</returns>
public static IApplicationBuilder UseGraphQLUpload<TSchema>(this IApplicationBuilder builder, string path = "/graphql", Action<GraphQLUploadOptions>? configureOptions = null)
where TSchema : ISchema
{
/// <summary>
/// Adds the <see cref="GraphQLUploadMiddleware{TSchema}"/> to handle file uploads in GraphQL requests.
/// </summary>
/// <typeparam name="TSchema">The implementation of <see cref="ISchema"/> to use</typeparam>
/// <param name="builder">The application builder</param>
/// <param name="path">The path to the GraphQL endpoint which defaults to '/graphql'</param>
/// <returns>The <see cref="IApplicationBuilder"/> received as parameter</returns>
public static IApplicationBuilder UseGraphQLUpload<TSchema>(this IApplicationBuilder builder, string path = "/graphql")
where TSchema : ISchema
{
return builder.UseGraphQLUpload<TSchema>(new PathString(path));
}

/// <summary>
/// Adds the <see cref="GraphQLUploadMiddleware{TSchema}"/> to handle file uploads in GraphQL requests.
/// </summary>
/// <typeparam name="TSchema">The implementation of <see cref="ISchema"/> to use</typeparam>
/// <param name="builder">The application builder</param>
/// <param name="path">The path to the GraphQL endpoint</param>
/// <returns>The <see cref="IApplicationBuilder"/> received as parameter</returns>>
public static IApplicationBuilder UseGraphQLUpload<TSchema>(this IApplicationBuilder builder, PathString path)
where TSchema : ISchema
{
return builder.UseGraphQLUpload<TSchema>(path, new GraphQLUploadOptions());
}
return builder.UseGraphQLUpload<TSchema>(new PathString(path), configureOptions);
}

/// <summary>
/// Adds the <see cref="GraphQLUploadMiddleware{TSchema}"/> to handle file uploads in GraphQL requests.
/// </summary>
/// <typeparam name="TSchema">The implementation of <see cref="ISchema"/> to use</typeparam>
/// <param name="builder">The application builder</param>
/// <param name="path">The path to the GraphQL endpoint</param>
/// <param name="configureOptions">A delegate that is used to configure the <see cref="GraphQLUploadOptions"/>, which are passed to the <see cref="GraphQLUploadMiddleware{TSchema}"/></param>
/// <returns>The <see cref="IApplicationBuilder"/> received as parameter</returns>>
public static IApplicationBuilder UseGraphQLUpload<TSchema>(this IApplicationBuilder builder, PathString path, Action<GraphQLUploadOptions> configureOptions)
where TSchema : ISchema
{
var options = new GraphQLUploadOptions();
configureOptions(options);
/// <summary>
/// Adds the <see cref="GraphQLUploadMiddleware{TSchema}"/> to handle file uploads in GraphQL requests.
/// </summary>
/// <typeparam name="TSchema">The implementation of <see cref="ISchema"/> to use</typeparam>
/// <param name="builder">The application builder</param>
/// <param name="path">The path to the GraphQL endpoint</param>
/// <param name="configureOptions">A delegate that is used to configure the <see cref="GraphQLUploadOptions"/>, which are passed to the <see cref="GraphQLUploadMiddleware{TSchema}"/></param>
/// <returns>The <see cref="IApplicationBuilder"/> received as parameter</returns>>
public static IApplicationBuilder UseGraphQLUpload<TSchema>(this IApplicationBuilder builder, PathString path, Action<GraphQLUploadOptions>? configureOptions = null)
where TSchema : ISchema
{
var options = new GraphQLUploadOptions();
configureOptions?.Invoke(options);

return builder.UseGraphQLUpload<TSchema>(path, options);
}
return builder.UseGraphQL<GraphQLUploadMiddleware<TSchema>>(path, options);
}

/// <summary>
/// Adds the <see cref="GraphQLUploadMiddleware{TSchema}"/> to handle file uploads in GraphQL requests.
/// </summary>
/// <typeparam name="TSchema">The implementation of <see cref="ISchema"/> to use</typeparam>
/// <param name="builder">The application builder</param>
/// <param name="path">The path to the GraphQL endpoint</param>
/// <param name="options">The options used to configure the <see cref="GraphQLUploadMiddleware{TSchema}"/></param>
/// <returns>The <see cref="IApplicationBuilder"/> received as parameter</returns>>
public static IApplicationBuilder UseGraphQLUpload<TSchema>(this IApplicationBuilder builder, PathString path, GraphQLUploadOptions options)
where TSchema : ISchema
{
if (options is null)
throw new ArgumentNullException(nameof(options));
/// <inheritdoc cref="UseGraphQLUpload{TSchema}(IApplicationBuilder, string, Action{GraphQLUploadOptions}?)"/>
public static IApplicationBuilder UseGraphQLUpload(this IApplicationBuilder builder, string path = "/graphql", Action<GraphQLUploadOptions>? configureOptions = null)
=> UseGraphQLUpload<ISchema>(builder, path, configureOptions);

return builder.UseMiddleware<GraphQLUploadMiddleware<TSchema>>(options, path);
}
}
/// <inheritdoc cref="UseGraphQLUpload{TSchema}(IApplicationBuilder, PathString, Action{GraphQLUploadOptions}?)"/>
public static IApplicationBuilder UseGraphQLUpload(this IApplicationBuilder builder, PathString path, Action<GraphQLUploadOptions>? configureOptions = null)
=> UseGraphQLUpload<ISchema>(builder, path, configureOptions);
}
18 changes: 18 additions & 0 deletions src/GraphQL.Upload.AspNetCore/BadMapPathError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Net;

namespace GraphQL.Upload.AspNetCore;

/// <summary>
/// Represents an error when an invalid map path is provided in a GraphQL file upload request.
/// </summary>
public class BadMapPathError : GraphQLUploadError
{
/// <summary>
/// Initializes a new instance of the <see cref="BadMapPathError"/> class.
/// </summary>
/// <param name="innerException">The inner exception, if any, that caused the bad map path error.</param>
public BadMapPathError(Exception? innerException = null)
: base("Invalid map path." + (innerException != null ? " " + innerException.Message : null), HttpStatusCode.BadRequest, innerException)
{
}
}
17 changes: 17 additions & 0 deletions src/GraphQL.Upload.AspNetCore/FileSizeExceededError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Net;

namespace GraphQL.Upload.AspNetCore;

/// <summary>
/// Represents an error when a file exceeds the allowed size limit in a GraphQL upload.
/// </summary>
public class FileSizeExceededError : GraphQLUploadError
{
/// <summary>
/// Initializes a new instance of the <see cref="FileSizeExceededError"/> class.
/// </summary>
public FileSizeExceededError()
: base("File size limit exceeded.", HttpStatusCode.RequestEntityTooLarge)
{
}
}
16 changes: 8 additions & 8 deletions src/GraphQL.Upload.AspNetCore/GraphQL.Upload.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;</TargetFrameworks>
<Version>3.0.3</Version>
<TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks>
<Version>4.0.0</Version>
<Authors>Jannik Lassahn</Authors>
<Company />
<RepositoryUrl>https://github.com/JannikLassahn/graphql-dotnet-upload</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageProjectUrl>https://github.com/JannikLassahn/graphql-dotnet-upload</PackageProjectUrl>
<Description>Middleware and an Upload scalar to add support for GraphQL multipart requests for ASP.NET Core</Description>
<PackageTags>ASP.NET Core, GraphQL, File Upload</PackageTags>
<DebugType>embedded</DebugType>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" Condition="'$(TargetFramework)' == 'net6.0'" />
<PackageReference Include="GraphQL" Version="$(GraphQLVersion)" />
<PackageReference Include="GraphQL.SystemTextJson" Version="$(GraphQLVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingVersion)" />
<PackageReference Include="Microsoft.Extensions.Options" Version="$(MicrosoftExtensionsOptionsVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="$(MicrosoftAspNetCoreHttpAbstractionsVersion)" Condition="'$(TargetFramework)' == 'netstandard2.1' Or '$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="$(GraphQLServerVersion)" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" Visible="false" />
<None Include="..\..\LICENSE" Pack="true" PackagePath="\" Visible="false" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions src/GraphQL.Upload.AspNetCore/GraphQLBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using GraphQL.DI;
using GraphQL.Upload.AspNetCore;

namespace GraphQL;

/// <summary>
/// Provides extension methods for setting up GraphQL file upload support in an application.
/// </summary>
public static class GraphQLUploadExtensions
{
/// <summary>
/// Registers <see cref="UploadGraphType"/> within the dependency injection framework
/// as a singleton for use within a GraphQL schema.
/// </summary>
public static IGraphQLBuilder AddGraphQLUpload(this IGraphQLBuilder builder)
{
builder.Services.Register<UploadGraphType>(ServiceLifetime.Singleton);

return builder;
}
}
Loading