Add EntityFramework Core Dynamic IQueryable support to GraphQL.
Build Azure | |
Codecov | |
NuGet | |
MyGet (preview) |
With this project you can easily expose all properties from the EF Entities as searchable fields on the GraphQL query.
public class Room
{
[Key]
public int Id { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public bool AllowedSmoking { get; set; }
public RoomStatus Status { get; set; }
}
public class RoomType : ObjectGraphType<Room>
{
public RoomType()
{
Field(x => x.Id);
Field(x => x.Name);
Field(x => x.Number);
Field(x => x.AllowedSmoking);
Field<RoomStatusType>(nameof(RoomModel.Status));
}
}
query {
rooms (allowedSmoking: false) {
name
number
allowedSmoking
status
}
}
It's also possible to add support for an OrderBy field, just add the .SupportOrderBy();
in the code.
query {
rooms (orderBy: "name desc") {
name
number
status
}
}
It's also possible to add support for Paging, just add the .SupportPaging();
in the code.
query {
roomsWithPaging (page: 1, pageSize: 2) {
id
name
number
status
}
}
public void ConfigureServices(IServiceCollection services)
{
+ services.Configure<QueryArgumentInfoListBuilderOptions>(Configuration.GetSection("QueryArgumentInfoListBuilderOptions"));
+ services.AddGraphQLEntityFrameworkCoreDynamicLinq();
}
public class MyHotelQuery : ObjectGraphType
{
public MyHotelQuery(MyHotelRepository myHotelRepository, IQueryArgumentInfoListBuilder builder)
{
1 var roomQueryArgumentList = builder.Build<RoomType>()
2 .Exclude("Id")
3 .SupportOrderBy()
4 .SupportPaging();
Field<ListGraphType<RoomType>>("rooms",
5 arguments: roomQueryArgumentList.ToQueryArguments(),
resolve: context => myHotelRepository.GetRoomsQuery()
6 .ApplyQueryArguments(roomQueryArgumentList, context)
.ToList()
);
}
}
- Use the
IQueryArgumentInfoListBuilder
to build all possible arguments based on the fields from the GraphQL type (e.g.RoomType
) - Optionally include/exclude some properties which should not be searchable (this can also be a wildcard like
*Id
) - Optionally add support for OrderBy (argument-name will be
OrderBy
) - Optionally add support for Paging (argument-names will be
Page
andPageSize
) - Call the
.ToQueryArguments()
to create a newQueryArguments
object. - Call the
ApplyQueryArguments
extension method to apply the search criteria (optionally the OrderBy and Paging)
See example projec: examples/MyHotel for more details.
- Microsoft.EntityFrameworkCore.DynamicLinq
- EntityFramework Core IQueryable
- GraphQL
- My example project is based on ebicoglu/AspNetCoreGraphQL-MyHotel.