-
Notifications
You must be signed in to change notification settings - Fork 193
Split Construction of Graph Types into individual modules #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EmmanuelPonnudurai
wants to merge
13
commits into
graphql-dotnet:master
Choose a base branch
from
EmmanuelPonnudurai:structured_example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9a55028
add multiple operations and structure things
EmmanuelPonnudurai ca1f6fc
cleanup
EmmanuelPonnudurai bef2064
scoped namespaces and formatting
EmmanuelPonnudurai 22d63d1
strong type api call
EmmanuelPonnudurai d7c8b70
changed the way fields are registered
EmmanuelPonnudurai ceefd06
removed unecessary new line
EmmanuelPonnudurai 5f03f96
further restructuting and renaming
EmmanuelPonnudurai de04f5b
further restructuring
EmmanuelPonnudurai 6eddb19
moved all mutation related things to same file
EmmanuelPonnudurai 1ce9c77
synched with main branch
EmmanuelPonnudurai 2090c19
cleanup
EmmanuelPonnudurai c2cf1a8
fixed code violations
EmmanuelPonnudurai 368092f
formatting
EmmanuelPonnudurai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| 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<IFieldType> 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<IFieldType> GetFields() | ||
| { | ||
| var fields = new List<IFieldType>(); | ||
|
|
||
| var sayField = new FieldType | ||
| { | ||
| Name = "say", | ||
| Type = typeof(StringGraphType), | ||
| Resolver = new FuncFieldResolver<object, object>(context => "woof woof woof") | ||
| }; | ||
|
|
||
| fields.Add(sayField); | ||
|
|
||
| var breedListField = new FieldType | ||
| { | ||
| Name = "dogBreeds", | ||
| Type = typeof(NonNullGraphType<ListGraphType<NonNullGraphType<DogType>>>), | ||
| Resolver = new FuncFieldResolver<object, object>(context => | ||
| { | ||
| using var scope = _serviceProvider.CreateScope(); | ||
| var dogRepository = scope.ServiceProvider.GetRequiredService<DogRepository>(); | ||
| var dogs = dogRepository.GetDogs(); | ||
| return dogs; | ||
| }) | ||
| }; | ||
|
|
||
| fields.Add(breedListField); | ||
|
|
||
| var imageDetailsField = new FieldType | ||
| { | ||
| Name = "dogImageDetails", | ||
| Type = typeof(NonNullGraphType<ImageDetailsType>), | ||
| Resolver = new FuncFieldResolver<object, object>(async context => | ||
| { | ||
| using var scope = _serviceProvider.CreateScope(); | ||
| var imageDetailsRepository = scope.ServiceProvider.GetRequiredService<DogImageDetailsRepository>(); | ||
| var imageDetails = await imageDetailsRepository.GetDogImageDetails(); | ||
| return imageDetails; | ||
| }) | ||
| }; | ||
|
|
||
| fields.Add(imageDetailsField); | ||
|
|
||
| return fields; | ||
| } | ||
| } | ||
|
|
||
| public class CatSayOperation : ICatOperation | ||
| { | ||
| public IEnumerable<IFieldType> GetFields() | ||
| { | ||
| var fields = new List<IFieldType>(); | ||
| var sayField = new FieldType | ||
| { | ||
| Name = "say", | ||
| Type = typeof(StringGraphType), | ||
| Resolver = new FuncFieldResolver<object, object>(context => "meow meow meow") | ||
| }; | ||
|
|
||
| fields.Add(sayField); | ||
|
|
||
| return fields; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| using GraphQL.Types; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Example.GraphQL | ||
| { | ||
| public class Queries | ||
| { | ||
| public class DogQuery : ObjectGraphType<object> | ||
| { | ||
| public DogQuery(IEnumerable<IDogOperation> dogOperations) | ||
| { | ||
| foreach (var dogOperation in dogOperations) | ||
| { | ||
| var fields = dogOperation.GetFields(); | ||
| foreach (var field in fields) | ||
| { | ||
| AddField((FieldType)field); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class CatQuery : ObjectGraphType<object> | ||
| { | ||
| public CatQuery(IEnumerable<ICatOperation> catOperations) | ||
| { | ||
| foreach (var catOperation in catOperations) | ||
| { | ||
| var fields = catOperation.GetFields(); | ||
| foreach (var field in fields) | ||
| { | ||
| AddField((FieldType)field); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using GraphQL.Types; | ||
|
|
||
| namespace Example.GraphQL | ||
| { | ||
| public class DogType : ObjectGraphType<Dog> | ||
| { | ||
| public DogType() | ||
| { | ||
| Field(x => x.Breed); | ||
| } | ||
| } | ||
|
|
||
| public class CatType : ObjectGraphType<Dog> | ||
| { | ||
| public CatType() | ||
| { | ||
| Field(x => x.Breed); | ||
| } | ||
| } | ||
|
|
||
| public class ImageDetailsType : ObjectGraphType<ImageDetails> | ||
| { | ||
| public ImageDetailsType() | ||
| { | ||
| Field(x => x.Url); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| namespace Example | ||
| { | ||
| public interface IBreed | ||
| { | ||
| public string Breed { get; set; } | ||
| } | ||
|
|
||
| public class Dog : IBreed | ||
| { | ||
| public string Breed { get; set; } | ||
| } | ||
|
|
||
| public class Cat : IBreed | ||
| { | ||
| public string Breed { get; set; } | ||
sungam3r marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public class ImageDetails | ||
| { | ||
| public string Url { get; set; } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace Example.Repositories; | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| public class CatRepository | ||
| { | ||
| private static readonly List<Cat> Cats = new() | ||
| { | ||
| new Cat { Breed = "Abyssinian" }, | ||
| new Cat { Breed = "American Bobtail" }, | ||
| new Cat { Breed = "Burmese" } | ||
| }; | ||
|
|
||
| public IEnumerable<Cat> GetCats() => Cats.AsEnumerable(); | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/AspNetCoreMulti/Example/Repositories/DogImageDetailsRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||
| 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 | ||||
| { | ||||
| private readonly IHttpClientFactory _httpClientFactory; | ||||
|
|
||||
| public DogImageDetailsRepository(IHttpClientFactory httpClientFactory) | ||||
| { | ||||
| _httpClientFactory = httpClientFactory; | ||||
| } | ||||
|
|
||||
| public async Task<ImageDetails> GetDogImageDetails() | ||||
| { | ||||
| try | ||||
| { | ||||
| var client = _httpClientFactory.CreateClient("DogsApi"); | ||||
| var result = await client.GetStringAsync("api/breeds/image/random"); | ||||
| var apiResponse = JsonSerializer.Deserialize<DogsImageApiResponse>(result); | ||||
|
|
||||
| return new ImageDetails { Url = apiResponse.Message }; | ||||
| } | ||||
| catch (Exception ex) | ||||
| { | ||||
|
|
||||
|
||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace Example.Repositories; | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| public class DogRepository | ||
| { | ||
| private static readonly List<Dog> Dogs = new() | ||
| { | ||
| new Dog { Breed = "Doberman" }, | ||
| new Dog { Breed = "Pit Bull" }, | ||
| new Dog { Breed = "German Shepard" } | ||
| }; | ||
|
|
||
| public IEnumerable<Dog> GetDogs() => Dogs.AsEnumerable(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's better to use
AutoSchemahere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give me some more details here? Do you have any doc handy for
AutoSchema? I'm thinking it is some way to set things up automatically.One reason I am doing the way I am right now, is to be non-opinionated on the setup. I give control to the implementor of
IQueryto give me the fields as they see fit. They can subsequently use reflection or any other controlled mechanism to set things up and finally return the fields which I would then attach to the root query. This is especially useful when the application is large, and consumers need several ways to opt in/out of things.Again, I might be talking about something different but will wait for information on
AutoSchema.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not really sure how the use of
AutoSchemafits into your example.AutoSchemais useful when creating a type-first schema, where the schema is automatically constructed from CLR types. Your sample does not use those features. Moreover, you are attempting to use methods from multiple classes to assemble a large root graph type. An auto-generated root query type would be contrary to that goal.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can see a demonstration of a type-first schema here:
https://github.com/graphql-dotnet/graphql-dotnet/tree/master/src/GraphQL.StarWars.TypeFirst
The entire schema can be constructed via:
You will notice there are no 'graph types' but rather simple CLR classes decorated with attributes where necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my own graphs, I use a slight variation of this to allow for DI injection of scoped services, which is not possible with the sample seen there. However, the code exists in the tests here:
https://github.com/graphql-dotnet/graphql-dotnet/blob/master/src/GraphQL.Tests/Bugs/Issue2932_DemoDIGraphType.cs
I also published it as a NuGet package here:
https://www.nuget.org/packages/Shane32.GraphQL.DI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EmmanuelPonnudurai I'm fine with example as is without
AutoSchema.