Skip to content

Latest commit

 

History

History
62 lines (50 loc) · 2.87 KB

filters.md

File metadata and controls

62 lines (50 loc) · 2.87 KB

Filters

Sometimes, in the context of constructing an EF query, it is not possible to know if any given item should be returned in the results. For example when performing authorization where the rules rules are pulled from a different system, and that information does not exist in the database.

Filters allows a custom function to be executed after the EF query execution and determine if any given node should be included in the result.

Notes:

  • When evaluated on nodes of a collection, excluded nodes will be removed from collection.
  • When evaluated on a property node, the value will be replaced with null.
  • When doing paging or counts, there is currently no smarts that adjust counts or pages sizes when items are excluded. If this is required submit a PR that adds this feature, or don't mix filters with paging.
  • The filter is passed the current User Context and the node item instance.
  • Filters will not be executed on null item instance.
  • A Type.IsAssignableFrom check will be performed to determine if an item instance should be filtered based on the <TItem>.

Signature:

public class Filters
{
    public delegate bool Filter<in TEntity>(object userContext, ClaimsPrincipal? userPrincipal, TEntity input)
        where TEntity : class;

    public delegate Task<bool> AsyncFilter<in TEntity>(object userContext, ClaimsPrincipal? userPrincipal, TEntity input)
        where TEntity : class;

snippet source | anchor

Usage:

public class MyEntity
{
    public string? Property { get; set; }
}

snippet source | anchor

var filters = new Filters();
filters.Add<MyEntity>(
    (userContext, userPrincipal, item) => item.Property != "Ignore");
EfGraphQLConventions.RegisterInContainer<MyDbContext>(
    services,
    resolveFilters: x => filters);

snippet source | anchor