mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-20 01:02:50 +08:00
update nuget packages and graphql example
This commit is contained in:
parent
fdad2bb302
commit
b356539cbc
@ -12,6 +12,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Ocelot" Version="14.0.9" />
|
<PackageReference Include="Ocelot" Version="14.0.9" />
|
||||||
<PackageReference Include="GraphQL" Version="2.4.0" />
|
<PackageReference Include="GraphQL" Version="3.1.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -1,133 +1,138 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore;
|
using Microsoft.AspNetCore;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Ocelot.Middleware;
|
using Ocelot.Middleware;
|
||||||
using Ocelot.DependencyInjection;
|
using Ocelot.DependencyInjection;
|
||||||
using GraphQL.Types;
|
using GraphQL.Types;
|
||||||
using GraphQL;
|
using GraphQL;
|
||||||
using Ocelot.Requester;
|
using Ocelot.Requester;
|
||||||
using Ocelot.Responses;
|
using Ocelot.Responses;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace OcelotGraphQL
|
||||||
|
{
|
||||||
|
public class Hero
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Query
|
||||||
|
{
|
||||||
|
private readonly List<Hero> _heroes = new List<Hero>
|
||||||
|
{
|
||||||
|
new Hero { Id = 1, Name = "R2-D2" },
|
||||||
|
new Hero { Id = 2, Name = "Batman" },
|
||||||
|
new Hero { Id = 3, Name = "Wonder Woman" },
|
||||||
|
new Hero { Id = 4, Name = "Tom Pallister" }
|
||||||
|
};
|
||||||
|
|
||||||
|
[GraphQLMetadata("hero")]
|
||||||
|
public Hero GetHero(int id)
|
||||||
|
{
|
||||||
|
return _heroes.FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GraphQlDelegatingHandler : DelegatingHandler
|
||||||
|
{
|
||||||
|
//private readonly ISchema _schema;
|
||||||
|
private readonly IDocumentExecuter _executer;
|
||||||
|
private readonly IDocumentWriter _writer;
|
||||||
|
|
||||||
|
public GraphQlDelegatingHandler(IDocumentExecuter executer, IDocumentWriter writer)
|
||||||
|
{
|
||||||
|
_executer = executer;
|
||||||
|
_writer = writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
//try get query from body, could check http method :)
|
||||||
|
var query = await request.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
//if not body try query string, dont hack like this in real world..
|
||||||
|
if (query.Length == 0)
|
||||||
|
{
|
||||||
|
var decoded = WebUtility.UrlDecode(request.RequestUri.Query);
|
||||||
|
query = decoded.Replace("?query=", "");
|
||||||
|
}
|
||||||
|
|
||||||
namespace OcelotGraphQL
|
var result = await _executer.ExecuteAsync(_ =>
|
||||||
{
|
{
|
||||||
public class Hero
|
_.Query = query;
|
||||||
{
|
|
||||||
public int Id { get; set; }
|
|
||||||
public string Name { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Query
|
|
||||||
{
|
|
||||||
private readonly List<Hero> _heroes = new List<Hero>
|
|
||||||
{
|
|
||||||
new Hero { Id = 1, Name = "R2-D2" },
|
|
||||||
new Hero { Id = 2, Name = "Batman" },
|
|
||||||
new Hero { Id = 3, Name = "Wonder Woman" },
|
|
||||||
new Hero { Id = 4, Name = "Tom Pallister" }
|
|
||||||
};
|
|
||||||
|
|
||||||
[GraphQLMetadata("hero")]
|
|
||||||
public Hero GetHero(int id)
|
|
||||||
{
|
|
||||||
return _heroes.FirstOrDefault(x => x.Id == id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class GraphQlDelegatingHandler : DelegatingHandler
|
|
||||||
{
|
|
||||||
private readonly ISchema _schema;
|
|
||||||
|
|
||||||
public GraphQlDelegatingHandler(ISchema schema)
|
|
||||||
{
|
|
||||||
_schema = schema;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
//try get query from body, could check http method :)
|
|
||||||
var query = await request.Content.ReadAsStringAsync();
|
|
||||||
|
|
||||||
//if not body try query string, dont hack like this in real world..
|
|
||||||
if (query.Length == 0)
|
|
||||||
{
|
|
||||||
var decoded = WebUtility.UrlDecode(request.RequestUri.Query);
|
|
||||||
query = decoded.Replace("?query=", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = _schema.Execute(_ =>
|
|
||||||
{
|
|
||||||
_.Query = query;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//maybe check for errors and headers etc in real world?
|
var responseBody = await _writer.WriteToStringAsync(result);
|
||||||
var response = new HttpResponseMessage(HttpStatusCode.OK)
|
|
||||||
{
|
|
||||||
Content = new StringContent(result)
|
|
||||||
};
|
|
||||||
|
|
||||||
//ocelot will treat this like any other http request...
|
//maybe check for errors and headers etc in real world?
|
||||||
return response;
|
var response = new HttpResponseMessage(HttpStatusCode.OK)
|
||||||
}
|
{
|
||||||
}
|
Content = new StringContent(responseBody)
|
||||||
|
};
|
||||||
public class Program
|
|
||||||
{
|
//ocelot will treat this like any other http request...
|
||||||
public static void Main()
|
return response;
|
||||||
{
|
}
|
||||||
var schema = Schema.For(@"
|
}
|
||||||
type Hero {
|
|
||||||
id: Int
|
public class Program
|
||||||
name: String
|
{
|
||||||
}
|
public static void Main()
|
||||||
|
{
|
||||||
type Query {
|
var schema = Schema.For(@"
|
||||||
hero(id: Int): Hero
|
type Hero {
|
||||||
}
|
id: Int
|
||||||
", _ =>
|
name: String
|
||||||
{
|
}
|
||||||
_.Types.Include<Query>();
|
|
||||||
});
|
type Query {
|
||||||
|
hero(id: Int): Hero
|
||||||
new WebHostBuilder()
|
}
|
||||||
.UseKestrel()
|
", _ =>
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
{
|
||||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
_.Types.Include<Query>();
|
||||||
{
|
});
|
||||||
config
|
|
||||||
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
|
new WebHostBuilder()
|
||||||
.AddJsonFile("appsettings.json", true, true)
|
.UseKestrel()
|
||||||
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
.AddJsonFile("ocelot.json", false, false)
|
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||||
.AddEnvironmentVariables();
|
{
|
||||||
})
|
config
|
||||||
.ConfigureServices(s =>
|
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
|
||||||
{
|
.AddJsonFile("appsettings.json", true, true)
|
||||||
s.AddSingleton<ISchema>(schema);
|
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
|
||||||
s.AddOcelot()
|
.AddJsonFile("ocelot.json", false, false)
|
||||||
.AddDelegatingHandler<GraphQlDelegatingHandler>();
|
.AddEnvironmentVariables();
|
||||||
})
|
})
|
||||||
.ConfigureLogging((hostingContext, logging) =>
|
.ConfigureServices(s =>
|
||||||
{
|
{
|
||||||
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
s.AddSingleton<ISchema>(schema);
|
||||||
logging.AddConsole();
|
s.AddOcelot()
|
||||||
})
|
.AddDelegatingHandler<GraphQlDelegatingHandler>();
|
||||||
.UseIISIntegration()
|
})
|
||||||
.Configure(app =>
|
.ConfigureLogging((hostingContext, logging) =>
|
||||||
{
|
{
|
||||||
app.UseOcelot().Wait();
|
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
||||||
})
|
logging.AddConsole();
|
||||||
.Build()
|
})
|
||||||
.Run();
|
.UseIISIntegration()
|
||||||
}
|
.Configure(app =>
|
||||||
}
|
{
|
||||||
}
|
app.UseOcelot().Wait();
|
||||||
|
})
|
||||||
|
.Build()
|
||||||
|
.Run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
<ProjectReference Include="..\Ocelot\Ocelot.csproj" />
|
<ProjectReference Include="..\Ocelot\Ocelot.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Consul" Version="0.7.2.6" />
|
<PackageReference Include="Consul" Version="1.6.1.1" />
|
||||||
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164">
|
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FluentValidation" Version="8.6.2" />
|
<PackageReference Include="FluentValidation" Version="9.3.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="3.1.3" />
|
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="3.1.3" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="3.1.3">
|
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="3.1.3">
|
||||||
<NoWarn>NU1701</NoWarn>
|
<NoWarn>NU1701</NoWarn>
|
||||||
|
@ -1,22 +1,22 @@
|
|||||||
using Ocelot.Errors;
|
using Ocelot.Errors;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Ocelot.Responses
|
namespace Ocelot.Responses
|
||||||
{
|
{
|
||||||
public abstract class Response
|
public abstract class Response
|
||||||
{
|
{
|
||||||
protected Response()
|
protected Response()
|
||||||
{
|
{
|
||||||
Errors = new List<Error>();
|
Errors = new List<Error>();
|
||||||
}
|
|
||||||
|
|
||||||
protected Response(List<Error> errors)
|
|
||||||
{
|
|
||||||
Errors = errors ?? new List<Error>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Error> Errors { get; }
|
protected Response(List<Error> errors)
|
||||||
|
{
|
||||||
public bool IsError => Errors.Count > 0;
|
Errors = errors ?? new List<Error>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Error> Errors { get; }
|
||||||
|
|
||||||
|
public bool IsError => Errors.Count > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,7 @@
|
|||||||
<PackageReference Include="Butterfly.Client.AspNetCore" Version="0.0.8" />
|
<PackageReference Include="Butterfly.Client.AspNetCore" Version="0.0.8" />
|
||||||
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
|
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
|
||||||
<PackageReference Include="IdentityServer4" Version="3.1.1" />
|
<PackageReference Include="IdentityServer4" Version="3.1.1" />
|
||||||
<PackageReference Include="Consul" Version="0.7.2.6" />
|
<PackageReference Include="Consul" Version="1.6.1.1" />
|
||||||
<PackageReference Include="Rafty" Version="0.4.4" />
|
<PackageReference Include="Rafty" Version="0.4.4" />
|
||||||
<PackageReference Include="CacheManager.Microsoft.Extensions.Logging" Version="2.0.0-beta-1629" />
|
<PackageReference Include="CacheManager.Microsoft.Extensions.Logging" Version="2.0.0-beta-1629" />
|
||||||
<PackageReference Include="CacheManager.Serialization.Json" Version="2.0.0-beta-1629" />
|
<PackageReference Include="CacheManager.Serialization.Json" Version="2.0.0-beta-1629" />
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
|
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
|
||||||
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164">
|
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
@ -75,7 +75,7 @@
|
|||||||
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
|
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
|
||||||
<PackageReference Include="IdentityServer4" Version="3.1.1" />
|
<PackageReference Include="IdentityServer4" Version="3.1.1" />
|
||||||
<PackageReference Include="Steeltoe.Discovery.ClientCore" Version="2.4.2" />
|
<PackageReference Include="Steeltoe.Discovery.ClientCore" Version="2.4.2" />
|
||||||
<PackageReference Include="Consul" Version="0.7.2.6" />
|
<PackageReference Include="Consul" Version="1.6.1.1" />
|
||||||
<PackageReference Include="CacheManager.Core" Version="2.0.0-beta-1629" />
|
<PackageReference Include="CacheManager.Core" Version="2.0.0-beta-1629" />
|
||||||
<PackageReference Include="CacheManager.Microsoft.Extensions.Configuration" Version="2.0.0-beta-1629" />
|
<PackageReference Include="CacheManager.Microsoft.Extensions.Configuration" Version="2.0.0-beta-1629" />
|
||||||
<PackageReference Include="CacheManager.Microsoft.Extensions.Logging" Version="2.0.0-beta-1629" />
|
<PackageReference Include="CacheManager.Microsoft.Extensions.Logging" Version="2.0.0-beta-1629" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user