-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathGraphQlController.cs
60 lines (54 loc) · 1.57 KB
/
GraphQlController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using GraphQL.SystemTextJson;
using GraphQL.Types;
using Microsoft.AspNetCore.Mvc;
#region GraphQlController
[Route("[controller]")]
[ApiController]
public class GraphQlController(ISchema schema, IDocumentExecuter executer) :
Controller
{
static GraphQLSerializer writer = new(true);
[HttpGet]
public Task Get(
[FromQuery] string query,
[FromQuery] string? variables,
[FromQuery] string? operationName,
Cancel cancel)
{
var inputs = variables.ToInputs();
return Execute(query, operationName, inputs, cancel);
}
public class GraphQLQuery
{
public string? OperationName { get; set; }
public string Query { get; set; } = null!;
public string? Variables { get; set; }
}
[HttpPost]
public Task Post(
[FromBody]GraphQLQuery query,
Cancel cancel)
{
var inputs = query.Variables.ToInputs();
return Execute(query.Query, query.OperationName, inputs, cancel);
}
async Task Execute(string query,
string? operationName,
Inputs? variables,
Cancel cancel)
{
var options = new ExecutionOptions
{
Schema = schema,
Query = query,
OperationName = operationName,
Variables = variables,
CancellationToken = cancel,
ThrowOnUnhandledException = true,
EnableMetrics = true,
};
var executeAsync = await executer.ExecuteAsync(options);
await writer.WriteAsync(Response.Body, executeAsync, cancel);
}
}
#endregion