https://github.com/titaniumit/graphql-filters
Provides a C# library to implement Graphql Filters
https://github.com/titaniumit/graphql-filters
dotnet graphql graphql-server
Last synced: 5 months ago
JSON representation
Provides a C# library to implement Graphql Filters
- Host: GitHub
- URL: https://github.com/titaniumit/graphql-filters
- Owner: TitaniumIT
- License: mit
- Created: 2023-11-13T18:26:46.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-23T23:39:42.000Z (over 1 year ago)
- Last Synced: 2025-06-15T05:04:40.079Z (about 1 year ago)
- Topics: dotnet, graphql, graphql-server
- Language: C#
- Homepage:
- Size: 1.92 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# Introduction
[](https://github.com/TitaniumIT/graphql-filters/actions/workflows/publishpackage.yml)
[](https://titaniumit.github.io/graphql-filters/coverage/index.html)
[](https://titaniumit.github.io/graphql-filters/tests/GraphQL.Filters.Specflow.html)
An extention on GraphQL.net to add filtering on clr types
# Example graphql filters
Filters are based on a clrType/interface
Using AddFilter as argument. The graphql type for the filters are automaticly added
* FilterGraphType
* And/Or/Not types
* Condition type
* Any
## Simple filters
```graphql
condition:{
fieldName:Id
operator: equal
value: 1
}
```
```graphql
condition:{
fieldName: DateField
operator: equal
value: "10-10-2023"
}
```
### Operators on scalars
supported operators on scalartypes
* equal
* greater
* greaterOrEqual
* less
* lessOrEqual
* notEqual
## Booleans
supported
* and
* or
* not
## Collections
to filter collection members the any field on the filtertype is present
that contains per colletion field a member.
## Resolvers
Using filters in a Resolver.
```csharp
Field("Diver")
.AddFilter("filter").FilterType()
.Resolve(ctx =>
{
var filter = ctx.GetFilterExpression("filter");
var datasource = ctx.RequestServices!.GetRequiredService();
if (filter != null)
return datasource.Divers.SingleOrDefault(filter.Compile());
else
return null;
});
```
When getting a filter on parent for a child
```csharp
Field>("Dives")
.Resolve( ctx => {
var datasource = ctx.RequestServices!.GetRequiredService();
var expression = ctx.GetSubFilterExpression();
if( expression != null){
return datasource.Dives.Where( d => d.Diver?.Id == ctx.Source.Id).Where(expression.Compile());
} else{
return datasource.Dives.Where( d => d.Diver?.Id == ctx.Source.Id);
}
});
```
# query examples
```graphql
query GetDiverFixedFilter {
diver(filter:{
condition:{
fieldName:id
operator: equal
value: 1
}
}){
name
email
id
}
}
query GetDiverByEmail {
diver(filter:{
condition:{
fieldName: email
operator: equal
value: "john@divers.down"
}
}){
name
email
id
}
}
query GetDiversWithCoarseFilters {
divers(filter:{
any:{
courses:{
condition:{
fieldName: name
operator:equal
value: "OpenWater"
}
}
}
}){
name
email
id
}
}
query GetDiverFilterById($id: ValueScalar!) {
diver(filter:{
condition:{
fieldName:id
operator: equal
value: $id
}
}){
id
}
}
query GetDiversWithDivesAt($location:ValueScalar!){
divers(filter: {
any:{
dives:{
condition:{
fieldName:location
operator:equal
value: $location
}
}
}
}){
name
dives{
start
end
}
}
}
```
[example project](https://github.com/TitaniumIT/graphql-filters/tree/main/src/GraphQL.Filters/GraphQL.Filters.Examples)
# Setup
# Todo
* documentation
* more test cases
* custom filter functions
* configuration options like, casing.