All Projects → intuit → graphql-filter-java

intuit / graphql-filter-java

Licence: Apache-2.0 license
This project is developed to help developers add filtering support to their graphql-java services

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to graphql-filter-java

gmail-gitlab-filtering
Google Apps Script for Gmail to filter and sort email from GitLab
Stars: ✭ 84 (+61.54%)
Mutual labels:  filters, filtering
Flutter filter selection
UI challenge for creating a filter selection screen with animations
Stars: ✭ 17 (-67.31%)
Mutual labels:  filters
Horizonsdk Ios
Horizon SDK for iOS
Stars: ✭ 171 (+228.85%)
Mutual labels:  filters
Iir1
IIR realtime filter library written in C++
Stars: ✭ 224 (+330.77%)
Mutual labels:  filters
React Imgpro
📷 Image Processing Component for React
Stars: ✭ 2,186 (+4103.85%)
Mutual labels:  filters
Optivideoeditor For Ios
Native Video editor : Video trim, Audio, Video merge, Slow and fast motion, Video transition, Text and image, Filters, etc...
Stars: ✭ 234 (+350%)
Mutual labels:  filters
Drf Url Filters
A django app to apply filters on drf querysets using query params with validations using voluptuous.
Stars: ✭ 150 (+188.46%)
Mutual labels:  filters
spring-graphql-sample
Spring GraphQL examples using Netflix DGS, GraphQL Java and Spring GraphQL
Stars: ✭ 67 (+28.85%)
Mutual labels:  graphql-java
spring-boot-react-ecommerce-app
eCommerce application based on the microservices architecture built using Spring Boot and ReactJS.
Stars: ✭ 221 (+325%)
Mutual labels:  spring-jpa
Linqit
Extend python lists with .NET's LINQ syntax for clean and fast coding. Also known as PINQ.
Stars: ✭ 222 (+326.92%)
Mutual labels:  filters
Agimagecontrols
cool tools for image edition
Stars: ✭ 217 (+317.31%)
Mutual labels:  filters
Decoda
A lightweight lexical string parser for BBCode styled markup.
Stars: ✭ 190 (+265.38%)
Mutual labels:  filters
Obscuracam
Photo and Video Filtering App for Privacy
Stars: ✭ 238 (+357.69%)
Mutual labels:  filters
Itemsjs
Full text, faceted, (almost) dependency free search engine in javascript
Stars: ✭ 179 (+244.23%)
Mutual labels:  filters
graphene-sqlalchemy-filter
Filters for Graphene SQLAlchemy integration
Stars: ✭ 117 (+125%)
Mutual labels:  filters
Gl React Instagramfilters
Instagram filters for gl-react and gl-react-native
Stars: ✭ 157 (+201.92%)
Mutual labels:  filters
Moogladders
🔉 Collected C++ implementations of the classic 4-pole moog ladder filter
Stars: ✭ 211 (+305.77%)
Mutual labels:  filters
Photofilters
photofilters library for flutter
Stars: ✭ 229 (+340.38%)
Mutual labels:  filters
susa
High Performance Computing (HPC) and Signal Processing Framework
Stars: ✭ 55 (+5.77%)
Mutual labels:  filters
django-admin-search
Modal filter for django admin
Stars: ✭ 60 (+15.38%)
Mutual labels:  filters

GraphQL-Filters

Build Status Apache 2

Overview

This library helps GraphQL developers build awesome APIs with fine grain filtering support.

Requirements

  • grahql-java (v13.x)
  • Java 8.x & Above

Features

This library will help create filter conditions which are dynamically created by combining any supported filter criteria field along with any of the supported logical operations including AND, OR and NOT. Consider the examples below.

Queries

OR

{
  searchEmployees (filter : {
      or : [{ firstName : {contains : "Saurabh"}},{ lastName : {equals : "Jaiswal"}}]
    }) {
      firstName
      lastName
      age
    }
}

AND

{
  searchEmployees (filter : {
      and : [{ firtName : {contains : "Saurabh"}},{ age : {gte : 25}}]
    }) {
      firstName
      lastName
      age
    }
}

AND with OR

{
  searchEmployees (filter : {
      and : [
        { firstName : {contains : "Saurabh"}},
        { or : [{ lastName: {equals : "Jaiswal"}},{ age: {gte: 25}}
        ]}]
    }) {
      firstName
      lastName
      age
    }
}

Usage

Dependency

<dependency>
    <groupId>com.intuit.graphql</groupId>
    <artifactId>graphql-filter-java</artifactId>
    <version>1.0.0</version>
</dependency>

Schema

# Define the query type
type Query {
   searchEmployees(filter: Filter): [Employee!]
 }
 
# Define the types
type Employee {
   firstName: String!
   lastName: String!
   age: Int!
 }
  
# Define filter input
input Filter {
   firstName: StringExpression
   lastName: StringExpression
   age: IntExpression
    
   and: [Filter!]
   or: [Filter!]
   not: Filter
}
 
# Define String expression
input StringExpression {
   equals: String
   contains: String
}
 
# Define Int Expression
input IntExpression {
   eq: Int
   gt: Int
   gte: Int
   lt: Int
   lte: Int
}

JPA Specification

Generates filter expression using JPA Specification for any SQL database.

@Repository
public interface EmployeeRepository extends JpaRepository<EmployeeEntity, String>, JpaSpecificationExecutor<EmployeeEntity> {
}
@Getter
@Setter
@Entity
@Table(name = "employee")
public class EmployeeEntity {
    private String firstName;
    private String lastName;
    private Integer age;
}
@Component
@Transactional
public class EmployeeService {
    @Autowired
    private EmployeeRepository employeeRepository;

    /**
     * Searched employees based on filter criteria.
     */ 
    public  List<EmployeeEntity> searchEmployees(DataFetchingEnvironment env) {
        List<EmployeeEntity> employees = null;
        Specification<EmployeeEntity> specification = getSpecification(env);
        if (specification != null) {
           employees = employeeRepository.findAll(specification);
        } else {
            employees = employeeRepository.findAll();
        }
        return employees;
    }
    
    /**
     * Generates the filter JPA specification
     */
    private Specification<EmployeeEntity> getSpecification(DataFetchingEnvironment env) {
        FilterExpression.FilterExpressionBuilder builder = FilterExpression.newFilterExpressionBuilder();
        FilterExpression filterExpression = builder.field(env.getField())
                .args(env.getArguments())
                .build();
        Specification<EmployeeEntity> specification = filterExpression.getExpression(ExpressionFormat.JPA);
        return specification;
    }
}

SQL WHERE

Generates SQL WHERE clause which can then be directly applied to any SQL database.

private String getExpression(DataFetchingEnvironment env) {
    FilterExpression.FilterExpressionBuilder builder = FilterExpression.newFilterExpressionBuilder();
    FilterExpression filterExpression = builder.field(env.getField())
        .args(env.getArguments())
        .build();
    String expression = filterExpression.getExpression(ExpressionFormat.SQL);
    return expression;   
}

Expression output

WHERE ((lastName = 'Jaiswal') OR (firstName LIKE '%Saurabh%'))

How it works?

When graphql-java receives and parses the source filter expression, it creates an AST in memory which contains all the fields, operators and values supplied in the source filter. The problem is the generated AST does not know about the valid rules of a correct logical expression with multiple filter criteria. In order to get a meaningful expression out of the source filter input, filter library parses the GraphQL generated AST and generates a new expression AST with correct syntax and semantics. After this step, the generated AST looks as shown below in memory.

(firstName contains Saurabh) and ((lastName equals Jaiswal) or (age gte 25))

Supported Formats

  • Infix String
  • SQL WHERE clause
  • JPA Specification

Supported Operators

Relational

  • String (EQUALS, CONTAINS, STARTS, ENDS)
  • Numeric (EQ, LT, GT, LTE, GTE)
  • Range (IN, BETWEEN)

Logical

  • AND
  • OR
  • NOT

Supported Database

  • MySQL

Complete GraphQL JPA Example

GraphQL Java Filtering With JPA Specification

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].