wabbajack/Wabbajack.BuildServer/Controllers/GraphQL.cs

42 lines
1.1 KiB
C#
Raw Normal View History

2020-01-10 02:06:11 +00:00
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Types;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Wabbajack.BuildServer.GraphQL;
using Wabbajack.BuildServer.Models;
namespace Wabbajack.BuildServer.Controllers
{
[Route("graphql")]
[ApiController]
public class GraphQL : AControllerBase<GraphQL>
{
public GraphQL(ILogger<GraphQL> logger, DBContext db) : base(logger, db)
{
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] GraphQLQuery query)
{
var inputs = query.Variables.ToInputs();
2020-01-10 02:06:11 +00:00
var schema = new Schema {Query = new Query(Db), Mutation = new Mutation(Db)};
var result = await new DocumentExecuter().ExecuteAsync(_ =>
{
_.Schema = schema;
_.Query = query.Query;
_.OperationName = query.OperationName;
_.Inputs = inputs;
});
2020-01-10 02:06:11 +00:00
if (result.Errors?.Count > 0)
{
return BadRequest();
}
return Ok(result);
}
}
}