Skip to content

Commit 6c0a8e3

Browse files
committed
Update sample
1 parent c823c0a commit 6c0a8e3

File tree

4 files changed

+85
-13
lines changed

4 files changed

+85
-13
lines changed
Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-

2-
using GraphQL;
1+
using GraphQL;
32
using GraphQL.Types;
43
using GraphQL.Upload.AspNetCore;
54
using Microsoft.AspNetCore.Http;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
68

79
namespace FileUploadSample
810
{
@@ -11,24 +13,22 @@ public class SampleSchema : Schema
1113
public SampleSchema(IDependencyResolver resolver)
1214
: base(resolver)
1315
{
14-
RegisterValueConverter(new FormFileConverter());
15-
1616
Query = resolver.Resolve<Query>();
1717
Mutation = resolver.Resolve<Mutation>();
1818
}
1919
}
2020

2121
public class Query : ObjectGraphType
2222
{
23-
public Query()
23+
public Query(UploadRepository uploads)
2424
{
25-
Field<StringGraphType>("hello", resolve: ctx => "hi :)");
25+
Field<ListGraphType<FileGraphType>>("uploads", resolve: ctx => uploads.Files);
2626
}
2727
}
2828

2929
public class Mutation : ObjectGraphType
3030
{
31-
public Mutation()
31+
public Mutation(UploadRepository uploads)
3232
{
3333
Field<FileGraphType>(
3434
"singleUpload",
@@ -37,24 +37,37 @@ public Mutation()
3737
resolve: context =>
3838
{
3939
var file = context.GetArgument<IFormFile>("file");
40-
return new File { Name = file.FileName, ContentType = file.ContentType };
40+
return uploads.Save(file);
4141
});
42-
}
4342

43+
Field<ListGraphType<FileGraphType>>(
44+
"multipleUpload",
45+
arguments: new QueryArguments(
46+
new QueryArgument<ListGraphType<UploadGraphType>> { Name = "files" }),
47+
resolve: context =>
48+
{
49+
var files = context.GetArgument<IEnumerable<IFormFile>>("files");
50+
return Task.WhenAll(files.Select(file => uploads.Save(file)));
51+
});
52+
}
4453
}
4554

4655
public class File
4756
{
57+
public string Id { get; set; }
4858
public string Name { get; set; }
49-
public string ContentType { get; set; }
59+
public string MimeType { get; set; }
60+
public string Path { get; set; }
5061
}
5162

5263
public class FileGraphType : ObjectGraphType<File>
5364
{
5465
public FileGraphType()
5566
{
56-
Field(f => f.Name);
57-
Field(f => f.ContentType);
67+
Field(f => f.Id).Name("id");
68+
Field(f => f.Name).Name("filename");
69+
Field(f => f.MimeType).Name("mimetype");
70+
Field(f => f.Path).Name("path");
5871
}
5972
}
6073
}

samples/FileUploadSample/Startup.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class Startup
1212
{
1313
public void ConfigureServices(IServiceCollection services)
1414
{
15+
services.AddSingleton<UploadRepository>();
16+
1517
services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));
1618

1719
services.AddSingleton<ISchema, SampleSchema>();
@@ -24,6 +26,8 @@ public void ConfigureServices(IServiceCollection services)
2426
{
2527
_.ExposeExceptions = true;
2628
});
29+
30+
services.AddCors();
2731
}
2832

2933
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
@@ -33,6 +37,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
3337
app.UseDeveloperExceptionPage();
3438
}
3539

40+
app.UseStaticFiles();
41+
42+
app.UseCors(b => b
43+
.AllowAnyOrigin()
44+
.AllowAnyMethod()
45+
.AllowAnyHeader());
46+
3647
// register the middleware that can handle multipart requests first
3748
app.UseGraphQLUpload<ISchema>();
3849

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
using System.Collections.Concurrent;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
8+
namespace FileUploadSample
9+
{
10+
public class UploadRepository
11+
{
12+
private readonly ConcurrentBag<File> _files;
13+
private readonly string _uploadDirectory;
14+
15+
public UploadRepository()
16+
{
17+
_files = new ConcurrentBag<File>();
18+
_uploadDirectory = Path.Combine(Directory.GetCurrentDirectory(), "uploads");
19+
Directory.CreateDirectory(_uploadDirectory);
20+
}
21+
22+
public async Task<File> Save(IFormFile formFile)
23+
{
24+
var id = Guid.NewGuid().ToString().Substring(0, 8);
25+
var path = Path.Combine(_uploadDirectory, id + Path.GetExtension(formFile.FileName));
26+
27+
using (var fs = formFile.OpenReadStream())
28+
using (var ws = System.IO.File.Create(path))
29+
{
30+
await fs.CopyToAsync(ws);
31+
}
32+
33+
var file = new File
34+
{
35+
Id = id,
36+
MimeType = formFile.ContentType,
37+
Name = formFile.FileName,
38+
Path = path
39+
};
40+
_files.Add(file);
41+
42+
return file;
43+
}
44+
45+
public IEnumerable<File> Files => _files;
46+
}
47+
}

src/GraphQL.Upload.AspNetCore/GraphQL.Upload.AspNetCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>0.1.0</Version>
5+
<Version>0.2.0</Version>
66
<Authors>Jannik Lassahn</Authors>
77
<Company />
88
<RepositoryUrl>https://github.com/JannikLassahn/graphql-dotnet-upload</RepositoryUrl>
99
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1010
<PackageProjectUrl>https://github.com/JannikLassahn/graphql-dotnet-upload</PackageProjectUrl>
1111
<Description>Middleware and an Upload scalar to add support for GraphQL multipart requests for ASP.NET Core</Description>
12+
<PackageTags>ASP.NET Core, GraphQL, File Upload</PackageTags>
1213
</PropertyGroup>
1314

1415
<ItemGroup>

0 commit comments

Comments
 (0)