-
Hello, I'm currently looking into introducing GraphQL into my solution for the first time.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
To understand better, take a look at the public class Message
{
[Id]
public int Id { get; set; }
[Name("Message")]
public string Value { get; set; } = null!;
public string From { get; set; } = null!;
public DateTime Sent { get; set; }
public IEnumerable<Message> Replies([FromServices] IChat chatService)
=> chatService.GetReplies(this);
} What you will notice is that the So that is how the This is applied to all types in the type-first coding model - but it affects the class Query
{
public User AuthenticatedUser { get; set; }
}
class User
{
public string Name { get; set; }
public IEnumerable<Message> Messages([FromServices] IChat chatService)
=> chatService.GetMessageFromUser(Name);
}
// during initialization:
ExecutionOptions.Root = new Query(new User("Sample User")); So now we understand why the query uses static methods - because by default Personally, I would agree with you that for the root The above library makes GraphQL act more like a ASP.NET MVC controller, where the controller is resolved by DI and then the proper method executed. Rewriting the public class QueryType : DIObjectGraphType<Query> { }
public class Query : DIObjectGraphBase<object>
{
private readonly IChat _chatService;
public Query(IChat chatService)
{
_chatService = chatService;
}
public Message? LastMessage() => _chatService.LastMessage;
public IEnumerable<Message> AllMessages(string? from = null)
=> from == null ? _chatService.GetAllMessages() : _chatService.GetMessageFromUser(from);
public int Count() => _chatService.Count;
}
public class MySchema : Schema
{
public MySchema(IServiceProvider serviceProvider, QueryType query) : base(serviceProvider)
{
Query = query;
}
}
// use .AddDIGraphTypes() in startup Within the
Sorry no docs, but see the sample project and notable files here:
|
Beta Was this translation helpful? Give feedback.
-
Keep in mind that without using Shane32.GraphQL.DI you could certainly do the following with no other changes to the sample project: public class Query
{
private readonly IChat _chatService;
public Query(IChat chatService)
{
_chatService = chatService;
}
public Message? LastMessage() => _chatService.LastMessage;
public IEnumerable<Message> AllMessages(string? from = null)
=> from == null ? _chatService.GetAllMessages() : _chatService.GetMessageFromUser(from);
public int Count() => _chatService.Count;
}
// in startup:
services.AddSingleton<Query>(); // or AddScoped if necessary
services.AddGraphQL(b => b
// other stuff
.ConfigureExecutionOptions(o => o.Root = o.RequestServices.GetRequiredService<Query>())
); But it won't work if you have both a Query and a Mutation root type set up like this, because within the |
Beta Was this translation helpful? Give feedback.
To understand better, take a look at the
Message
class. I'm going to add aReplies
method for discussion purposes also.What you will notice is that the
Message
class is a data model, not a service. It can contain service-like methods, but it's a data model with service methods layered on top. It would not make sense to have theMessage
class use DI to inj…