Skip to content

feat: add where relation attributes criteria expressions #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions .project

This file was deleted.

197 changes: 195 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Will return:

Query Wrapper with Where Criteria Expressions
-------------------------------------
This library supports flexible type safe criteria expressions with user-friendly SQL query syntax semantics using `where` arguments and `select` field to specify the entity graph query with entiy attribute names as a combination of logical expressions like OR, AND, EQ, NE, GT, GE, LT, LR, IN, NIN, IS_NULL, NOT_NULL, BETWEEN, NOT_BETWEEN.
This library supports flexible type safe criteria expressions with user-friendly SQL query syntax semantics using `where` arguments and `select` field to specify the entity graph query with entiy attribute names as a combination of logical expressions like EQ, NE, GT, GE, LT, LR, IN, NIN, IS_NULL, NOT_NULL, BETWEEN, NOT_BETWEEN. You can use logical AND/OR combinations in SQL criteria expressions to specify complex criterias to fetch your data from SQL database. If you omit, where argument, all entities will be returned.

For Example:

Expand All @@ -173,7 +173,200 @@ Will return:
}
}

You can use familiar SQL criteria expressions to specify complex criterias to fetch your data from SQL database. If you omit, where argument, all entities will be returned.
Relation Attributes in Where Criteria Expressions:
----------------------------
It is also possible to specify complex filters using many-to-one and one-to-many entity attributes in where criteria expressions with variable parameter bindings, i.e.

Given the following query with many-to-one relation with variables `{"authorId": 1 }` :

```
query($authorId: Long) {
Books(where: {
author: {id: {EQ: $authorId}}
}) {
select {
id
title
genre
author {
id
name
}
}
}
}
```

will result in

```
{
"data": {
"Books": {
"select": [
{
"id": 2,
"title": "War and Peace",
"genre": "NOVEL",
"author": {
"id": 1,
"name": "Leo Tolstoy"
}
},
{
"id": 3,
"title": "Anna Karenina",
"genre": "NOVEL",
"author": {
"id": 1,
"name": "Leo Tolstoy"
}
}
]
}
}
}
```

And given the following query with one-to-many relation:

```
query {
Authors(where: {
books: {genre: {IN: NOVEL}}
}) {
select {
id
name
books {
id
title
genre
}
}
}
}
```

will result in

```
{
"data": {
"Authors": {
"select": [
{
"id": 1,
"name": "Leo Tolstoy",
"books": [
{
"id": 2,
"title": "War and Peace",
"genre": "NOVEL"
},
{
"id": 3,
"title": "Anna Karenina",
"genre": "NOVEL"
}
]
}
]
}
}
}
```

It is possible to use compound criterias in where search expressions given:

```
query {
Authors(where: {
books: {
genre: {IN: NOVEL}
title: {LIKE: "War"}
}
}) {
select {
id
name
books {
id
title
genre
}
}
}
}
```

Will return filtered inner collection result:

```
{
"data": {
"Authors": {
"select": [
{
"id": 1,
"name": "Leo Tolstoy",
"books": [
{
"id": 2,
"title": "War and Peace",
"genre": "NOVEL"
}
]
}
]
}
}
}
```

It is also possible to filter inner collections as follows:

```
query {
Authors(where: {
books: {genre: {IN: NOVEL}}
}) {
select {
id
name
books(where: {title: {LIKE: "War"}}) {
id
title
genre
}
}
}
}
```

will result in

```
{
"data": {
"Authors": {
"select": [
{
"id": 1,
"name": "Leo Tolstoy",
"books": [
{
"id": 2,
"title": "War and Peace",
"genre": "NOVEL"
}
]
}
]
}
}
}
```

Collection Filtering
--------------------
Expand Down
56 changes: 0 additions & 56 deletions graphql-jpa-query-boot-starter/.project

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

Expand All @@ -32,6 +33,6 @@ public class Author {

String name;

@OneToMany(mappedBy="author")
@OneToMany(mappedBy="author", fetch=FetchType.LAZY)
Collection<Book> books;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

Expand All @@ -32,7 +33,7 @@ public class Book {

String title;

@ManyToOne
@ManyToOne(fetch=FetchType.LAZY)
Author author;

@Enumerated(EnumType.STRING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;

import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -50,14 +52,14 @@ public abstract class Character {
String name;

@GraphQLDescription("Who are the known friends to this character")
@ManyToMany
@JoinTable(name="character_friends",
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name="character_friends",
joinColumns=@JoinColumn(name="source_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="friend_id", referencedColumnName="id"))
Set<Character> friends;

@GraphQLDescription("What Star Wars episodes does this character appear in")
@ElementCollection(targetClass = Episode.class)
@ElementCollection(targetClass = Episode.class, fetch = FetchType.LAZY)
@Enumerated(EnumType.ORDINAL)
@OrderBy
Set<Episode> appearsIn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package com.introproventures.graphql.jpa.query.example.starwars;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;

import lombok.Data;

@Entity
Expand All @@ -39,7 +41,7 @@ public class CodeList {
boolean active;
String description;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
CodeList parent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Human extends Character {
@JoinColumn(name = "favorite_droid_id")
Droid favoriteDroid;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "gender_code_id")
CodeList gender;

Expand Down
Loading