-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphQLBananaCakePopIDEEndpoint.cs
47 lines (41 loc) · 2.15 KB
/
GraphQLBananaCakePopIDEEndpoint.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
using System;
using System.Threading.Tasks;
using HotChocolate.AzureFunctionsProxy;
using HotChocolate.AzureFunctionsProxy.IsolatedProcess;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace StarWars.AzureFunctionsIsolatedProcess
{
/// <summary>
/// AzureFunction Endpoint for the Star Wars GraphQL Schema queries and Banana Cake Pop binary/asset handling
/// NOTE: This class is not marked as static so that .Net Core DI handles injecting the Executor Proxy for us.
/// </summary>
public class GraphQLBananaCakePopEndpoint
{
private readonly IGraphQLAzureFunctionsExecutorProxy _graphqlExecutorProxy;
public GraphQLBananaCakePopEndpoint(IGraphQLAzureFunctionsExecutorProxy graphqlExecutorProxy)
{
_graphqlExecutorProxy = graphqlExecutorProxy;
}
[Function(nameof(GraphQLBananaCakePopEndpoint))]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "graphql/bcp/{*path}")] HttpRequestData req,
FunctionContext executionContext
)
{
var logger = executionContext.GetLogger<GraphQLBananaCakePopEndpoint>();
logger.LogInformation("C# GraphQL Request processing via Serverless AzureFunctions (Isolated Process)...");
//SECURE this endpoint against actual Data Queries
// This is useful for exposing the GraphQL IDE (Banana Cake Pop) anonymously, but keeping the actual GraphQL data endpoint
// secured with AzureFunction token security and/or other authorization approach.
if (HttpMethods.IsPost(req.Method) || (HttpMethods.IsGet(req.Method) && !string.IsNullOrWhiteSpace(req.GetQueryStringParam("query"))))
{
return req.CreateBadRequestErrorMessageResponse("POST or GET GraphQL queries are invalid for the GraphQL IDE endpoint.");
}
var response = await _graphqlExecutorProxy.ExecuteFunctionsQueryAsync(req, logger).ConfigureAwait(false);
return response;
}
}
}