Skip to content
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

Support for c# extensions #136

Merged
merged 2 commits into from
Mar 3, 2019

Conversation

john-obrien
Copy link
Contributor

This PR introduces support for C# extensions to add to the definition of a query type.

This is particularly useful when the schema is defined across several projects / assemblies and you wish to stitch these together into a single graph.

// Actors.csproj
class Actor
{
   public Guid Id { get; set; }
   public string Name { get; set; }
}

// Movies.csproj
class Movie
{
    public Guid Id { get; set; }
    public string Title { get; set; }
}

// Schema.csproj
static class MovieExtensions
{
    public async Task<Actor[]> Actors(this Movie movie, [Inject] IActorRepository repository)
       => await repository.GetActorsPerMovie(movie.Id);
}
class Query
{
    public async Task<Movie> Movie([Inject] IMovieRepository repository, Guid id)
       => await repository.GetMovie(.id);
}

var response = await RequestHandler
  .New()
  .WithQuery<Query>()
  .WithQueryExtensions(typeof(MovieExtensions))
  .Generate();

@@ -12,6 +13,7 @@ public class GraphFieldInfo : GraphEntityInfo
public GraphFieldInfo(ITypeResolver typeResolver, MemberInfo field = null)
: base(typeResolver, field)
{
IsExtensionMethod = (AttributeProvider as MethodInfo)?.IsExtensionMethod() ?? false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can bring the null-check down to the extension method:

public static bool IsExtensionMethod(this MethodInfo methodInfo) => 
    methodInfo?.IsDefined(typeof(ExtensionAttribute), false) ?? false;

And thus, this line could be removed and the property getter expressed like so:

public bool IsExtensionMethod => (AttributeProvider as MethodInfo).IsExtensionMethod()

#nitpick

@tlil tlil merged commit 48b5a70 into graphql-dotnet:master Mar 3, 2019
rodolfotorres added a commit to rodolfotorres/conventions that referenced this pull request Mar 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants