/graphql-query

Instrument a graphql-java server with filters on lists

Primary LanguageKotlinGNU Affero General Public License v3.0AGPL-3.0

Build master

GraphQL-Query

Instrument a graphql-java server with filters on lists

This is an extension to the excellent graphql-java that adds a parameter to each list field that allows to filter the elements of that list using a simple predicate.

Given this schema:

type Query {
  result: [result]
}

type result {
  int: Int
  string: String
}

You could write the query:

{
  result(_filter: {
      int: {gt: 15),
      string: {eq: "hello"}
  }) {
    int
    string
  }
}

This would retrieve the list of results as usual and then remove everything where int is not greater than 15 or string not equal to "hello".

Usage

GraphQL-Query is available from maven central: https://search.maven.org/artifact/net.bloemsma.graphql.query/graphql-query

See in the file "TestSchema.kt" how to add FilterInstrumentation to your GraphQL object:

val graphQL: GraphQL = GraphQL
    .newGraphQL(oldSchema)
    .instrumentation(FilterInstrumentation())
    .build()

Limitations

  • Fields used in the _filter argument have to be selected, either directly or using a fragment, using their original name. Otherwise the data to test on is simply not found.
  • No calculations can be done, only comparisons of fields against literals.
  • There are very few operators.
  • No aggregations.
  • No pagination.
  • The code needs some serious cleanup!
  • It should be easier to add new operators.
  • Needs more tests.
  • NOT ready for production.