Пример #1
0
        public async Task <HttpResponseMessage> Handle(HttpRequestMessage request)
        {
            string         content    = "";
            HttpStatusCode statusCode = HttpStatusCode.OK;
            RelayRequest   queries    = await Deserializer.Deserialize(request.Content);

            var options = executionOptions(queries);

            if (options.RootContext != null && options.RootContext is RelayRootContext)
            {
                RelayRootContext ctx = options.RootContext as RelayRootContext;
                if (ctx.Files == null)
                {
                    ctx.Files = queries.Files;
                }
            }

            var result = await Task.WhenAll(
                queries.Select(q => executor.ExecuteAsync(
                                   options.Schema,
                                   options.RootContext,
                                   query: q.Query,
                                   inputs: q.Variables,
                                   userContext: options.UserContext,
                                   operationName: q.OperationName
                                   ))
                );

            if (Enumerable.Any(result, r => r.Errors?.Count > 0))
            {
                statusCode = HttpStatusCode.BadRequest;
            }

            try {
                content = writer.Write(queries.IsBatched
                    ? (object)result
                    : result[0]
                                       );
            }
            catch (Exception err)
            {
                statusCode = HttpStatusCode.InternalServerError;
                content    = $"{{ \"errors\": [\"{err.Message}\"] }}";
            }

            return(new HttpResponseMessage
            {
                StatusCode = statusCode,
                RequestMessage = request,
                Content = new StringContent(content, Encoding.UTF8, "application/json")
            });
        }
Пример #2
0
        /// <summary>
        /// This method is currently used by Soft.Framework
        /// </summary>
        /// <param name="queries"></param>
        /// <param name="configure"></param>
        /// <returns></returns>
        public async Task <RelayResponse> ExecuteAsync(RelayRequest queries, Action <ExecutionOptions, IEnumerable <HttpFile> > configure)
        {
            var results = await Task.WhenAll(
                queries.Select(q => _executer.ExecuteAsync(options =>
            {
                options.Query         = q.Query;
                options.OperationName = q.OperationName;
                options.Inputs        = q.Variables;

                configure(options, queries.Files);
            }))
                );

            return(new RelayResponse
            {
                Writer = _writer,
                IsBatched = queries.IsBatched,
                Results = results
            });
        }
Пример #3
0
        private static RelayRequest DeserializeFormData(Stream body)
        {
            var form = new MultipartFormDataParser(body);

            var req = new RelayRequest()
            {
                Files = form.Files.Select(f => new HttpFile {
                    ContentDisposition = f.ContentDisposition,
                    ContentType        = f.ContentType,
                    Data     = f.Data,
                    FileName = f.FileName,
                    Name     = f.Name
                })
            };

            req.Add(new RelayQuery {
                Query     = form.Parameters.Find(p => p.Name == "query").Data,
                Variables = form.Parameters.Find(p => p.Name == "variables").Data.ToInputs(),
            });

            return(req);
        }
Пример #4
0
        public static async Task <RelayRequest> Deserialize(HttpContent content)
        {
            RelayRequest queries   = null;
            string       mediaType = content.Headers.ContentType.MediaType;

            switch (mediaType)
            {
            case "multipart/form-data":
                queries = await DeserializeFormData(content);

                break;

            case "application/json":
                queries = await DeserializeJson(content);

                break;

            default:
                throw new ArgumentOutOfRangeException($"Unknown media type: {mediaType}. Cannot deserialize the Http request");
            }

            return(queries);
        }