From 9a5502883b8b77d3b9689155ed61bc7feb680fe4 Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Mon, 30 Jan 2023 23:30:17 -0500 Subject: [PATCH 01/12] add multiple operations and structure things --- .../Example/GraphQL/Operations.cs | 72 +++++++++++++++++++ .../Example/GraphQL/Queries.cs | 55 ++++++++++++++ .../{GraphQL.cs => GraphQL/Schemas.cs} | 19 +---- src/AspNetCoreMulti/Example/GraphQL/Types.cs | 28 ++++++++ src/AspNetCoreMulti/Example/Models.cs | 17 +++++ src/AspNetCoreMulti/Example/Program.cs | 22 ++++-- .../Example/Repositories/CatRepository.cs | 17 +++++ .../Repositories/DogImageDetailsRepository.cs | 23 ++++++ .../Example/Repositories/DogRepository.cs | 17 +++++ src/AspNetCoreMulti/Example/Startup.cs | 23 ++++++ 10 files changed, 271 insertions(+), 22 deletions(-) create mode 100644 src/AspNetCoreMulti/Example/GraphQL/Operations.cs create mode 100644 src/AspNetCoreMulti/Example/GraphQL/Queries.cs rename src/AspNetCoreMulti/Example/{GraphQL.cs => GraphQL/Schemas.cs} (51%) create mode 100644 src/AspNetCoreMulti/Example/GraphQL/Types.cs create mode 100644 src/AspNetCoreMulti/Example/Models.cs create mode 100644 src/AspNetCoreMulti/Example/Repositories/CatRepository.cs create mode 100644 src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs create mode 100644 src/AspNetCoreMulti/Example/Repositories/DogRepository.cs diff --git a/src/AspNetCoreMulti/Example/GraphQL/Operations.cs b/src/AspNetCoreMulti/Example/GraphQL/Operations.cs new file mode 100644 index 0000000..51a2b51 --- /dev/null +++ b/src/AspNetCoreMulti/Example/GraphQL/Operations.cs @@ -0,0 +1,72 @@ +using Example.Repositories; +using GraphQL.Resolvers; +using GraphQL.Types; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace Example.GraphQL +{ + public interface IOperation + { + IEnumerable RegisterFields(); + } + + public interface IDogOperation : IOperation { } + + public interface ICatOperation : IOperation { } + + public class DogOperation : ObjectGraphType, IDogOperation + { + private readonly IServiceProvider _serviceProvider; + + public DogOperation(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public IEnumerable RegisterFields() + { + var fields = new List + { + Field("say", resolve: context => "woof woof woof"), + GetBreedListField(), + GetImageDetailsField() + }; + + return fields; + } + + private IFieldType GetBreedListField() + { + return Field>>>("dogBreeds", resolve: context => + { + using var scope = _serviceProvider.CreateScope(); + var dogRepository = scope.ServiceProvider.GetRequiredService(); + var dogs = dogRepository.GetDogs(); + return dogs; + }); + } + + private IFieldType GetImageDetailsField() + { + return FieldAsync>("dogImageDetails", resolve: async context => + { + using var scope = _serviceProvider.CreateScope(); + var imageDetailsRepository = scope.ServiceProvider.GetRequiredService(); + var imageDetails = await imageDetailsRepository.GetDogImageDetails(); + return imageDetails; + }); + } + } + + public class CatSayOperation : ObjectGraphType, ICatOperation + { + public IEnumerable RegisterFields() + { + return new List { Field("say", resolve: context => "meow meow meow") }; + } + } +} diff --git a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs new file mode 100644 index 0000000..00d16a6 --- /dev/null +++ b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs @@ -0,0 +1,55 @@ +using GraphQL.Types; +using System.Collections; +using System.Collections.Generic; + +namespace Example.GraphQL +{ + public class Queries + { + //public class DogQuery : ObjectGraphType + //{ + // public DogQuery() + // { + // Field("say", resolve: context => "woof woof woof"); + // } + //} + + //public class CatQuery : ObjectGraphType + //{ + // public CatQuery() + // { + // Field("say", resolve: context => "meow meow meow"); + // } + //} + + public class DogQuery : ObjectGraphType + { + public DogQuery(IEnumerable dogOperations) + { + foreach(var dogOperation in dogOperations) + { + var fields = dogOperation.RegisterFields(); + foreach(var field in fields) + { + AddField((FieldType)field); + } + } + } + } + + public class CatQuery : ObjectGraphType + { + public CatQuery(IEnumerable catOperations) + { + foreach(var catOperation in catOperations) + { + var fields = catOperation.RegisterFields(); + foreach (var field in fields) + { + AddField((FieldType)field); + } + } + } + } + } +} diff --git a/src/AspNetCoreMulti/Example/GraphQL.cs b/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs similarity index 51% rename from src/AspNetCoreMulti/Example/GraphQL.cs rename to src/AspNetCoreMulti/Example/GraphQL/Schemas.cs index f58fc92..b611995 100644 --- a/src/AspNetCoreMulti/Example/GraphQL.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs @@ -1,7 +1,8 @@ using GraphQL.Types; using System; +using static Example.GraphQL.Queries; -namespace Example +namespace Example.GraphQL { public class DogSchema : Schema { @@ -12,14 +13,6 @@ public DogSchema(IServiceProvider provider, DogQuery query) } } - public class DogQuery : ObjectGraphType - { - public DogQuery() - { - Field("say", resolve: context => "woof woof woof"); - } - } - public class CatSchema : Schema { public CatSchema(IServiceProvider provider, CatQuery query) @@ -28,12 +21,4 @@ public CatSchema(IServiceProvider provider, CatQuery query) Query = query; } } - - public class CatQuery : ObjectGraphType - { - public CatQuery() - { - Field("say", resolve: context => "meow meow meow"); - } - } } diff --git a/src/AspNetCoreMulti/Example/GraphQL/Types.cs b/src/AspNetCoreMulti/Example/GraphQL/Types.cs new file mode 100644 index 0000000..19fa652 --- /dev/null +++ b/src/AspNetCoreMulti/Example/GraphQL/Types.cs @@ -0,0 +1,28 @@ +using GraphQL.Types; + +namespace Example.GraphQL +{ + public class DogType : ObjectGraphType + { + public DogType() + { + Field(x => x.Breed); + } + } + + public class CatType : ObjectGraphType + { + public CatType() + { + Field(x => x.Breed); + } + } + + public class ImageDetailsType : ObjectGraphType + { + public ImageDetailsType() + { + Field(x => x.Url); + } + } +} diff --git a/src/AspNetCoreMulti/Example/Models.cs b/src/AspNetCoreMulti/Example/Models.cs new file mode 100644 index 0000000..3918997 --- /dev/null +++ b/src/AspNetCoreMulti/Example/Models.cs @@ -0,0 +1,17 @@ +namespace Example +{ + public class Dog + { + public string Breed { get; set; } + } + + public class Cat + { + public string Breed { get; set; } + } + + public class ImageDetails + { + public string Url { get; set; } + } +} diff --git a/src/AspNetCoreMulti/Example/Program.cs b/src/AspNetCoreMulti/Example/Program.cs index e58a459..c003357 100644 --- a/src/AspNetCoreMulti/Example/Program.cs +++ b/src/AspNetCoreMulti/Example/Program.cs @@ -1,15 +1,27 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; +using System; using System.Threading.Tasks; namespace Example { public class Program { - public static Task Main(string[] args) => Host - .CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(builder => builder.UseStartup()) - .Build() - .RunAsync(); + public static Task Main(string[] args) + { + try + { + return Host + .CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(builder => builder.UseStartup()) + .Build() + .RunAsync(); + } + catch (System.Exception ex) + { + Console.WriteLine(ex); + return Task.FromResult(0); + } + } } } diff --git a/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs b/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs new file mode 100644 index 0000000..1c07747 --- /dev/null +++ b/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.Linq; + +namespace Example.Repositories +{ + public class CatRepository + { + private static readonly List Cats = new() + { + new Cat{ Breed = "Abyssinian" }, + new Cat{ Breed = "American Bobtail" }, + new Cat{ Breed = "Burmese" } + }; + + public IEnumerable GetCats() => Cats.AsEnumerable(); + } +} diff --git a/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs b/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs new file mode 100644 index 0000000..f214a60 --- /dev/null +++ b/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs @@ -0,0 +1,23 @@ +using System.Net.Http; +using System.Threading.Tasks; + +namespace Example.Repositories +{ + public class DogImageDetailsRepository + { + private readonly IHttpClientFactory _httpClientFactory; + + public DogImageDetailsRepository(IHttpClientFactory httpClientFactory) + { + _httpClientFactory = httpClientFactory; + } + + public async Task GetDogImageDetails() + { + var client = _httpClientFactory.CreateClient("DogsApi"); + var result = await client.GetStringAsync("api/breeds/image/random"); + + return new ImageDetails { Url = result }; + } + } +} diff --git a/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs b/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs new file mode 100644 index 0000000..b767c03 --- /dev/null +++ b/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.Linq; + +namespace Example.Repositories +{ + public class DogRepository + { + private static readonly List Dogs = new() + { + new Dog{ Breed = "Doberman" }, + new Dog{ Breed = "Pit Bull" }, + new Dog{ Breed = "German Shepard" } + }; + + public IEnumerable GetDogs() => Dogs.AsEnumerable(); + } +} diff --git a/src/AspNetCoreMulti/Example/Startup.cs b/src/AspNetCoreMulti/Example/Startup.cs index f23007f..92aec44 100644 --- a/src/AspNetCoreMulti/Example/Startup.cs +++ b/src/AspNetCoreMulti/Example/Startup.cs @@ -1,3 +1,5 @@ +using Example.GraphQL; +using Example.Repositories; using GraphQL; using GraphQL.MicrosoftDI; using GraphQL.Server; @@ -16,6 +18,12 @@ public class Startup { public void ConfigureServices(IServiceCollection services) { + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + services.AddQueries(); + services.AddGraphQL(b => b .AddHttpMiddleware() .AddHttpMiddleware() @@ -28,6 +36,10 @@ public void ConfigureServices(IServiceCollection services) services.AddLogging(builder => builder.AddConsole()); services.AddHttpContextAccessor(); + services.AddHttpClient("DogsApi", x => + { + x.BaseAddress = new System.Uri("https://dog.ceo/"); + }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -40,6 +52,17 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseGraphQLPlayground(new PlaygroundOptions { GraphQLEndPoint = "/api/dogs" }, "/ui/dogs"); app.UseGraphQLPlayground(new PlaygroundOptions { GraphQLEndPoint = "/api/cats" }, "/ui/cats"); + } + } + + public static class StartupExtensions + { + public static void AddQueries(this IServiceCollection services) + { + services.AddSingleton(); + //services.AddSingleton(); + + services.AddSingleton(); } } } From ca1f6fc3b24456eb225e35d8d9056e5485b68c3b Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Mon, 30 Jan 2023 23:32:37 -0500 Subject: [PATCH 02/12] cleanup --- .../Example/GraphQL/Operations.cs | 3 --- src/AspNetCoreMulti/Example/GraphQL/Queries.cs | 17 ----------------- src/AspNetCoreMulti/Example/Startup.cs | 6 ++---- 3 files changed, 2 insertions(+), 24 deletions(-) diff --git a/src/AspNetCoreMulti/Example/GraphQL/Operations.cs b/src/AspNetCoreMulti/Example/GraphQL/Operations.cs index 51a2b51..891e7c9 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Operations.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Operations.cs @@ -1,11 +1,8 @@ using Example.Repositories; -using GraphQL.Resolvers; using GraphQL.Types; using Microsoft.Extensions.DependencyInjection; using System; -using System.Collections; using System.Collections.Generic; -using System.Linq; namespace Example.GraphQL { diff --git a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs index 00d16a6..7d1a7ae 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs @@ -1,27 +1,10 @@ using GraphQL.Types; -using System.Collections; using System.Collections.Generic; namespace Example.GraphQL { public class Queries { - //public class DogQuery : ObjectGraphType - //{ - // public DogQuery() - // { - // Field("say", resolve: context => "woof woof woof"); - // } - //} - - //public class CatQuery : ObjectGraphType - //{ - // public CatQuery() - // { - // Field("say", resolve: context => "meow meow meow"); - // } - //} - public class DogQuery : ObjectGraphType { public DogQuery(IEnumerable dogOperations) diff --git a/src/AspNetCoreMulti/Example/Startup.cs b/src/AspNetCoreMulti/Example/Startup.cs index 92aec44..f33d59f 100644 --- a/src/AspNetCoreMulti/Example/Startup.cs +++ b/src/AspNetCoreMulti/Example/Startup.cs @@ -22,7 +22,7 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); - services.AddQueries(); + services.AddOperations(); services.AddGraphQL(b => b .AddHttpMiddleware() @@ -57,11 +57,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public static class StartupExtensions { - public static void AddQueries(this IServiceCollection services) + public static void AddOperations(this IServiceCollection services) { services.AddSingleton(); - //services.AddSingleton(); - services.AddSingleton(); } } From bef20641cfb1801b396d64777a8b2c71df073b50 Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Tue, 31 Jan 2023 21:29:00 -0500 Subject: [PATCH 03/12] scoped namespaces and formatting --- .../Example/Repositories/CatRepository.cs | 17 ++++++------ .../Repositories/DogImageDetailsRepository.cs | 27 +++++++++---------- .../Example/Repositories/DogRepository.cs | 17 ++++++------ 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs b/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs index 1c07747..7949646 100644 --- a/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs +++ b/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs @@ -1,17 +1,16 @@ +namespace Example.Repositories; + using System.Collections.Generic; using System.Linq; -namespace Example.Repositories +public class CatRepository { - public class CatRepository - { - private static readonly List Cats = new() + private static readonly List Cats = new() { - new Cat{ Breed = "Abyssinian" }, - new Cat{ Breed = "American Bobtail" }, - new Cat{ Breed = "Burmese" } + new Cat { Breed = "Abyssinian" }, + new Cat { Breed = "American Bobtail" }, + new Cat { Breed = "Burmese" } }; - public IEnumerable GetCats() => Cats.AsEnumerable(); - } + public IEnumerable GetCats() => Cats.AsEnumerable(); } diff --git a/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs b/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs index f214a60..f3d60ff 100644 --- a/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs +++ b/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs @@ -1,23 +1,22 @@ +namespace Example.Repositories; + using System.Net.Http; using System.Threading.Tasks; -namespace Example.Repositories +public class DogImageDetailsRepository { - public class DogImageDetailsRepository - { - private readonly IHttpClientFactory _httpClientFactory; + private readonly IHttpClientFactory _httpClientFactory; - public DogImageDetailsRepository(IHttpClientFactory httpClientFactory) - { - _httpClientFactory = httpClientFactory; - } + public DogImageDetailsRepository(IHttpClientFactory httpClientFactory) + { + _httpClientFactory = httpClientFactory; + } - public async Task GetDogImageDetails() - { - var client = _httpClientFactory.CreateClient("DogsApi"); - var result = await client.GetStringAsync("api/breeds/image/random"); + public async Task GetDogImageDetails() + { + var client = _httpClientFactory.CreateClient("DogsApi"); + var result = await client.GetStringAsync("api/breeds/image/random"); - return new ImageDetails { Url = result }; - } + return new ImageDetails { Url = result }; } } diff --git a/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs b/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs index b767c03..3ad1b09 100644 --- a/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs +++ b/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs @@ -1,17 +1,16 @@ +namespace Example.Repositories; + using System.Collections.Generic; using System.Linq; -namespace Example.Repositories +public class DogRepository { - public class DogRepository - { - private static readonly List Dogs = new() + private static readonly List Dogs = new() { - new Dog{ Breed = "Doberman" }, - new Dog{ Breed = "Pit Bull" }, - new Dog{ Breed = "German Shepard" } + new Dog { Breed = "Doberman" }, + new Dog { Breed = "Pit Bull" }, + new Dog { Breed = "German Shepard" } }; - public IEnumerable GetDogs() => Dogs.AsEnumerable(); - } + public IEnumerable GetDogs() => Dogs.AsEnumerable(); } From 22d63d11f1a35627ad5586eb0118080a31a2c42e Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Tue, 31 Jan 2023 21:36:44 -0500 Subject: [PATCH 04/12] strong type api call --- src/AspNetCoreMulti/Example/Models.cs | 9 +++++-- src/AspNetCoreMulti/Example/Program.cs | 22 ++++----------- .../Repositories/DogImageDetailsRepository.cs | 27 ++++++++++++++++--- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/AspNetCoreMulti/Example/Models.cs b/src/AspNetCoreMulti/Example/Models.cs index 3918997..595021e 100644 --- a/src/AspNetCoreMulti/Example/Models.cs +++ b/src/AspNetCoreMulti/Example/Models.cs @@ -1,11 +1,16 @@ namespace Example { - public class Dog + public interface IBreed { public string Breed { get; set; } } - public class Cat + public class Dog : IBreed + { + public string Breed { get; set; } + } + + public class Cat : IBreed { public string Breed { get; set; } } diff --git a/src/AspNetCoreMulti/Example/Program.cs b/src/AspNetCoreMulti/Example/Program.cs index c003357..e58a459 100644 --- a/src/AspNetCoreMulti/Example/Program.cs +++ b/src/AspNetCoreMulti/Example/Program.cs @@ -1,27 +1,15 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; -using System; using System.Threading.Tasks; namespace Example { public class Program { - public static Task Main(string[] args) - { - try - { - return Host - .CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(builder => builder.UseStartup()) - .Build() - .RunAsync(); - } - catch (System.Exception ex) - { - Console.WriteLine(ex); - return Task.FromResult(0); - } - } + public static Task Main(string[] args) => Host + .CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(builder => builder.UseStartup()) + .Build() + .RunAsync(); } } diff --git a/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs b/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs index f3d60ff..186b026 100644 --- a/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs +++ b/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs @@ -1,6 +1,9 @@ namespace Example.Repositories; +using System; using System.Net.Http; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; public class DogImageDetailsRepository @@ -14,9 +17,27 @@ public DogImageDetailsRepository(IHttpClientFactory httpClientFactory) public async Task GetDogImageDetails() { - var client = _httpClientFactory.CreateClient("DogsApi"); - var result = await client.GetStringAsync("api/breeds/image/random"); + try + { + var client = _httpClientFactory.CreateClient("DogsApi"); + var result = await client.GetStringAsync("api/breeds/image/random"); + var apiResponse = JsonSerializer.Deserialize(result); - return new ImageDetails { Url = result }; + return new ImageDetails { Url = apiResponse.Message }; + } + catch (Exception ex) + { + + return new ImageDetails { Url = ex.Message }; + } + } + + private class DogsImageApiResponse + { + [JsonPropertyName("status")] + public string Status { get; set; } + + [JsonPropertyName("message")] + public string Message { get; set; } } } From d7c8b704a971f7423db9b42f9214ee132a5b34b5 Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Tue, 31 Jan 2023 21:59:18 -0500 Subject: [PATCH 05/12] changed the way fields are registered --- .../Example/GraphQL/Operations.cs | 82 ++++++++++++------- .../Example/GraphQL/Queries.cs | 10 +-- src/AspNetCoreMulti/Example/Startup.cs | 13 +-- 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/src/AspNetCoreMulti/Example/GraphQL/Operations.cs b/src/AspNetCoreMulti/Example/GraphQL/Operations.cs index 891e7c9..a2a334c 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Operations.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Operations.cs @@ -1,21 +1,23 @@ using Example.Repositories; +using GraphQL.Resolvers; using GraphQL.Types; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; +using System.Security.AccessControl; namespace Example.GraphQL { public interface IOperation { - IEnumerable RegisterFields(); + IEnumerable GetFields(); } public interface IDogOperation : IOperation { } public interface ICatOperation : IOperation { } - public class DogOperation : ObjectGraphType, IDogOperation + public class DogOperation : IDogOperation { private readonly IServiceProvider _serviceProvider; @@ -24,46 +26,68 @@ public DogOperation(IServiceProvider serviceProvider) _serviceProvider = serviceProvider; } - public IEnumerable RegisterFields() + public IEnumerable GetFields() { - var fields = new List + var fields = new List(); + + var sayField = new FieldType { - Field("say", resolve: context => "woof woof woof"), - GetBreedListField(), - GetImageDetailsField() + Name = "say", + Type = typeof(StringGraphType), + Resolver = new FuncFieldResolver(context => "woof woof woof") }; - return fields; - } + fields.Add(sayField); - private IFieldType GetBreedListField() - { - return Field>>>("dogBreeds", resolve: context => + var breedListField = new FieldType { - using var scope = _serviceProvider.CreateScope(); - var dogRepository = scope.ServiceProvider.GetRequiredService(); - var dogs = dogRepository.GetDogs(); - return dogs; - }); - } + Name = "dogBreeds", + Type = typeof(NonNullGraphType>>), + Resolver = new FuncFieldResolver(context => + { + using var scope = _serviceProvider.CreateScope(); + var dogRepository = scope.ServiceProvider.GetRequiredService(); + var dogs = dogRepository.GetDogs(); + return dogs; + }) + }; - private IFieldType GetImageDetailsField() - { - return FieldAsync>("dogImageDetails", resolve: async context => + fields.Add(breedListField); + + var imageDetailsField = new FieldType { - using var scope = _serviceProvider.CreateScope(); - var imageDetailsRepository = scope.ServiceProvider.GetRequiredService(); - var imageDetails = await imageDetailsRepository.GetDogImageDetails(); - return imageDetails; - }); + Name = "dogImageDetails", + Type = typeof(NonNullGraphType), + Resolver = new FuncFieldResolver(async context => + { + using var scope = _serviceProvider.CreateScope(); + var imageDetailsRepository = scope.ServiceProvider.GetRequiredService(); + var imageDetails = await imageDetailsRepository.GetDogImageDetails(); + return imageDetails; + }) + }; + + fields.Add(imageDetailsField); + + return fields; } } - public class CatSayOperation : ObjectGraphType, ICatOperation + public class CatSayOperation : ICatOperation { - public IEnumerable RegisterFields() + public IEnumerable GetFields() { - return new List { Field("say", resolve: context => "meow meow meow") }; + var fields = new List(); + var sayField = new FieldType + { + Name = "say", + Type = typeof(StringGraphType), + Resolver = new FuncFieldResolver(context => "meow meow meow") + }; + + fields.Add(sayField); + + return fields; } } } diff --git a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs index 7d1a7ae..24dba3a 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs @@ -9,10 +9,10 @@ public class DogQuery : ObjectGraphType { public DogQuery(IEnumerable dogOperations) { - foreach(var dogOperation in dogOperations) + foreach (var dogOperation in dogOperations) { - var fields = dogOperation.RegisterFields(); - foreach(var field in fields) + var fields = dogOperation.GetFields(); + foreach (var field in fields) { AddField((FieldType)field); } @@ -24,9 +24,9 @@ public class CatQuery : ObjectGraphType { public CatQuery(IEnumerable catOperations) { - foreach(var catOperation in catOperations) + foreach (var catOperation in catOperations) { - var fields = catOperation.RegisterFields(); + var fields = catOperation.GetFields(); foreach (var field in fields) { AddField((FieldType)field); diff --git a/src/AspNetCoreMulti/Example/Startup.cs b/src/AspNetCoreMulti/Example/Startup.cs index f33d59f..e0fbcbc 100644 --- a/src/AspNetCoreMulti/Example/Startup.cs +++ b/src/AspNetCoreMulti/Example/Startup.cs @@ -5,7 +5,6 @@ using GraphQL.Server; using GraphQL.Server.Ui.Playground; using GraphQL.SystemTextJson; -using GraphQL.Types; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; @@ -22,7 +21,8 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); - services.AddOperations(); + services.AddSingleton(); + services.AddSingleton(); services.AddGraphQL(b => b .AddHttpMiddleware() @@ -52,15 +52,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseGraphQLPlayground(new PlaygroundOptions { GraphQLEndPoint = "/api/dogs" }, "/ui/dogs"); app.UseGraphQLPlayground(new PlaygroundOptions { GraphQLEndPoint = "/api/cats" }, "/ui/cats"); - } - } - - public static class StartupExtensions - { - public static void AddOperations(this IServiceCollection services) - { - services.AddSingleton(); - services.AddSingleton(); } } } From ceefd064f06d81de3180398075503a59eddebedf Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Wed, 1 Feb 2023 14:50:31 -0500 Subject: [PATCH 06/12] removed unecessary new line --- .../Example/Repositories/DogImageDetailsRepository.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs b/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs index 186b026..cdc7fb2 100644 --- a/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs +++ b/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs @@ -27,7 +27,6 @@ public async Task GetDogImageDetails() } catch (Exception ex) { - return new ImageDetails { Url = ex.Message }; } } From 5f03f965c09a1f1b214d4d0e5fbedbcd576043db Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Wed, 1 Feb 2023 15:20:41 -0500 Subject: [PATCH 07/12] further restructuting and renaming --- .../Example/GraphQL/Operations.cs | 93 -------------- .../Example/GraphQL/Queries.cs | 117 ++++++++++++++---- .../Example/GraphQL/RootMutations.cs | 16 +++ .../Example/GraphQL/RootQueries.cs | 27 ++++ .../Example/GraphQL/Schemas.cs | 6 +- .../Example/Repositories/CatRepository.cs | 13 ++ src/AspNetCoreMulti/Example/Startup.cs | 4 +- 7 files changed, 155 insertions(+), 121 deletions(-) delete mode 100644 src/AspNetCoreMulti/Example/GraphQL/Operations.cs create mode 100644 src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs create mode 100644 src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs diff --git a/src/AspNetCoreMulti/Example/GraphQL/Operations.cs b/src/AspNetCoreMulti/Example/GraphQL/Operations.cs deleted file mode 100644 index a2a334c..0000000 --- a/src/AspNetCoreMulti/Example/GraphQL/Operations.cs +++ /dev/null @@ -1,93 +0,0 @@ -using Example.Repositories; -using GraphQL.Resolvers; -using GraphQL.Types; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Security.AccessControl; - -namespace Example.GraphQL -{ - public interface IOperation - { - IEnumerable GetFields(); - } - - public interface IDogOperation : IOperation { } - - public interface ICatOperation : IOperation { } - - public class DogOperation : IDogOperation - { - private readonly IServiceProvider _serviceProvider; - - public DogOperation(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } - - public IEnumerable GetFields() - { - var fields = new List(); - - var sayField = new FieldType - { - Name = "say", - Type = typeof(StringGraphType), - Resolver = new FuncFieldResolver(context => "woof woof woof") - }; - - fields.Add(sayField); - - var breedListField = new FieldType - { - Name = "dogBreeds", - Type = typeof(NonNullGraphType>>), - Resolver = new FuncFieldResolver(context => - { - using var scope = _serviceProvider.CreateScope(); - var dogRepository = scope.ServiceProvider.GetRequiredService(); - var dogs = dogRepository.GetDogs(); - return dogs; - }) - }; - - fields.Add(breedListField); - - var imageDetailsField = new FieldType - { - Name = "dogImageDetails", - Type = typeof(NonNullGraphType), - Resolver = new FuncFieldResolver(async context => - { - using var scope = _serviceProvider.CreateScope(); - var imageDetailsRepository = scope.ServiceProvider.GetRequiredService(); - var imageDetails = await imageDetailsRepository.GetDogImageDetails(); - return imageDetails; - }) - }; - - fields.Add(imageDetailsField); - - return fields; - } - } - - public class CatSayOperation : ICatOperation - { - public IEnumerable GetFields() - { - var fields = new List(); - var sayField = new FieldType - { - Name = "say", - Type = typeof(StringGraphType), - Resolver = new FuncFieldResolver(context => "meow meow meow") - }; - - fields.Add(sayField); - - return fields; - } - } -} diff --git a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs index 24dba3a..0781303 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs @@ -1,38 +1,109 @@ +using Example.Repositories; +using GraphQL; +using GraphQL.Resolvers; using GraphQL.Types; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; +using System.Security.AccessControl; namespace Example.GraphQL { - public class Queries + public interface IQueryFieldsProvider { - public class DogQuery : ObjectGraphType + void AddQueryFields(ObjectGraphType objectGraph); + } + + public interface IMutationFieldsProvider + { + void AddMutationFields(ObjectGraphType objectGraph); + } + + public interface IDogQuery : IQueryFieldsProvider { } + + public interface ICatQuery : IQueryFieldsProvider { } + + public interface ICatMutation : IMutationFieldsProvider { } + + public class DogQuery : IDogQuery + { + private readonly IServiceProvider _serviceProvider; + + public DogQuery(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public void AddQueryFields(ObjectGraphType objectGraph) + { + objectGraph.Field("say", resolve: context => "woof woof woof"); + + objectGraph.Field>>>("dogBreeds", resolve: context => + { + using var scope = _serviceProvider.CreateScope(); + var dogRepository = scope.ServiceProvider.GetRequiredService(); + var dogs = dogRepository.GetDogs(); + return dogs; + }); + } + } + + public class DogImageDetailsQuery : IDogQuery + { + private readonly IServiceProvider _serviceProvider; + + public DogImageDetailsQuery(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public void AddQueryFields(ObjectGraphType objectGraph) { - public DogQuery(IEnumerable dogOperations) + objectGraph.FieldAsync>("dogImageDetails", resolve: async context => { - foreach (var dogOperation in dogOperations) - { - var fields = dogOperation.GetFields(); - foreach (var field in fields) - { - AddField((FieldType)field); - } - } - } + using var scope = _serviceProvider.CreateScope(); + var imageDetailsRepository = scope.ServiceProvider.GetRequiredService(); + var imageDetails = await imageDetailsRepository.GetDogImageDetails(); + return imageDetails; + }); } + } - public class CatQuery : ObjectGraphType + public class CatQuery : ICatQuery + { + public void AddQueryFields(ObjectGraphType objectGraph) { - public CatQuery(IEnumerable catOperations) + objectGraph.Field("say", resolve: context => "meow meow meow"); + } + } + + public class CatBreedUpdateMutation : ICatMutation + { + private readonly IServiceProvider _serviceProvider; + + public CatBreedUpdateMutation(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public void AddMutationFields(ObjectGraphType objectGraph) + { + var args = new QueryArguments + { + new QueryArgument { Name = "breedName" }, + new QueryArgument { Name = "newBreedName" } + }; + + objectGraph.Field("updateCatBreed", arguments: args, resolve: context => { - foreach (var catOperation in catOperations) - { - var fields = catOperation.GetFields(); - foreach (var field in fields) - { - AddField((FieldType)field); - } - } - } + var breed = context.GetArgument("breedName"); + var newBreed = context.GetArgument("newBreedName"); + using var scope = _serviceProvider.CreateScope(); + var catRepository = scope.ServiceProvider.GetRequiredService(); + var result = catRepository.UpdateCatBreedName(breed, newBreed); + + return result; + }); } } } diff --git a/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs b/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs new file mode 100644 index 0000000..aa9e369 --- /dev/null +++ b/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs @@ -0,0 +1,16 @@ +using GraphQL.Types; +using System.Collections.Generic; + +namespace Example.GraphQL +{ + public class CatRootMutation : ObjectGraphType + { + public CatRootMutation(IEnumerable mutations) + { + foreach(var mutation in mutations) + { + mutation.AddMutationFields(this); + } + } + } +} diff --git a/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs b/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs new file mode 100644 index 0000000..3b28dd9 --- /dev/null +++ b/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs @@ -0,0 +1,27 @@ +using GraphQL.Types; +using System.Collections.Generic; + +namespace Example.GraphQL +{ + public class DogRootQuery : ObjectGraphType + { + public DogRootQuery(IEnumerable dogQueries) + { + foreach (var dogQuery in dogQueries) + { + dogQuery.AddQueryFields(this); + } + } + } + + public class CatRootQuery : ObjectGraphType + { + public CatRootQuery(IEnumerable catQueries) + { + foreach (var catQuery in catQueries) + { + catQuery.AddQueryFields(this); + } + } + } +} diff --git a/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs b/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs index b611995..7ea095d 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs @@ -1,12 +1,11 @@ using GraphQL.Types; using System; -using static Example.GraphQL.Queries; namespace Example.GraphQL { public class DogSchema : Schema { - public DogSchema(IServiceProvider provider, DogQuery query) + public DogSchema(IServiceProvider provider, DogRootQuery query) : base(provider) { Query = query; @@ -15,10 +14,11 @@ public DogSchema(IServiceProvider provider, DogQuery query) public class CatSchema : Schema { - public CatSchema(IServiceProvider provider, CatQuery query) + public CatSchema(IServiceProvider provider, CatRootQuery query, CatRootMutation mutation) : base(provider) { Query = query; + Mutation = mutation; } } } diff --git a/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs b/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs index 7949646..9cc6386 100644 --- a/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs +++ b/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs @@ -13,4 +13,17 @@ public class CatRepository }; public IEnumerable GetCats() => Cats.AsEnumerable(); + + public Cat UpdateCatBreedName(string oldName, string newName) + { + var match = Cats.FirstOrDefault(x => x.Breed == oldName); + if (match == null) + { + throw new System.Exception("Cannot find that cat !"); + } + + match.Breed = newName; + + return match; + } } diff --git a/src/AspNetCoreMulti/Example/Startup.cs b/src/AspNetCoreMulti/Example/Startup.cs index e0fbcbc..a845a45 100644 --- a/src/AspNetCoreMulti/Example/Startup.cs +++ b/src/AspNetCoreMulti/Example/Startup.cs @@ -21,8 +21,8 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); - services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); services.AddGraphQL(b => b .AddHttpMiddleware() From de04f5b99bd9527601743def39e66bc43f5724bb Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Wed, 1 Feb 2023 22:14:29 -0500 Subject: [PATCH 08/12] further restructuring --- .../Example/GraphQL/Mutations.cs | 42 +++++++++++++++++++ .../Example/GraphQL/Queries.cs | 36 +++------------- .../Example/GraphQL/RootMutations.cs | 2 +- src/AspNetCoreMulti/Example/GraphQL/Types.cs | 2 +- src/AspNetCoreMulti/Example/Startup.cs | 2 + 5 files changed, 52 insertions(+), 32 deletions(-) create mode 100644 src/AspNetCoreMulti/Example/GraphQL/Mutations.cs diff --git a/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs b/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs new file mode 100644 index 0000000..4c12094 --- /dev/null +++ b/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs @@ -0,0 +1,42 @@ +using Example.Repositories; +using GraphQL; +using GraphQL.Types; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace Example.GraphQL; + +public interface IMutationFieldsProvider +{ + void AddMutationFields(ObjectGraphType objectGraph); +} + +public class CatBreedUpdateMutation : ICatMutation +{ + private readonly IServiceProvider _serviceProvider; + + public CatBreedUpdateMutation(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public void AddMutationFields(ObjectGraphType objectGraph) + { + var args = new QueryArguments + { + new QueryArgument { Name = "breedName" }, + new QueryArgument { Name = "newBreedName" } + }; + + objectGraph.Field("updateCatBreed", arguments: args, resolve: context => + { + var breed = context.GetArgument("breedName"); + var newBreed = context.GetArgument("newBreedName"); + using var scope = _serviceProvider.CreateScope(); + var catRepository = scope.ServiceProvider.GetRequiredService(); + var result = catRepository.UpdateCatBreedName(breed, newBreed); + + return result; + }); + } +} diff --git a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs index 0781303..762dfd9 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs @@ -1,11 +1,7 @@ using Example.Repositories; -using GraphQL; -using GraphQL.Resolvers; using GraphQL.Types; using Microsoft.Extensions.DependencyInjection; using System; -using System.Collections.Generic; -using System.Security.AccessControl; namespace Example.GraphQL { @@ -14,11 +10,6 @@ public interface IQueryFieldsProvider void AddQueryFields(ObjectGraphType objectGraph); } - public interface IMutationFieldsProvider - { - void AddMutationFields(ObjectGraphType objectGraph); - } - public interface IDogQuery : IQueryFieldsProvider { } public interface ICatQuery : IQueryFieldsProvider { } @@ -70,39 +61,24 @@ public void AddQueryFields(ObjectGraphType objectGraph) } public class CatQuery : ICatQuery - { - public void AddQueryFields(ObjectGraphType objectGraph) - { - objectGraph.Field("say", resolve: context => "meow meow meow"); - } - } - - public class CatBreedUpdateMutation : ICatMutation { private readonly IServiceProvider _serviceProvider; - public CatBreedUpdateMutation(IServiceProvider serviceProvider) + public CatQuery(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } - public void AddMutationFields(ObjectGraphType objectGraph) + public void AddQueryFields(ObjectGraphType objectGraph) { - var args = new QueryArguments - { - new QueryArgument { Name = "breedName" }, - new QueryArgument { Name = "newBreedName" } - }; + objectGraph.Field("say", resolve: context => "meow meow meow"); - objectGraph.Field("updateCatBreed", arguments: args, resolve: context => + objectGraph.Field>>>("catBreeds", resolve: context => { - var breed = context.GetArgument("breedName"); - var newBreed = context.GetArgument("newBreedName"); using var scope = _serviceProvider.CreateScope(); var catRepository = scope.ServiceProvider.GetRequiredService(); - var result = catRepository.UpdateCatBreedName(breed, newBreed); - - return result; + var cats = catRepository.GetCats(); + return cats; }); } } diff --git a/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs b/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs index aa9e369..4721537 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs @@ -7,7 +7,7 @@ public class CatRootMutation : ObjectGraphType { public CatRootMutation(IEnumerable mutations) { - foreach(var mutation in mutations) + foreach (var mutation in mutations) { mutation.AddMutationFields(this); } diff --git a/src/AspNetCoreMulti/Example/GraphQL/Types.cs b/src/AspNetCoreMulti/Example/GraphQL/Types.cs index 19fa652..782d3a7 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Types.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Types.cs @@ -10,7 +10,7 @@ public DogType() } } - public class CatType : ObjectGraphType + public class CatType : ObjectGraphType { public CatType() { diff --git a/src/AspNetCoreMulti/Example/Startup.cs b/src/AspNetCoreMulti/Example/Startup.cs index a845a45..09579b7 100644 --- a/src/AspNetCoreMulti/Example/Startup.cs +++ b/src/AspNetCoreMulti/Example/Startup.cs @@ -22,7 +22,9 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); services.AddGraphQL(b => b .AddHttpMiddleware() From 6eddb191e898421372c64e9558e3188c6e31e6e5 Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Wed, 1 Feb 2023 22:17:06 -0500 Subject: [PATCH 09/12] moved all mutation related things to same file --- src/AspNetCoreMulti/Example/GraphQL/Mutations.cs | 2 ++ src/AspNetCoreMulti/Example/GraphQL/Queries.cs | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs b/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs index 4c12094..25140f6 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs @@ -11,6 +11,8 @@ public interface IMutationFieldsProvider void AddMutationFields(ObjectGraphType objectGraph); } +public interface ICatMutation : IMutationFieldsProvider { } + public class CatBreedUpdateMutation : ICatMutation { private readonly IServiceProvider _serviceProvider; diff --git a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs index 762dfd9..a4ddd8f 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs @@ -14,8 +14,6 @@ public interface IDogQuery : IQueryFieldsProvider { } public interface ICatQuery : IQueryFieldsProvider { } - public interface ICatMutation : IMutationFieldsProvider { } - public class DogQuery : IDogQuery { private readonly IServiceProvider _serviceProvider; From 2090c199ca504e9937c1d179c4856ea80ae7f340 Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Wed, 1 Feb 2023 22:25:18 -0500 Subject: [PATCH 10/12] cleanup --- src/AspNetCoreMulti/Example/GraphQL.cs | 39 ------ .../Example/GraphQL/Queries.cs | 114 +++++++++--------- .../Example/GraphQL/RootMutations.cs | 13 +- .../Example/GraphQL/RootQueries.cs | 25 ++-- .../Example/GraphQL/Schemas.cs | 27 ++--- src/AspNetCoreMulti/Example/GraphQL/Types.cs | 31 +++-- .../Example/GraphQLUserContext.cs | 9 +- src/AspNetCoreMulti/Example/Models.cs | 33 +++-- src/AspNetCoreMulti/Example/Startup.cs | 5 +- 9 files changed, 124 insertions(+), 172 deletions(-) delete mode 100644 src/AspNetCoreMulti/Example/GraphQL.cs diff --git a/src/AspNetCoreMulti/Example/GraphQL.cs b/src/AspNetCoreMulti/Example/GraphQL.cs deleted file mode 100644 index c988986..0000000 --- a/src/AspNetCoreMulti/Example/GraphQL.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using GraphQL.Types; - -namespace Example -{ - public class DogSchema : Schema - { - public DogSchema(IServiceProvider provider, DogQuery query) - : base(provider) - { - Query = query; - } - } - - public class DogQuery : ObjectGraphType - { - public DogQuery() - { - Field("say", resolve: context => "woof woof woof"); - } - } - - public class CatSchema : Schema - { - public CatSchema(IServiceProvider provider, CatQuery query) - : base(provider) - { - Query = query; - } - } - - public class CatQuery : ObjectGraphType - { - public CatQuery() - { - Field("say", resolve: context => "meow meow meow"); - } - } -} diff --git a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs index a4ddd8f..484ab1e 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs @@ -3,81 +3,81 @@ using Microsoft.Extensions.DependencyInjection; using System; -namespace Example.GraphQL +namespace Example.GraphQL; + +public interface IQueryFieldsProvider { - public interface IQueryFieldsProvider - { - void AddQueryFields(ObjectGraphType objectGraph); - } + void AddQueryFields(ObjectGraphType objectGraph); +} + +public interface IDogQuery : IQueryFieldsProvider { } - public interface IDogQuery : IQueryFieldsProvider { } +public interface ICatQuery : IQueryFieldsProvider { } - public interface ICatQuery : IQueryFieldsProvider { } +public class DogQuery : IDogQuery +{ + private readonly IServiceProvider _serviceProvider; - public class DogQuery : IDogQuery + public DogQuery(IServiceProvider serviceProvider) { - private readonly IServiceProvider _serviceProvider; + _serviceProvider = serviceProvider; + } - public DogQuery(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } + public void AddQueryFields(ObjectGraphType objectGraph) + { + objectGraph.Field("say", resolve: context => "woof woof woof"); - public void AddQueryFields(ObjectGraphType objectGraph) + objectGraph.Field>>>("dogBreeds", resolve: context => { - objectGraph.Field("say", resolve: context => "woof woof woof"); - - objectGraph.Field>>>("dogBreeds", resolve: context => - { - using var scope = _serviceProvider.CreateScope(); - var dogRepository = scope.ServiceProvider.GetRequiredService(); - var dogs = dogRepository.GetDogs(); - return dogs; - }); - } + using var scope = _serviceProvider.CreateScope(); + var dogRepository = scope.ServiceProvider.GetRequiredService(); + var dogs = dogRepository.GetDogs(); + return dogs; + }); } +} - public class DogImageDetailsQuery : IDogQuery - { - private readonly IServiceProvider _serviceProvider; +public class DogImageDetailsQuery : IDogQuery +{ + private readonly IServiceProvider _serviceProvider; - public DogImageDetailsQuery(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } + public DogImageDetailsQuery(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } - public void AddQueryFields(ObjectGraphType objectGraph) + public void AddQueryFields(ObjectGraphType objectGraph) + { + objectGraph.FieldAsync>("dogImageDetails", resolve: async context => { - objectGraph.FieldAsync>("dogImageDetails", resolve: async context => - { - using var scope = _serviceProvider.CreateScope(); - var imageDetailsRepository = scope.ServiceProvider.GetRequiredService(); - var imageDetails = await imageDetailsRepository.GetDogImageDetails(); - return imageDetails; - }); - } + using var scope = _serviceProvider.CreateScope(); + var imageDetailsRepository = scope.ServiceProvider.GetRequiredService(); + var imageDetails = await imageDetailsRepository.GetDogImageDetails(); + return imageDetails; + }); } +} - public class CatQuery : ICatQuery +public class CatQuery : ICatQuery +{ + private readonly IServiceProvider _serviceProvider; + + public CatQuery(IServiceProvider serviceProvider) { - private readonly IServiceProvider _serviceProvider; + _serviceProvider = serviceProvider; + } - public CatQuery(IServiceProvider serviceProvider) - { - _serviceProvider = serviceProvider; - } + public void AddQueryFields(ObjectGraphType objectGraph) + { + objectGraph.Field("say", resolve: context => "meow meow meow"); - public void AddQueryFields(ObjectGraphType objectGraph) + objectGraph.Field>>>("catBreeds", resolve: context => { - objectGraph.Field("say", resolve: context => "meow meow meow"); - - objectGraph.Field>>>("catBreeds", resolve: context => - { - using var scope = _serviceProvider.CreateScope(); - var catRepository = scope.ServiceProvider.GetRequiredService(); - var cats = catRepository.GetCats(); - return cats; - }); - } + using var scope = _serviceProvider.CreateScope(); + var catRepository = scope.ServiceProvider.GetRequiredService(); + var cats = catRepository.GetCats(); + return cats; + }); } } + diff --git a/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs b/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs index 4721537..e5a3aaf 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs @@ -1,16 +1,15 @@ using GraphQL.Types; using System.Collections.Generic; -namespace Example.GraphQL +namespace Example.GraphQL; + +public class CatRootMutation : ObjectGraphType { - public class CatRootMutation : ObjectGraphType + public CatRootMutation(IEnumerable mutations) { - public CatRootMutation(IEnumerable mutations) + foreach (var mutation in mutations) { - foreach (var mutation in mutations) - { - mutation.AddMutationFields(this); - } + mutation.AddMutationFields(this); } } } diff --git a/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs b/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs index 3b28dd9..666a535 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs @@ -1,27 +1,26 @@ using GraphQL.Types; using System.Collections.Generic; -namespace Example.GraphQL +namespace Example.GraphQL; + +public class DogRootQuery : ObjectGraphType { - public class DogRootQuery : ObjectGraphType + public DogRootQuery(IEnumerable dogQueries) { - public DogRootQuery(IEnumerable dogQueries) + foreach (var dogQuery in dogQueries) { - foreach (var dogQuery in dogQueries) - { - dogQuery.AddQueryFields(this); - } + dogQuery.AddQueryFields(this); } } +} - public class CatRootQuery : ObjectGraphType +public class CatRootQuery : ObjectGraphType +{ + public CatRootQuery(IEnumerable catQueries) { - public CatRootQuery(IEnumerable catQueries) + foreach (var catQuery in catQueries) { - foreach (var catQuery in catQueries) - { - catQuery.AddQueryFields(this); - } + catQuery.AddQueryFields(this); } } } diff --git a/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs b/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs index 7ea095d..a2c8b58 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs @@ -1,24 +1,23 @@ using GraphQL.Types; using System; -namespace Example.GraphQL +namespace Example.GraphQL; + +public class DogSchema : Schema { - public class DogSchema : Schema + public DogSchema(IServiceProvider provider, DogRootQuery query) + : base(provider) { - public DogSchema(IServiceProvider provider, DogRootQuery query) - : base(provider) - { - Query = query; - } + Query = query; } +} - public class CatSchema : Schema +public class CatSchema : Schema +{ + public CatSchema(IServiceProvider provider, CatRootQuery query, CatRootMutation mutation) + : base(provider) { - public CatSchema(IServiceProvider provider, CatRootQuery query, CatRootMutation mutation) - : base(provider) - { - Query = query; - Mutation = mutation; - } + Query = query; + Mutation = mutation; } } diff --git a/src/AspNetCoreMulti/Example/GraphQL/Types.cs b/src/AspNetCoreMulti/Example/GraphQL/Types.cs index 782d3a7..bea9d77 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Types.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Types.cs @@ -1,28 +1,27 @@ using GraphQL.Types; -namespace Example.GraphQL +namespace Example.GraphQL; + +public class DogType : ObjectGraphType { - public class DogType : ObjectGraphType + public DogType() { - public DogType() - { - Field(x => x.Breed); - } + Field(x => x.Breed); } +} - public class CatType : ObjectGraphType +public class CatType : ObjectGraphType +{ + public CatType() { - public CatType() - { - Field(x => x.Breed); - } + Field(x => x.Breed); } +} - public class ImageDetailsType : ObjectGraphType +public class ImageDetailsType : ObjectGraphType +{ + public ImageDetailsType() { - public ImageDetailsType() - { - Field(x => x.Url); - } + Field(x => x.Url); } } diff --git a/src/AspNetCoreMulti/Example/GraphQLUserContext.cs b/src/AspNetCoreMulti/Example/GraphQLUserContext.cs index c6661f7..7f85ce9 100644 --- a/src/AspNetCoreMulti/Example/GraphQLUserContext.cs +++ b/src/AspNetCoreMulti/Example/GraphQLUserContext.cs @@ -1,10 +1,9 @@ using System.Collections.Generic; using System.Security.Claims; -namespace Example +namespace Example; + +public class GraphQLUserContext : Dictionary { - public class GraphQLUserContext : Dictionary - { - public ClaimsPrincipal User { get; set; } - } + public ClaimsPrincipal User { get; set; } } diff --git a/src/AspNetCoreMulti/Example/Models.cs b/src/AspNetCoreMulti/Example/Models.cs index 595021e..0d10ad5 100644 --- a/src/AspNetCoreMulti/Example/Models.cs +++ b/src/AspNetCoreMulti/Example/Models.cs @@ -1,22 +1,21 @@ -namespace Example +namespace Example; + +public interface IBreed { - public interface IBreed - { - public string Breed { get; set; } - } + public string Breed { get; set; } +} - public class Dog : IBreed - { - public string Breed { get; set; } - } +public class Dog : IBreed +{ + public string Breed { get; set; } +} - public class Cat : IBreed - { - public string Breed { get; set; } - } +public class Cat : IBreed +{ + public string Breed { get; set; } +} - public class ImageDetails - { - public string Url { get; set; } - } +public class ImageDetails +{ + public string Url { get; set; } } diff --git a/src/AspNetCoreMulti/Example/Startup.cs b/src/AspNetCoreMulti/Example/Startup.cs index 09579b7..f6d94a6 100644 --- a/src/AspNetCoreMulti/Example/Startup.cs +++ b/src/AspNetCoreMulti/Example/Startup.cs @@ -38,10 +38,7 @@ public void ConfigureServices(IServiceCollection services) services.AddLogging(builder => builder.AddConsole()); services.AddHttpContextAccessor(); - services.AddHttpClient("DogsApi", x => - { - x.BaseAddress = new System.Uri("https://dog.ceo/"); - }); + services.AddHttpClient("DogsApi", x => x.BaseAddress = new System.Uri("https://dog.ceo/")); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) From c2cf1a82f660d60707284ed689bf133133ccc64d Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Wed, 1 Feb 2023 22:42:30 -0500 Subject: [PATCH 11/12] fixed code violations --- .../Example/GraphQL/Mutations.cs | 8 ++++---- .../Example/GraphQL/Queries.cs | 2 +- .../Example/Repositories/CatRepository.cs | 19 +++++++++---------- .../Repositories/DogImageDetailsRepository.cs | 5 ++--- .../Example/Repositories/DogRepository.cs | 7 +++---- 5 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs b/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs index 25140f6..f42a9dd 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs @@ -25,10 +25,10 @@ public CatBreedUpdateMutation(IServiceProvider serviceProvider) public void AddMutationFields(ObjectGraphType objectGraph) { var args = new QueryArguments - { - new QueryArgument { Name = "breedName" }, - new QueryArgument { Name = "newBreedName" } - }; + { + new QueryArgument { Name = "breedName" }, + new QueryArgument { Name = "newBreedName" } + }; objectGraph.Field("updateCatBreed", arguments: args, resolve: context => { diff --git a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs index 484ab1e..f63eee8 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs @@ -52,7 +52,7 @@ public void AddQueryFields(ObjectGraphType objectGraph) { using var scope = _serviceProvider.CreateScope(); var imageDetailsRepository = scope.ServiceProvider.GetRequiredService(); - var imageDetails = await imageDetailsRepository.GetDogImageDetails(); + var imageDetails = await imageDetailsRepository.GetDogImageDetailsAsync(); return imageDetails; }); } diff --git a/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs b/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs index 9cc6386..2df263d 100644 --- a/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs +++ b/src/AspNetCoreMulti/Example/Repositories/CatRepository.cs @@ -1,22 +1,21 @@ -namespace Example.Repositories; - using System.Collections.Generic; using System.Linq; +namespace Example.Repositories; public class CatRepository { - private static readonly List Cats = new() - { - new Cat { Breed = "Abyssinian" }, - new Cat { Breed = "American Bobtail" }, - new Cat { Breed = "Burmese" } - }; + private static readonly List _cats = new() + { + new Cat { Breed = "Abyssinian" }, + new Cat { Breed = "American Bobtail" }, + new Cat { Breed = "Burmese" } + }; - public IEnumerable GetCats() => Cats.AsEnumerable(); + public IEnumerable GetCats() => _cats.AsEnumerable(); public Cat UpdateCatBreedName(string oldName, string newName) { - var match = Cats.FirstOrDefault(x => x.Breed == oldName); + var match = _cats.FirstOrDefault(x => x.Breed == oldName); if (match == null) { throw new System.Exception("Cannot find that cat !"); diff --git a/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs b/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs index cdc7fb2..002dc72 100644 --- a/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs +++ b/src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs @@ -1,11 +1,10 @@ -namespace Example.Repositories; - using System; using System.Net.Http; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading.Tasks; +namespace Example.Repositories; public class DogImageDetailsRepository { private readonly IHttpClientFactory _httpClientFactory; @@ -15,7 +14,7 @@ public DogImageDetailsRepository(IHttpClientFactory httpClientFactory) _httpClientFactory = httpClientFactory; } - public async Task GetDogImageDetails() + public async Task GetDogImageDetailsAsync() { try { diff --git a/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs b/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs index 3ad1b09..28788e3 100644 --- a/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs +++ b/src/AspNetCoreMulti/Example/Repositories/DogRepository.cs @@ -1,16 +1,15 @@ -namespace Example.Repositories; - using System.Collections.Generic; using System.Linq; +namespace Example.Repositories; public class DogRepository { - private static readonly List Dogs = new() + private static readonly List _dogs = new() { new Dog { Breed = "Doberman" }, new Dog { Breed = "Pit Bull" }, new Dog { Breed = "German Shepard" } }; - public IEnumerable GetDogs() => Dogs.AsEnumerable(); + public IEnumerable GetDogs() => _dogs.AsEnumerable(); } From 368092f92b45af89d798bd49f302a4026b35eb33 Mon Sep 17 00:00:00 2001 From: Emmanuel Ponnudurai Date: Wed, 1 Feb 2023 22:55:53 -0500 Subject: [PATCH 12/12] formatting --- src/AspNetCoreMulti/Example/GraphQL/Mutations.cs | 2 +- src/AspNetCoreMulti/Example/GraphQL/Queries.cs | 2 +- src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs | 2 +- src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs | 2 +- src/AspNetCoreMulti/Example/GraphQL/Schemas.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs b/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs index f42a9dd..c16413d 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Mutations.cs @@ -1,8 +1,8 @@ +using System; using Example.Repositories; using GraphQL; using GraphQL.Types; using Microsoft.Extensions.DependencyInjection; -using System; namespace Example.GraphQL; diff --git a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs index f63eee8..1b8cba2 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Queries.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Queries.cs @@ -1,7 +1,7 @@ +using System; using Example.Repositories; using GraphQL.Types; using Microsoft.Extensions.DependencyInjection; -using System; namespace Example.GraphQL; diff --git a/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs b/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs index e5a3aaf..fa19252 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/RootMutations.cs @@ -1,5 +1,5 @@ -using GraphQL.Types; using System.Collections.Generic; +using GraphQL.Types; namespace Example.GraphQL; diff --git a/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs b/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs index 666a535..6544cb5 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/RootQueries.cs @@ -1,5 +1,5 @@ -using GraphQL.Types; using System.Collections.Generic; +using GraphQL.Types; namespace Example.GraphQL; diff --git a/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs b/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs index a2c8b58..6f5712b 100644 --- a/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs +++ b/src/AspNetCoreMulti/Example/GraphQL/Schemas.cs @@ -1,5 +1,5 @@ -using GraphQL.Types; using System; +using GraphQL.Types; namespace Example.GraphQL;