An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# Introduction

[![publish Graphql Filters](https://github.com/TitaniumIT/graphql-filters/actions/workflows/publishpackage.yml/badge.svg)](https://github.com/TitaniumIT/graphql-filters/actions/workflows/publishpackage.yml)
[![coverage](docs/coverage/badge_combined.svg)](https://titaniumit.github.io/graphql-filters/coverage/index.html)
[![testoutcome](docs/testoutcome.svg)](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.