|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.IO; |
4 |
| -using System.Linq; |
5 |
| -using System.Threading.Tasks; |
6 |
| -using Microsoft.AspNetCore; |
7 |
| -using Microsoft.AspNetCore.Hosting; |
8 |
| -using Microsoft.Extensions.Configuration; |
9 |
| -using Microsoft.Extensions.Logging; |
10 |
| -using Ocelot.Middleware; |
11 |
| -using Ocelot.DependencyInjection; |
12 |
| -using GraphQL.Types; |
13 |
| -using GraphQL; |
14 |
| -using Ocelot.Requester; |
15 |
| -using Ocelot.Responses; |
16 |
| -using System.Net.Http; |
17 |
| -using System.Net; |
18 |
| -using Microsoft.Extensions.DependencyInjection; |
19 |
| -using System.Threading; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNetCore; |
| 7 | +using Microsoft.AspNetCore.Hosting; |
| 8 | +using Microsoft.Extensions.Configuration; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Ocelot.Middleware; |
| 11 | +using Ocelot.DependencyInjection; |
| 12 | +using GraphQL.Types; |
| 13 | +using GraphQL; |
| 14 | +using Ocelot.Requester; |
| 15 | +using Ocelot.Responses; |
| 16 | +using System.Net.Http; |
| 17 | +using System.Net; |
| 18 | +using Microsoft.Extensions.DependencyInjection; |
| 19 | +using System.Threading; |
| 20 | + |
| 21 | +namespace OcelotGraphQL |
| 22 | +{ |
| 23 | + public class Hero |
| 24 | + { |
| 25 | + public int Id { get; set; } |
| 26 | + public string Name { get; set; } |
| 27 | + } |
| 28 | + |
| 29 | + public class Query |
| 30 | + { |
| 31 | + private readonly List<Hero> _heroes = new List<Hero> |
| 32 | + { |
| 33 | + new Hero { Id = 1, Name = "R2-D2" }, |
| 34 | + new Hero { Id = 2, Name = "Batman" }, |
| 35 | + new Hero { Id = 3, Name = "Wonder Woman" }, |
| 36 | + new Hero { Id = 4, Name = "Tom Pallister" } |
| 37 | + }; |
| 38 | + |
| 39 | + [GraphQLMetadata("hero")] |
| 40 | + public Hero GetHero(int id) |
| 41 | + { |
| 42 | + return _heroes.FirstOrDefault(x => x.Id == id); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public class GraphQlDelegatingHandler : DelegatingHandler |
| 47 | + { |
| 48 | + //private readonly ISchema _schema; |
| 49 | + private readonly IDocumentExecuter _executer; |
| 50 | + private readonly IDocumentWriter _writer; |
| 51 | + |
| 52 | + public GraphQlDelegatingHandler(IDocumentExecuter executer, IDocumentWriter writer) |
| 53 | + { |
| 54 | + _executer = executer; |
| 55 | + _writer = writer; |
| 56 | + } |
| 57 | + |
| 58 | + protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 59 | + { |
| 60 | + //try get query from body, could check http method :) |
| 61 | + var query = await request.Content.ReadAsStringAsync(); |
| 62 | + |
| 63 | + //if not body try query string, dont hack like this in real world.. |
| 64 | + if (query.Length == 0) |
| 65 | + { |
| 66 | + var decoded = WebUtility.UrlDecode(request.RequestUri.Query); |
| 67 | + query = decoded.Replace("?query=", ""); |
| 68 | + } |
20 | 69 |
|
21 |
| -namespace OcelotGraphQL |
22 |
| -{ |
23 |
| - public class Hero |
24 |
| - { |
25 |
| - public int Id { get; set; } |
26 |
| - public string Name { get; set; } |
27 |
| - } |
28 |
| - |
29 |
| - public class Query |
30 |
| - { |
31 |
| - private readonly List<Hero> _heroes = new List<Hero> |
32 |
| - { |
33 |
| - new Hero { Id = 1, Name = "R2-D2" }, |
34 |
| - new Hero { Id = 2, Name = "Batman" }, |
35 |
| - new Hero { Id = 3, Name = "Wonder Woman" }, |
36 |
| - new Hero { Id = 4, Name = "Tom Pallister" } |
37 |
| - }; |
38 |
| - |
39 |
| - [GraphQLMetadata("hero")] |
40 |
| - public Hero GetHero(int id) |
41 |
| - { |
42 |
| - return _heroes.FirstOrDefault(x => x.Id == id); |
43 |
| - } |
44 |
| - } |
45 |
| - |
46 |
| - public class GraphQlDelegatingHandler : DelegatingHandler |
47 |
| - { |
48 |
| - private readonly ISchema _schema; |
49 |
| - |
50 |
| - public GraphQlDelegatingHandler(ISchema schema) |
51 |
| - { |
52 |
| - _schema = schema; |
53 |
| - } |
54 |
| - |
55 |
| - protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
56 |
| - { |
57 |
| - //try get query from body, could check http method :) |
58 |
| - var query = await request.Content.ReadAsStringAsync(); |
59 |
| - |
60 |
| - //if not body try query string, dont hack like this in real world.. |
61 |
| - if (query.Length == 0) |
62 |
| - { |
63 |
| - var decoded = WebUtility.UrlDecode(request.RequestUri.Query); |
64 |
| - query = decoded.Replace("?query=", ""); |
65 |
| - } |
66 |
| - |
67 |
| - var result = _schema.Execute(_ => |
68 |
| - { |
69 |
| - _.Query = query; |
| 70 | + var result = await _executer.ExecuteAsync(_ => |
| 71 | + { |
| 72 | + _.Query = query; |
70 | 73 | });
|
71 | 74 |
|
72 |
| - //maybe check for errors and headers etc in real world? |
73 |
| - var response = new HttpResponseMessage(HttpStatusCode.OK) |
74 |
| - { |
75 |
| - Content = new StringContent(result) |
76 |
| - }; |
77 |
| - |
78 |
| - //ocelot will treat this like any other http request... |
79 |
| - return response; |
80 |
| - } |
81 |
| - } |
82 |
| - |
83 |
| - public class Program |
84 |
| - { |
85 |
| - public static void Main() |
86 |
| - { |
87 |
| - var schema = Schema.For(@" |
88 |
| - type Hero { |
89 |
| - id: Int |
90 |
| - name: String |
91 |
| - } |
92 |
| -
|
93 |
| - type Query { |
94 |
| - hero(id: Int): Hero |
95 |
| - } |
96 |
| - ", _ => |
97 |
| - { |
98 |
| - _.Types.Include<Query>(); |
99 |
| - }); |
| 75 | + var responseBody = await _writer.WriteToStringAsync(result); |
100 | 76 |
|
101 |
| - new WebHostBuilder() |
102 |
| - .UseKestrel() |
103 |
| - .UseContentRoot(Directory.GetCurrentDirectory()) |
104 |
| - .ConfigureAppConfiguration((hostingContext, config) => |
105 |
| - { |
106 |
| - config |
107 |
| - .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) |
108 |
| - .AddJsonFile("appsettings.json", true, true) |
109 |
| - .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) |
110 |
| - .AddJsonFile("ocelot.json", false, false) |
111 |
| - .AddEnvironmentVariables(); |
112 |
| - }) |
113 |
| - .ConfigureServices(s => |
114 |
| - { |
115 |
| - s.AddSingleton<ISchema>(schema); |
116 |
| - s.AddOcelot() |
117 |
| - .AddDelegatingHandler<GraphQlDelegatingHandler>(); |
118 |
| - }) |
119 |
| - .ConfigureLogging((hostingContext, logging) => |
120 |
| - { |
121 |
| - logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); |
122 |
| - logging.AddConsole(); |
123 |
| - }) |
124 |
| - .UseIISIntegration() |
125 |
| - .Configure(app => |
126 |
| - { |
127 |
| - app.UseOcelot().Wait(); |
128 |
| - }) |
129 |
| - .Build() |
130 |
| - .Run(); |
131 |
| - } |
132 |
| - } |
133 |
| -} |
| 77 | + //maybe check for errors and headers etc in real world? |
| 78 | + var response = new HttpResponseMessage(HttpStatusCode.OK) |
| 79 | + { |
| 80 | + Content = new StringContent(responseBody) |
| 81 | + }; |
| 82 | + |
| 83 | + //ocelot will treat this like any other http request... |
| 84 | + return response; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public class Program |
| 89 | + { |
| 90 | + public static void Main() |
| 91 | + { |
| 92 | + var schema = Schema.For(@" |
| 93 | + type Hero { |
| 94 | + id: Int |
| 95 | + name: String |
| 96 | + } |
| 97 | +
|
| 98 | + type Query { |
| 99 | + hero(id: Int): Hero |
| 100 | + } |
| 101 | + ", _ => |
| 102 | + { |
| 103 | + _.Types.Include<Query>(); |
| 104 | + }); |
| 105 | + |
| 106 | + new WebHostBuilder() |
| 107 | + .UseKestrel() |
| 108 | + .UseContentRoot(Directory.GetCurrentDirectory()) |
| 109 | + .ConfigureAppConfiguration((hostingContext, config) => |
| 110 | + { |
| 111 | + config |
| 112 | + .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) |
| 113 | + .AddJsonFile("appsettings.json", true, true) |
| 114 | + .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) |
| 115 | + .AddJsonFile("ocelot.json", false, false) |
| 116 | + .AddEnvironmentVariables(); |
| 117 | + }) |
| 118 | + .ConfigureServices(s => |
| 119 | + { |
| 120 | + s.AddSingleton<ISchema>(schema); |
| 121 | + s.AddOcelot() |
| 122 | + .AddDelegatingHandler<GraphQlDelegatingHandler>(); |
| 123 | + }) |
| 124 | + .ConfigureLogging((hostingContext, logging) => |
| 125 | + { |
| 126 | + logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); |
| 127 | + logging.AddConsole(); |
| 128 | + }) |
| 129 | + .UseIISIntegration() |
| 130 | + .Configure(app => |
| 131 | + { |
| 132 | + app.UseOcelot().Wait(); |
| 133 | + }) |
| 134 | + .Build() |
| 135 | + .Run(); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments