Skip to content

Commit

Permalink
Update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikLassahn committed Feb 8, 2019
1 parent c823c0a commit 6c0a8e3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 13 deletions.
37 changes: 25 additions & 12 deletions samples/FileUploadSample/SampleSchema.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

using GraphQL;
using GraphQL;
using GraphQL.Types;
using GraphQL.Upload.AspNetCore;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FileUploadSample
{
Expand All @@ -11,24 +13,22 @@ public class SampleSchema : Schema
public SampleSchema(IDependencyResolver resolver)
: base(resolver)
{
RegisterValueConverter(new FormFileConverter());

Query = resolver.Resolve<Query>();
Mutation = resolver.Resolve<Mutation>();
}
}

public class Query : ObjectGraphType
{
public Query()
public Query(UploadRepository uploads)
{
Field<StringGraphType>("hello", resolve: ctx => "hi :)");
Field<ListGraphType<FileGraphType>>("uploads", resolve: ctx => uploads.Files);
}
}

public class Mutation : ObjectGraphType
{
public Mutation()
public Mutation(UploadRepository uploads)
{
Field<FileGraphType>(
"singleUpload",
Expand All @@ -37,24 +37,37 @@ public Mutation()
resolve: context =>
{
var file = context.GetArgument<IFormFile>("file");
return new File { Name = file.FileName, ContentType = file.ContentType };
return uploads.Save(file);
});
}

Field<ListGraphType<FileGraphType>>(
"multipleUpload",
arguments: new QueryArguments(
new QueryArgument<ListGraphType<UploadGraphType>> { Name = "files" }),
resolve: context =>
{
var files = context.GetArgument<IEnumerable<IFormFile>>("files");
return Task.WhenAll(files.Select(file => uploads.Save(file)));
});
}
}

public class File
{
public string Id { get; set; }
public string Name { get; set; }
public string ContentType { get; set; }
public string MimeType { get; set; }
public string Path { get; set; }
}

public class FileGraphType : ObjectGraphType<File>
{
public FileGraphType()
{
Field(f => f.Name);
Field(f => f.ContentType);
Field(f => f.Id).Name("id");
Field(f => f.Name).Name("filename");
Field(f => f.MimeType).Name("mimetype");
Field(f => f.Path).Name("path");
}
}
}
11 changes: 11 additions & 0 deletions samples/FileUploadSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<UploadRepository>();

services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));

services.AddSingleton<ISchema, SampleSchema>();
Expand All @@ -24,6 +26,8 @@ public void ConfigureServices(IServiceCollection services)
{
_.ExposeExceptions = true;
});

services.AddCors();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
Expand All @@ -33,6 +37,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseDeveloperExceptionPage();
}

app.UseStaticFiles();

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

// register the middleware that can handle multipart requests first
app.UseGraphQLUpload<ISchema>();

Expand Down
47 changes: 47 additions & 0 deletions samples/FileUploadSample/UploadRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace FileUploadSample
{
public class UploadRepository
{
private readonly ConcurrentBag<File> _files;
private readonly string _uploadDirectory;

public UploadRepository()
{
_files = new ConcurrentBag<File>();
_uploadDirectory = Path.Combine(Directory.GetCurrentDirectory(), "uploads");
Directory.CreateDirectory(_uploadDirectory);
}

public async Task<File> Save(IFormFile formFile)
{
var id = Guid.NewGuid().ToString().Substring(0, 8);
var path = Path.Combine(_uploadDirectory, id + Path.GetExtension(formFile.FileName));

using (var fs = formFile.OpenReadStream())
using (var ws = System.IO.File.Create(path))
{
await fs.CopyToAsync(ws);
}

var file = new File
{
Id = id,
MimeType = formFile.ContentType,
Name = formFile.FileName,
Path = path
};
_files.Add(file);

return file;
}

public IEnumerable<File> Files => _files;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.1.0</Version>
<Version>0.2.0</Version>
<Authors>Jannik Lassahn</Authors>
<Company />
<RepositoryUrl>https://github.com/JannikLassahn/graphql-dotnet-upload</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<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>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 6c0a8e3

Please sign in to comment.