Code Monkey home page Code Monkey logo

graphql-dynamic-filter-dotnetcore's Introduction

graphql-dynamic-filter-dotnetcore

Simple filter for .net WebApi that enables your endpoint to query your requests by url.

It provides ways to query, order and page your webapi.

Further features like select will be updated soon.

Get Started

First, download the package into your webapi project from nuget (https://www.nuget.org/packages/Graphql.DynamicFilter)

nuget install Graphql.DynamicFilter

After downloaded, go to your webapi and create an get endpoint receiving DynamicFilter<TClass> as parameter.

[HttpGet]
public Task<List<User>> Get(DynamicFilter<User> filter)

Now you can query your endpoint with the TClass on DynamicFilter properties. Check "Tests" folder on the project for examples.

DynamicFilter will transform your URI Queries into .Net Expressions. That way, you can use these expressions to filter your values into your database repository.

List<User> users = new List<User>()
{
    new User("Bruno", 27),
    new User("Fred", 33),
    new User("Albert", 37),
    new User("Lucao", 23),
    new User("Luide", 28)
};

[HttpGet]
public Task<List<User>> Get(DynamicFilter<User> filter)
{
    var result = this.users.Where(filter.Filter.Compile());

    return Task.FromResult(result.ToList());
}
  • Simple Query

Example Uri:

GET http://url?query=name=Bruno

Expression generated by Uri:

x => x.Name == "Bruno"
  • Complex Query

Example Uri:

GET http://url?query=name=Bruno,lastname%r,age>=27

Expression generated by Uri:

x => x.Name == "Bruno" 
  && x.LastName.ToLower().Contains("r") 
  && x.Age >= 27

You can add conditions to your query sorting your filters with a comma (,), as shown above.

  • Nested Query

Example Uri:

GET http://url?query=address.number=23

Expression generated by Uri:

x => x.Address.Number == 23

Ordering

You can also order your queries via DynamicFilter. You simply need to add an order parameter on your query, where you specify the property you'll use for order.

GET http://url?query=name=Bruno&order=name

Default order type is Ascending. You can specify the order type with an equals (=) Asc or Desc after the property.

GET http://url?query=name=Bruno&order=name=Asc

GET http://url?query=name=Bruno&order=name=Desc

On your DynamicFilter object received on the endpoint, you'll get the orderType as an Enum, this way you can order by the type specified on enum.

public Task<List<User>> Get(DynamicFilter<User> filter)
{
    var result = this.users.Where(filter.Filter.Compile());

    if (filter.Order != null)
    {
        if (filter.OrderType == OrderType.Asc)
            result = result.OrderBy(filter.Order.Compile());
        else
            result = result.OrderByDescending(filter.Order.Compile());
    }

    return Task.FromResult(result.ToList());
}

Pagging

To page, you simply needs to add the parameters page and pagesize on your get request.

GET http://url?query=name%b&order=name&page=1&pagesize=10
result = result.Skip(filter.Page).Take(filter.PageSize);

Select

To select, you simply needs to add the parameter select with the properties you want to select from. It will render either an linq select and a plain string select.

GET http://url?select=name,age

Filters

  • Equals (=)
GET http://url?query=name=Bruno
  • Contains (%)
GET http://url?query=name%b
  • Contains Case Sensitive (%%)
GET http://url?query=name%%B
  • GreaterThan (>)
GET http://url?query=age>15
  • GreaterOrEqual (>=)
GET http://url?query=age>=15
  • LessThan (<)
GET http://url?query=age<15
  • LessOrEqual (<=)
GET http://url?query=age<=15
  • NotEquals (!=)
GET http://url?query=age!=15

graphql-dynamic-filter-dotnetcore's People

Contributors

brunogr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.