public async Task GetHero_By_Episode() { // arrange TestServer httpServer = ServerFactory.Create( services => services.AddStarWars(), app => app.UseGraphQL()); HttpClient httpClient = httpServer.CreateClient(); httpClient.BaseAddress = new Uri("http://localhost:5000"); var clientFactory = new Mock <IHttpClientFactory>(); clientFactory.Setup(t => t.CreateClient(It.IsAny <string>())).Returns(httpClient); var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton <IHttpClientFactory>(clientFactory.Object); serviceCollection.AddDefaultScalarSerializers(); serviceCollection.AddStarWarsClient(); var services = serviceCollection.BuildServiceProvider(); // act IStarWarsClient client = services.GetRequiredService <IStarWarsClient>(); IOperationResult <IGetHero> result = await client.GetHeroAsync(Episode.Empire); // assert result.MatchSnapshot(); }
public async Task GetHero_By_Episode() { // arrange IStarWarsClient client = Services.GetRequiredService <IStarWarsClient>(); // act IOperationResult <IGetHero> result = await client.GetHeroAsync(Episode.Empire); // assert result.MatchSnapshot(); }
static async Task Main(string[] args) { var serviceCollection = new ServiceCollection(); serviceCollection.AddHttpClient( "StarWarsClient", c => c.BaseAddress = new Uri("http://localhost:5000/graphql")); serviceCollection.AddStarWarsClient(); IServiceProvider services = serviceCollection.BuildServiceProvider(); IStarWarsClient client = services.GetRequiredService <IStarWarsClient>(); IOperationResult <IGetHero> result = await client.GetHeroAsync(Episode.NewHope); Console.WriteLine(((ISomeDroid)result.Data.Hero).Name); result = await client.GetHeroAsync(Episode.Empire); Console.WriteLine(((ISomeHuman)result.Data.Hero).Name); }
public async Task GetHero_By_Episode() { // arrange using IWebHost host = TestServerHelper.CreateServer(out int port); IServiceProvider services = CreateServices( "StarWarsClient", port, s => s.AddStarWarsClient()); IStarWarsClient client = services.GetRequiredService <IStarWarsClient>(); // act IOperationResult <IGetHero> result = await client.GetHeroAsync(Episode.Empire); // assert result.MatchSnapshot(); }
private static void FetchHeroes(Episode episode) { var result = _starWarsClient.GetHeroAsync(episode).Result; var hero = result.Data?.Hero; switch (hero) { case ISomeHuman someHuman: Console.WriteLine($"Name of some human: {someHuman.Name}"); break; case ISomeDroid someDroid: Console.WriteLine($"Name of some droid: {someDroid.Name}"); break; } }