使用Relay和graphql-dotnet上传文件

时间:2016-11-11 11:34:57

标签: asp.net asp.net-mvc graphql relayjs graphql-dotnet

我试图在包含文件的中继中构建突变。我一实施此处引用的getFiles()方法:https://facebook.github.io/relay/docs/api-reference-relay-mutation.html#getfiles

Relay发送一个多部分请求,导致ASP.NET Core MVC出现415错误。

我正在寻找一个有效的例子,类似于" How would you do file uploads in a React-Relay app?"使用graphql-dotnet library

2 个答案:

答案 0 :(得分:4)

GraphQL端点不接受多部分表单mimetype,因为它不是JSON。一旦我通过RootObject将它们放入graphql-dotnet并且突变中可用的上下文,我就能够处理这些文件。让我失望的是MVC。

所以我写了一个简单的过滤器:

public class RelayResourceFilter : IResourceFilter
{
    private readonly string jsonMediaType = "application/json";

    public void OnResourceExecuted(ResourceExecutedContext context)
    {
    }

    public void OnResourceExecuting(ResourceExecutingContext context)
    {


        if (!string.Equals(MediaTypeHeaderValue.Parse(context.HttpContext.Request.ContentType).MediaType,
            this.jsonMediaType, StringComparison.OrdinalIgnoreCase))
        {
            var encoder = JavaScriptEncoder.Create();
            var variables = encoder.Encode(context.HttpContext.Request.Form["variables"]);
            var query = encoder.Encode(context.HttpContext.Request.Form["query"]);
            var body = $"{{\"query\":\"{query}\", \"variables\":\"{variables}\"}}";

            byte[] requestData = Encoding.UTF8.GetBytes(body);
            context.HttpContext.Request.Body = new MemoryStream(requestData);
            context.HttpContext.Request.ContentType = this.jsonMediaType;
        }
    }
}

注册了它:

services.AddScoped<RelayResourceFilter>();

然后在Controller中应用它:

[ServiceFilter(typeof(RelayResourceFilter))]
    public async Task<ExecutionResult> Post([FromBody]GraphQLQuery query, bool? useErrorCode)
{
var files = this.Request.HasFormContentType ? this.Request.Form.Files : null;
// ... assignment to Root Object
}

答案 1 :(得分:3)

您是否在查找如何访问服务器上解析程序中的文件时遇到问题?您可以将文件作为rootObjectuserContext

传递
// GraphQLController
var files = Request.Form.Files;
var userContext = files;

var result = await executer.ExecuteAsync(
    schema,
    rootObject,
    query,
    operationName,
    inputs,
    userContext).ConfigureAwait(false);

// Mutation type
Field<StringGraphType>(
  "uploadFile",
  arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> {Name = "fileName"}),
  resolve: context =>
  {
      var userContext = context.UserContext.As<IFormFileCollection>();

      // process files

      // return data
      return "success";
  });