https://github.com/stefh/graphql.entityframeworkcore.dynamiclinq
GraphQL extensions for EntityFrameworkCore.DynamicLinq to automatically configure the query arguments and execute the query
https://github.com/stefh/graphql.entityframeworkcore.dynamiclinq
Last synced: 12 months ago
JSON representation
GraphQL extensions for EntityFrameworkCore.DynamicLinq to automatically configure the query arguments and execute the query
- Host: GitHub
- URL: https://github.com/stefh/graphql.entityframeworkcore.dynamiclinq
- Owner: StefH
- License: mit
- Created: 2019-08-08T20:20:14.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-04-28T06:58:39.000Z (about 1 year ago)
- Last Synced: 2025-06-20T04:33:43.026Z (12 months ago)
- Language: C#
- Size: 2.54 MB
- Stars: 12
- Watchers: 3
- Forks: 4
- Open Issues: 55
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# GraphQL.EntityFrameworkCore.DynamicLinq
Add EntityFramework Core Dynamic IQueryable support to GraphQL.
## Info
| | |
|-|-|
| **Build Azure** | [](https://dev.azure.com/stef/GraphQL.EntityFrameworkCore.DynamicLinq/_build/latest?definitionId=26) |
| **Codecov** | [](https://codecov.io/gh/StefH/GraphQL.EntityFrameworkCore.DynamicLinq) |
| **NuGet** | [](https://www.nuget.org/packages/GraphQL.EntityFrameworkCore.DynamicLinq)
| **MyGet (preview)** | [](https://www.myget.org/feed/graphql_entityframeworkcore_dynamiclinq/package/nuget/GraphQL.EntityFrameworkCore.DynamicLinq) |
# How To
With this project you can easily expose all properties from the EF Entities as searchable fields on the GraphQL query.
## Entity Type
``` c#
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; }
}
```
## GraphQL Type
``` c#
public class RoomType : ObjectGraphType
{
public RoomType()
{
Field(x => x.Id);
Field(x => x.Name);
Field(x => x.Number);
Field(x => x.AllowedSmoking);
Field(nameof(RoomModel.Status));
}
}
```
# Execute the Query
#### Filter on `allowedSmoking`
``` js
query {
rooms (allowedSmoking: false) {
name
number
allowedSmoking
status
}
}
```
#### OrderBy `name`
It's also possible to add support for an **OrderBy** field, just add the `.SupportOrderBy();` in the code.
``` js
query {
rooms (orderBy: "name desc") {
name
number
status
}
}
```
#### Paging
It's also possible to add support for **Paging**, just add the `.SupportPaging();` in the code.
``` js
query {
roomsWithPaging (page: 1, pageSize: 2) {
id
name
number
status
}
}
```
# How to use
### Add the required dependency injections
``` diff
public void ConfigureServices(IServiceCollection services)
{
+ services.Configure(Configuration.GetSection("QueryArgumentInfoListBuilderOptions"));
+ services.AddGraphQLEntityFrameworkCoreDynamicLinq();
}
```
### Update your GraphQL Query
``` c#
public class MyHotelQuery : ObjectGraphType
{
public MyHotelQuery(MyHotelRepository myHotelRepository, IQueryArgumentInfoListBuilder builder)
{
1 var roomQueryArgumentList = builder.Build()
2 .Exclude("Id")
3 .SupportOrderBy()
4 .SupportPaging();
Field>("rooms",
5 arguments: roomQueryArgumentList.ToQueryArguments(),
resolve: context => myHotelRepository.GetRoomsQuery()
6 .ApplyQueryArguments(roomQueryArgumentList, context)
.ToList()
);
}
}
```
1. Use the `IQueryArgumentInfoListBuilder` to build all possible arguments based on the fields from the GraphQL type (e.g. `RoomType`)
2. Optionally include/exclude some properties which should not be searchable (this can also be a wildcard like `*Id`)
3. Optionally add support for OrderBy (argument-name will be `OrderBy`)
4. Optionally add support for Paging (argument-names will be `Page` and `PageSize`)
5. Call the `.ToQueryArguments()` to create a new `QueryArguments` object.
6. Call the `ApplyQueryArguments` extension method to apply the search criteria (optionally the OrderBy and Paging)
### Example
See example projec: [examples/MyHotel](https://github.com/StefH/GraphQL.EntityFrameworkCore.DynamicLinq/tree/master/examples/MyHotel) for more details.
# References
- [Microsoft.EntityFrameworkCore.DynamicLinq](https://github.com/StefH/System.Linq.Dynamic.Core)
- [EntityFramework Core IQueryable](https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbset-1.system-linq-iqueryable-provider)
- [GraphQL](https://github.com/graphql-dotnet/graphql-dotnet)
- My example project is based on [ebicoglu/AspNetCoreGraphQL-MyHotel](https://github.com/ebicoglu/AspNetCoreGraphQL-MyHotel).