Created
January 3, 2020 01:31
-
-
Save waiholiu/1831ea6ba082c71e6f49355271446d37 to your computer and use it in GitHub Desktop.
GraphQLController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using GraphQL; | |
using GraphQL.Types; | |
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Mvc; | |
namespace app | |
{ | |
[Route("[controller]")] | |
// [Authorize] | |
public class GraphQLController : Controller | |
{ | |
private readonly IDocumentExecuter _documentExecuter; | |
private readonly ISchema _schema; | |
public GraphQLController(ISchema schema, IDocumentExecuter documentExecuter) | |
{ | |
_schema = schema; | |
_documentExecuter = documentExecuter; | |
} | |
[HttpPost] | |
public async Task<IActionResult> Post([FromBody] BaseGraphQLQuery query) | |
{ | |
if (query == null) { throw new ArgumentNullException(nameof(query)); } | |
var inputs = query.Variables.ToInputs(); | |
var executionOptions = new ExecutionOptions | |
{ | |
Schema = _schema, | |
Query = query.Query, | |
Inputs = inputs | |
}; | |
var result = await _documentExecuter.ExecuteAsync(executionOptions).ConfigureAwait(false); | |
if (result.Errors?.Count > 0) | |
{ | |
return BadRequest(result); | |
} | |
return Ok(result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment