├── .gitattributes ├── .gitignore ├── .mvn └── wrapper │ └── maven-wrapper.properties ├── README.md ├── compose.yaml ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── java │ └── dev │ │ └── danvega │ │ ├── Application.java │ │ ├── DataLoader.java │ │ └── book │ │ ├── Book.java │ │ └── BookRepository.java └── resources │ ├── application.properties │ └── graphql │ └── schema.graphqls └── test └── java └── dev └── danvega ├── ApplicationTests.java └── book └── BookGraphQlIntTest.java /.gitattributes: -------------------------------------------------------------------------------- 1 | /mvnw text eol=lf 2 | *.cmd text eol=crlf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one 2 | # or more contributor license agreements. See the NOTICE file 3 | # distributed with this work for additional information 4 | # regarding copyright ownership. The ASF licenses this file 5 | # to you under the Apache License, Version 2.0 (the 6 | # "License"); you may not use this file except in compliance 7 | # with the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, 12 | # software distributed under the License is distributed on an 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | # KIND, either express or implied. See the License for the 15 | # specific language governing permissions and limitations 16 | # under the License. 17 | wrapperVersion=3.3.2 18 | distributionType=only-script 19 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot GraphQL Book Management System 2 | 3 | A modern book management system built with Spring Boot and GraphQL, demonstrating how to implement Query by Example (QBE) pattern with JPA repositories. 4 | 5 | ## Overview 6 | 7 | This project showcases a robust implementation of a GraphQL API for managing books, featuring: 8 | 9 | - GraphQL API with flexible querying capabilities 10 | - Spring Data JPA with Query by Example support 11 | - PostgreSQL database integration 12 | - Docker Compose for local development 13 | - Comprehensive test coverage using TestContainers 14 | 15 | ## Why Query by Example (QBE)? 16 | 17 | Query by Example significantly simplifies dynamic querying in your application. Here's a comparison of implementing the same search functionality with and without QBE: 18 | 19 | ### Traditional Approach (Without QBE) 20 | 21 | ```java 22 | @RestController 23 | @RequestMapping("/api/books") 24 | public class BookController { 25 | 26 | @Autowired 27 | private BookRepository bookRepository; 28 | 29 | @GetMapping("/search") 30 | public List searchBooks( 31 | @RequestParam(required = false) String title, 32 | @RequestParam(required = false) String author, 33 | @RequestParam(required = false) Integer publishedYear) { 34 | 35 | return bookRepository.findAll(new Specification() { 36 | @Override 37 | public Predicate toPredicate(Root root, CriteriaQuery query, 38 | CriteriaBuilder cb) { 39 | List predicates = new ArrayList<>(); 40 | 41 | if (title != null) { 42 | predicates.add(cb.like(root.get("title"), "%" + title + "%")); 43 | } 44 | if (author != null) { 45 | predicates.add(cb.like(root.get("author"), "%" + author + "%")); 46 | } 47 | if (publishedYear != null) { 48 | predicates.add(cb.equal(root.get("publishedYear"), publishedYear)); 49 | } 50 | 51 | return cb.and(predicates.toArray(new Predicate[0])); 52 | } 53 | }); 54 | } 55 | } 56 | ``` 57 | 58 | You can also write custom repository methods but this gets cluttered when you want to support every possible combination. 59 | 60 | ```java 61 | @Repository 62 | public interface BookRepository extends JpaRepository { 63 | 64 | // Basic search methods 65 | List findByTitle(String title); 66 | List findByAuthor(String author); 67 | List findByPublishedYear(Integer year); 68 | 69 | // Partial matches 70 | List findByTitleContainingIgnoreCase(String title); 71 | List findByAuthorContainingIgnoreCase(String author); 72 | 73 | // Multiple criteria - exact matches 74 | List findByTitleAndAuthor(String title, String author); 75 | List findByTitleAndPublishedYear(String title, Integer year); 76 | List findByAuthorAndPublishedYear(String author, Integer year); 77 | List findByTitleAndAuthorAndPublishedYear(String title, String author, Integer year); 78 | 79 | // Multiple criteria - partial matches 80 | List findByTitleContainingIgnoreCaseAndAuthorContainingIgnoreCase(String title, String author); 81 | List findByTitleContainingIgnoreCaseAndPublishedYear(String title, Integer year); 82 | List findByAuthorContainingIgnoreCaseAndPublishedYear(String author, Integer year); 83 | List findByTitleContainingIgnoreCaseAndAuthorContainingIgnoreCaseAndPublishedYear( 84 | String title, String author, Integer year); 85 | 86 | // Ordered results 87 | List findByPublishedYearOrderByTitleAsc(Integer year); 88 | List findByAuthorOrderByPublishedYearDesc(String author); 89 | List findByTitleContainingIgnoreCaseOrderByPublishedYearDesc(String title); 90 | 91 | // Complex queries with @Query 92 | @Query("SELECT b FROM Book b WHERE b.publishedYear >= :startYear AND b.publishedYear <= :endYear") 93 | List findBooksInYearRange(@Param("startYear") Integer startYear, @Param("endYear") Integer endYear); 94 | 95 | @Query("SELECT b FROM Book b WHERE LOWER(b.title) LIKE LOWER(CONCAT('%', :keyword, '%')) " + 96 | "OR LOWER(b.author) LIKE LOWER(CONCAT('%', :keyword, '%'))") 97 | List findByTitleOrAuthorContaining(@Param("keyword") String keyword); 98 | 99 | // Pagination support 100 | Page findByPublishedYear(Integer year, Pageable pageable); 101 | Page findByAuthorContainingIgnoreCase(String author, Pageable pageable); 102 | 103 | // Count queries 104 | Long countByPublishedYear(Integer year); 105 | Long countByAuthor(String author); 106 | 107 | // Exists queries 108 | boolean existsByTitleIgnoreCase(String title); 109 | boolean existsByAuthorAndPublishedYear(String author, Integer year); 110 | 111 | // Delete queries 112 | void deleteByPublishedYearBefore(Integer year); 113 | 114 | // Custom specification for complex dynamic queries 115 | default List findByCustomCriteria(String title, String author, Integer yearFrom, Integer yearTo) { 116 | return findAll((Specification) (root, query, cb) -> { 117 | List predicates = new ArrayList<>(); 118 | 119 | if (title != null && !title.isEmpty()) { 120 | predicates.add(cb.like(cb.lower(root.get("title")), 121 | "%" + title.toLowerCase() + "%")); 122 | } 123 | 124 | if (author != null && !author.isEmpty()) { 125 | predicates.add(cb.like(cb.lower(root.get("author")), 126 | "%" + author.toLowerCase() + "%")); 127 | } 128 | 129 | if (yearFrom != null) { 130 | predicates.add(cb.greaterThanOrEqualTo(root.get("publishedYear"), yearFrom)); 131 | } 132 | 133 | if (yearTo != null) { 134 | predicates.add(cb.lessThanOrEqualTo(root.get("publishedYear"), yearTo)); 135 | } 136 | 137 | return cb.and(predicates.toArray(new Predicate[0])); 138 | }); 139 | } 140 | } 141 | ``` 142 | 143 | ### With Query by Example 144 | 145 | ```java 146 | @GraphQlRepository 147 | public interface BookRepository extends JpaRepository, 148 | QueryByExampleExecutor { 149 | } 150 | 151 | // GraphQL Query 152 | query { 153 | books(book: { 154 | author: "Craig Walls" 155 | publishedYear: 2022 156 | }) { 157 | id 158 | title 159 | author 160 | publishedYear 161 | } 162 | } 163 | ``` 164 | 165 | 166 | **Benefits of QBE** 167 | 168 | - Reduced Boilerplate: Eliminates the need for complex specification classes or multiple repository methods 169 | - Type Safety: Provides compile-time type checking for your queries 170 | - Flexible Querying: Easily handle any combination of search criteria without writing custom methods 171 | - GraphQL Integration: Naturally fits with GraphQL's flexible query structure 172 | - Maintainable Code: Less code to maintain and test 173 | - Dynamic Queries: Handle multiple search parameters without complex conditional logic 174 | 175 | ## Project Requirements 176 | 177 | - Java 23 178 | - Docker and Docker Compose 179 | - Maven 3.9.x 180 | - PostgreSQL 16 181 | 182 | ## Dependencies 183 | 184 | Main dependencies included in this project: 185 | 186 | ```xml 187 | 188 | 189 | org.springframework.boot 190 | spring-boot-starter-data-jpa 191 | 192 | 193 | org.springframework.boot 194 | spring-boot-starter-graphql 195 | 196 | 197 | org.springframework.boot 198 | spring-boot-starter-web 199 | 200 | 201 | org.postgresql 202 | postgresql 203 | runtime 204 | 205 | 206 | ``` 207 | 208 | ## Getting Started 209 | 210 | 1. Ensure Docker is running on your system 211 | 2. The application will automatically start PostgreSQL using Docker Compose with the following configuration: 212 | ```yaml 213 | services: 214 | postgres: 215 | image: 'postgres:latest' 216 | environment: 217 | - 'POSTGRES_DB=graphql_books' 218 | - 'POSTGRES_PASSWORD=password' 219 | - 'POSTGRES_USER=user' 220 | ports: 221 | - '5432:5432' 222 | ``` 223 | 224 | ## Running the Application 225 | 226 | 1. Start the application: 227 | ```bash 228 | ./mvnw spring-boot:run 229 | ``` 230 | 231 | 2. Access GraphiQL interface at: http://localhost:8080/graphiql 232 | 233 | ## GraphQL API Usage 234 | 235 | The API supports the following queries: 236 | 237 | ### Query all books 238 | ```graphql 239 | query { 240 | books { 241 | id 242 | title 243 | author 244 | publishedYear 245 | } 246 | } 247 | ``` 248 | 249 | ### Query book by ID 250 | ```graphql 251 | query { 252 | book(id: "1") { 253 | title 254 | author 255 | publishedYear 256 | } 257 | } 258 | ``` 259 | 260 | ### Query books using example 261 | ```graphql 262 | query { 263 | books(book: { 264 | author: "Craig Walls" 265 | publishedYear: 2022 266 | }) { 267 | id 268 | title 269 | author 270 | publishedYear 271 | } 272 | } 273 | ``` 274 | 275 | ## Data Model 276 | 277 | The Book entity contains the following fields: 278 | 279 | ```java 280 | public class Book { 281 | private Long id; 282 | private String title; 283 | private String author; 284 | private Integer publishedYear; 285 | } 286 | ``` 287 | 288 | ## Testing 289 | 290 | The project includes comprehensive integration tests using TestContainers. Key test cases: 291 | 292 | - Finding all books 293 | - Finding books by ID 294 | - Finding books by example (QBE) 295 | - Testing with no matches 296 | - Author pattern matching 297 | 298 | Run tests using: 299 | ```bash 300 | ./mvnw test 301 | ``` 302 | 303 | ## Development Features 304 | 305 | - GraphiQL interface enabled for development 306 | - Automatic schema generation 307 | - JPA show-sql enabled for debugging 308 | - Spring DevTools for rapid development 309 | 310 | ## Configuration 311 | 312 | Key application properties: 313 | 314 | ```properties 315 | spring.application.name=graphql-qbe 316 | spring.graphql.graphiql.enabled=true 317 | spring.jpa.hibernate.ddl-auto=create-drop 318 | spring.jpa.show-sql=true 319 | ``` 320 | 321 | ## Additional Resources 322 | 323 | - [Spring GraphQL Documentation](https://docs.spring.io/spring-graphql/docs/current/reference/html/) 324 | - [GraphQL Java Documentation](https://www.graphql-java.com/documentation/getting-started) 325 | - [Spring Data JPA Documentation](https://docs.spring.io/spring-data/jpa/docs/current/reference/html/) -------------------------------------------------------------------------------- /compose.yaml: -------------------------------------------------------------------------------- 1 | services: 2 | postgres: 3 | image: 'postgres:latest' 4 | environment: 5 | - 'POSTGRES_DB=graphql_books' 6 | - 'POSTGRES_PASSWORD=password' 7 | - 'POSTGRES_USER=user' 8 | ports: 9 | - '5432:5432' 10 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Apache Maven Wrapper startup batch script, version 3.3.2 23 | # 24 | # Optional ENV vars 25 | # ----------------- 26 | # JAVA_HOME - location of a JDK home dir, required when download maven via java source 27 | # MVNW_REPOURL - repo url base for downloading maven distribution 28 | # MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 29 | # MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output 30 | # ---------------------------------------------------------------------------- 31 | 32 | set -euf 33 | [ "${MVNW_VERBOSE-}" != debug ] || set -x 34 | 35 | # OS specific support. 36 | native_path() { printf %s\\n "$1"; } 37 | case "$(uname)" in 38 | CYGWIN* | MINGW*) 39 | [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" 40 | native_path() { cygpath --path --windows "$1"; } 41 | ;; 42 | esac 43 | 44 | # set JAVACMD and JAVACCMD 45 | set_java_home() { 46 | # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched 47 | if [ -n "${JAVA_HOME-}" ]; then 48 | if [ -x "$JAVA_HOME/jre/sh/java" ]; then 49 | # IBM's JDK on AIX uses strange locations for the executables 50 | JAVACMD="$JAVA_HOME/jre/sh/java" 51 | JAVACCMD="$JAVA_HOME/jre/sh/javac" 52 | else 53 | JAVACMD="$JAVA_HOME/bin/java" 54 | JAVACCMD="$JAVA_HOME/bin/javac" 55 | 56 | if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then 57 | echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 58 | echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 59 | return 1 60 | fi 61 | fi 62 | else 63 | JAVACMD="$( 64 | 'set' +e 65 | 'unset' -f command 2>/dev/null 66 | 'command' -v java 67 | )" || : 68 | JAVACCMD="$( 69 | 'set' +e 70 | 'unset' -f command 2>/dev/null 71 | 'command' -v javac 72 | )" || : 73 | 74 | if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then 75 | echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 76 | return 1 77 | fi 78 | fi 79 | } 80 | 81 | # hash string like Java String::hashCode 82 | hash_string() { 83 | str="${1:-}" h=0 84 | while [ -n "$str" ]; do 85 | char="${str%"${str#?}"}" 86 | h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) 87 | str="${str#?}" 88 | done 89 | printf %x\\n $h 90 | } 91 | 92 | verbose() { :; } 93 | [ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } 94 | 95 | die() { 96 | printf %s\\n "$1" >&2 97 | exit 1 98 | } 99 | 100 | trim() { 101 | # MWRAPPER-139: 102 | # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. 103 | # Needed for removing poorly interpreted newline sequences when running in more 104 | # exotic environments such as mingw bash on Windows. 105 | printf "%s" "${1}" | tr -d '[:space:]' 106 | } 107 | 108 | # parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties 109 | while IFS="=" read -r key value; do 110 | case "${key-}" in 111 | distributionUrl) distributionUrl=$(trim "${value-}") ;; 112 | distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; 113 | esac 114 | done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" 115 | [ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" 116 | 117 | case "${distributionUrl##*/}" in 118 | maven-mvnd-*bin.*) 119 | MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ 120 | case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in 121 | *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; 122 | :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; 123 | :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; 124 | :Linux*x86_64*) distributionPlatform=linux-amd64 ;; 125 | *) 126 | echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 127 | distributionPlatform=linux-amd64 128 | ;; 129 | esac 130 | distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" 131 | ;; 132 | maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; 133 | *) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; 134 | esac 135 | 136 | # apply MVNW_REPOURL and calculate MAVEN_HOME 137 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 138 | [ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" 139 | distributionUrlName="${distributionUrl##*/}" 140 | distributionUrlNameMain="${distributionUrlName%.*}" 141 | distributionUrlNameMain="${distributionUrlNameMain%-bin}" 142 | MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" 143 | MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" 144 | 145 | exec_maven() { 146 | unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : 147 | exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" 148 | } 149 | 150 | if [ -d "$MAVEN_HOME" ]; then 151 | verbose "found existing MAVEN_HOME at $MAVEN_HOME" 152 | exec_maven "$@" 153 | fi 154 | 155 | case "${distributionUrl-}" in 156 | *?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; 157 | *) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; 158 | esac 159 | 160 | # prepare tmp dir 161 | if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then 162 | clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } 163 | trap clean HUP INT TERM EXIT 164 | else 165 | die "cannot create temp dir" 166 | fi 167 | 168 | mkdir -p -- "${MAVEN_HOME%/*}" 169 | 170 | # Download and Install Apache Maven 171 | verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 172 | verbose "Downloading from: $distributionUrl" 173 | verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 174 | 175 | # select .zip or .tar.gz 176 | if ! command -v unzip >/dev/null; then 177 | distributionUrl="${distributionUrl%.zip}.tar.gz" 178 | distributionUrlName="${distributionUrl##*/}" 179 | fi 180 | 181 | # verbose opt 182 | __MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' 183 | [ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v 184 | 185 | # normalize http auth 186 | case "${MVNW_PASSWORD:+has-password}" in 187 | '') MVNW_USERNAME='' MVNW_PASSWORD='' ;; 188 | has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; 189 | esac 190 | 191 | if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then 192 | verbose "Found wget ... using wget" 193 | wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" 194 | elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then 195 | verbose "Found curl ... using curl" 196 | curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" 197 | elif set_java_home; then 198 | verbose "Falling back to use Java to download" 199 | javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" 200 | targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" 201 | cat >"$javaSource" <<-END 202 | public class Downloader extends java.net.Authenticator 203 | { 204 | protected java.net.PasswordAuthentication getPasswordAuthentication() 205 | { 206 | return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); 207 | } 208 | public static void main( String[] args ) throws Exception 209 | { 210 | setDefault( new Downloader() ); 211 | java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); 212 | } 213 | } 214 | END 215 | # For Cygwin/MinGW, switch paths to Windows format before running javac and java 216 | verbose " - Compiling Downloader.java ..." 217 | "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" 218 | verbose " - Running Downloader.java ..." 219 | "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" 220 | fi 221 | 222 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 223 | if [ -n "${distributionSha256Sum-}" ]; then 224 | distributionSha256Result=false 225 | if [ "$MVN_CMD" = mvnd.sh ]; then 226 | echo "Checksum validation is not supported for maven-mvnd." >&2 227 | echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 228 | exit 1 229 | elif command -v sha256sum >/dev/null; then 230 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then 231 | distributionSha256Result=true 232 | fi 233 | elif command -v shasum >/dev/null; then 234 | if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then 235 | distributionSha256Result=true 236 | fi 237 | else 238 | echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 239 | echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 240 | exit 1 241 | fi 242 | if [ $distributionSha256Result = false ]; then 243 | echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 244 | echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 245 | exit 1 246 | fi 247 | fi 248 | 249 | # unzip and move 250 | if command -v unzip >/dev/null; then 251 | unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" 252 | else 253 | tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" 254 | fi 255 | printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" 256 | mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" 257 | 258 | clean || : 259 | exec_maven "$@" 260 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | <# : batch portion 2 | @REM ---------------------------------------------------------------------------- 3 | @REM Licensed to the Apache Software Foundation (ASF) under one 4 | @REM or more contributor license agreements. See the NOTICE file 5 | @REM distributed with this work for additional information 6 | @REM regarding copyright ownership. The ASF licenses this file 7 | @REM to you under the Apache License, Version 2.0 (the 8 | @REM "License"); you may not use this file except in compliance 9 | @REM with the License. You may obtain a copy of the License at 10 | @REM 11 | @REM http://www.apache.org/licenses/LICENSE-2.0 12 | @REM 13 | @REM Unless required by applicable law or agreed to in writing, 14 | @REM software distributed under the License is distributed on an 15 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | @REM KIND, either express or implied. See the License for the 17 | @REM specific language governing permissions and limitations 18 | @REM under the License. 19 | @REM ---------------------------------------------------------------------------- 20 | 21 | @REM ---------------------------------------------------------------------------- 22 | @REM Apache Maven Wrapper startup batch script, version 3.3.2 23 | @REM 24 | @REM Optional ENV vars 25 | @REM MVNW_REPOURL - repo url base for downloading maven distribution 26 | @REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven 27 | @REM MVNW_VERBOSE - true: enable verbose log; others: silence the output 28 | @REM ---------------------------------------------------------------------------- 29 | 30 | @IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) 31 | @SET __MVNW_CMD__= 32 | @SET __MVNW_ERROR__= 33 | @SET __MVNW_PSMODULEP_SAVE=%PSModulePath% 34 | @SET PSModulePath= 35 | @FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( 36 | IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) 37 | ) 38 | @SET PSModulePath=%__MVNW_PSMODULEP_SAVE% 39 | @SET __MVNW_PSMODULEP_SAVE= 40 | @SET __MVNW_ARG0_NAME__= 41 | @SET MVNW_USERNAME= 42 | @SET MVNW_PASSWORD= 43 | @IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) 44 | @echo Cannot start maven from wrapper >&2 && exit /b 1 45 | @GOTO :EOF 46 | : end batch / begin powershell #> 47 | 48 | $ErrorActionPreference = "Stop" 49 | if ($env:MVNW_VERBOSE -eq "true") { 50 | $VerbosePreference = "Continue" 51 | } 52 | 53 | # calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties 54 | $distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl 55 | if (!$distributionUrl) { 56 | Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" 57 | } 58 | 59 | switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { 60 | "maven-mvnd-*" { 61 | $USE_MVND = $true 62 | $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" 63 | $MVN_CMD = "mvnd.cmd" 64 | break 65 | } 66 | default { 67 | $USE_MVND = $false 68 | $MVN_CMD = $script -replace '^mvnw','mvn' 69 | break 70 | } 71 | } 72 | 73 | # apply MVNW_REPOURL and calculate MAVEN_HOME 74 | # maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ 75 | if ($env:MVNW_REPOURL) { 76 | $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } 77 | $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" 78 | } 79 | $distributionUrlName = $distributionUrl -replace '^.*/','' 80 | $distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' 81 | $MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" 82 | if ($env:MAVEN_USER_HOME) { 83 | $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" 84 | } 85 | $MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' 86 | $MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" 87 | 88 | if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { 89 | Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" 90 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 91 | exit $? 92 | } 93 | 94 | if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { 95 | Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" 96 | } 97 | 98 | # prepare tmp dir 99 | $TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile 100 | $TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" 101 | $TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null 102 | trap { 103 | if ($TMP_DOWNLOAD_DIR.Exists) { 104 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 105 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 106 | } 107 | } 108 | 109 | New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null 110 | 111 | # Download and Install Apache Maven 112 | Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." 113 | Write-Verbose "Downloading from: $distributionUrl" 114 | Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" 115 | 116 | $webclient = New-Object System.Net.WebClient 117 | if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { 118 | $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) 119 | } 120 | [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 121 | $webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null 122 | 123 | # If specified, validate the SHA-256 sum of the Maven distribution zip file 124 | $distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum 125 | if ($distributionSha256Sum) { 126 | if ($USE_MVND) { 127 | Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." 128 | } 129 | Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash 130 | if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { 131 | Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." 132 | } 133 | } 134 | 135 | # unzip and move 136 | Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null 137 | Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null 138 | try { 139 | Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null 140 | } catch { 141 | if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { 142 | Write-Error "fail to move MAVEN_HOME" 143 | } 144 | } finally { 145 | try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } 146 | catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } 147 | } 148 | 149 | Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" 150 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 3.3.5 9 | 10 | 11 | dev.danvega 12 | graphql-qbe 13 | 0.0.1-SNAPSHOT 14 | graphql-qbe 15 | Demo project for Spring Boot 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 23 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-data-jpa 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-graphql 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-web 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-devtools 49 | runtime 50 | true 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-docker-compose 55 | runtime 56 | true 57 | 58 | 59 | org.postgresql 60 | postgresql 61 | runtime 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-starter-test 66 | test 67 | 68 | 69 | org.springframework.boot 70 | spring-boot-testcontainers 71 | test 72 | 73 | 74 | org.springframework 75 | spring-webflux 76 | test 77 | 78 | 79 | org.springframework.graphql 80 | spring-graphql-test 81 | test 82 | 83 | 84 | org.testcontainers 85 | junit-jupiter 86 | test 87 | 88 | 89 | org.testcontainers 90 | postgresql 91 | test 92 | 93 | 94 | 95 | 96 | 97 | 98 | org.springframework.boot 99 | spring-boot-maven-plugin 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /src/main/java/dev/danvega/Application.java: -------------------------------------------------------------------------------- 1 | package dev.danvega; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/dev/danvega/DataLoader.java: -------------------------------------------------------------------------------- 1 | package dev.danvega; 2 | 3 | import dev.danvega.book.Book; 4 | import dev.danvega.book.BookRepository; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class DataLoader implements CommandLineRunner { 10 | 11 | private final BookRepository repository; 12 | 13 | public DataLoader(BookRepository repository) { 14 | this.repository = repository; 15 | } 16 | 17 | @Override 18 | public void run(String... args) { 19 | repository.save(new Book("Spring Boot Up & Running", "Mark Heckler", 2021)); 20 | repository.save(new Book("Cloud Native Spring in Action", "Thomas Vitale", 2022)); 21 | repository.save(new Book("Spring Security in Action", "Laurentiu Spilca", 2020)); 22 | repository.save(new Book("Spring Boot in Practice", "Somnath Musib", 2022)); 23 | repository.save(new Book("Pro Spring 5", "Iuliana Cosmina", 2017)); 24 | repository.save(new Book("Spring in Action", "Craig Walls", 2018)); 25 | repository.save(new Book("Spring Microservices in Action", "John Carnell", 2017)); 26 | repository.save(new Book("Java: The Complete Reference", "Herbert Schildt", 2018)); 27 | repository.save(new Book("Effective Java", "Joshua Bloch", 2018)); 28 | repository.save(new Book("Java Concurrency in Practice", "Brian Goetz", 2006)); 29 | repository.save(new Book("Head First Java", "Kathy Sierra and Bert Bates", 2005)); 30 | repository.save(new Book("Java Performance: The Definitive Guide", "Scott Oaks", 2014)); 31 | repository.save(new Book("Java Puzzlers: Traps, Pitfalls, and Corner Cases", "Joshua Bloch and Neal Gafter", 2005)); 32 | repository.save(new Book("Java 8 in Action", "Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft", 2014)); 33 | repository.save(new Book("Modern Java in Action", "Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft", 2018)); 34 | repository.save(new Book("Java: A Beginner's Guide", "Herbert Schildt", 2018)); 35 | repository.save(new Book("Spring 5 Design Patterns", "Dinesh Rajput", 2017)); 36 | repository.save(new Book("Spring MVC: A Tutorial", "Paul Deck", 2014)); 37 | repository.save(new Book("Spring Batch in Action", "Arnaud Cogoluegnes, Thierry Templier, Gary Gregory, and Olivier Bazoud", 2011)); 38 | repository.save(new Book("Spring Integration in Action", "Mark Fisher, Jonas Partner, Marius Bogoevici, and Iwein Fuld", 2012)); 39 | repository.save(new Book("Spring Data", "Mark Pollack, Oliver Gierke, Thomas Risberg, Jon Brisbin, and Michael Hunger", 2012)); 40 | repository.save(new Book("Spring Recipes: A Problem-Solution Approach", "Gary Mak, Josh Long, and Daniel Rubio", 2010)); 41 | repository.save(new Book("Spring Microservices", "Rajesh RV", 2016)); 42 | repository.save(new Book("Spring Boot Cookbook", "Alex Antonov", 2015)); 43 | } 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/dev/danvega/book/Book.java: -------------------------------------------------------------------------------- 1 | package dev.danvega.book; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.GeneratedValue; 5 | import jakarta.persistence.Id; 6 | 7 | @Entity 8 | public class Book { 9 | 10 | @Id 11 | @GeneratedValue 12 | private Long id; 13 | private String title; 14 | private String author; 15 | private Integer publishedYear; 16 | 17 | public Book() {} 18 | 19 | public Book(String title, String author, Integer publishedYear) { 20 | this.title = title; 21 | this.author = author; 22 | this.publishedYear = publishedYear; 23 | } 24 | 25 | public Long getId() { 26 | return id; 27 | } 28 | 29 | public void setId(Long id) { 30 | this.id = id; 31 | } 32 | 33 | public String getTitle() { 34 | return title; 35 | } 36 | 37 | public void setTitle(String title) { 38 | this.title = title; 39 | } 40 | 41 | public String getAuthor() { 42 | return author; 43 | } 44 | 45 | public void setAuthor(String author) { 46 | this.author = author; 47 | } 48 | 49 | public Integer getPublishedYear() { 50 | return publishedYear; 51 | } 52 | 53 | public void setPublishedYear(Integer publishedYear) { 54 | this.publishedYear = publishedYear; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "Book{" + 60 | "id=" + id + 61 | ", title='" + title + '\'' + 62 | ", author='" + author + '\'' + 63 | ", publishedYear=" + publishedYear + 64 | '}'; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/dev/danvega/book/BookRepository.java: -------------------------------------------------------------------------------- 1 | package dev.danvega.book; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.repository.query.QueryByExampleExecutor; 5 | import org.springframework.graphql.data.GraphQlRepository; 6 | 7 | @GraphQlRepository 8 | public interface BookRepository extends JpaRepository, QueryByExampleExecutor { 9 | 10 | } -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=graphql-qbe 2 | spring.graphql.graphiql.enabled=true 3 | 4 | spring.jpa.hibernate.ddl-auto=create-drop 5 | spring.jpa.show-sql=true -------------------------------------------------------------------------------- /src/main/resources/graphql/schema.graphqls: -------------------------------------------------------------------------------- 1 | type Book { 2 | id: ID! 3 | title: String! 4 | author: String! 5 | publishedYear: Int! 6 | } 7 | 8 | input BookInput { 9 | title: String 10 | author: String 11 | publishedYear: Int 12 | } 13 | 14 | type Query { 15 | books(book: BookInput): [Book]! 16 | book(id: ID!): Book 17 | } -------------------------------------------------------------------------------- /src/test/java/dev/danvega/ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package dev.danvega; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class ApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/dev/danvega/book/BookGraphQlIntTest.java: -------------------------------------------------------------------------------- 1 | package dev.danvega.book; 2 | 3 | import org.junit.jupiter.api.BeforeEach; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.autoconfigure.graphql.tester.AutoConfigureHttpGraphQlTester; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import org.springframework.boot.testcontainers.service.connection.ServiceConnection; 9 | import org.springframework.graphql.test.tester.HttpGraphQlTester; 10 | import org.testcontainers.containers.PostgreSQLContainer; 11 | import org.testcontainers.junit.jupiter.Container; 12 | import org.testcontainers.junit.jupiter.Testcontainers; 13 | import org.testcontainers.utility.DockerImageName; 14 | 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 19 | @AutoConfigureHttpGraphQlTester 20 | @Testcontainers 21 | public class BookGraphQlIntTest { 22 | 23 | @Container 24 | @ServiceConnection 25 | static PostgreSQLContainer postgres = new PostgreSQLContainer<>(DockerImageName.parse("postgres:16-alpine")); 26 | 27 | @Autowired 28 | private HttpGraphQlTester graphQlTester; 29 | 30 | @Autowired 31 | private BookRepository bookRepository; 32 | 33 | @BeforeEach 34 | void setUp() { 35 | bookRepository.deleteAll(); 36 | bookRepository.saveAll(List.of( 37 | new Book("Spring in Action", "Craig Walls", 2022), 38 | new Book("Spring Boot in Practice", "Somnath Musib", 2023), 39 | new Book("Learning GraphQL", "Eve Porcello", 2023), 40 | new Book("Java in Action", "Raoul-Gabriel Urma", 2021) 41 | )); 42 | } 43 | 44 | @Test 45 | void testFindAll() { 46 | String query = """ 47 | query { 48 | books { 49 | id 50 | title 51 | author 52 | publishedYear 53 | } 54 | } 55 | """; 56 | 57 | graphQlTester.document(query) 58 | .execute() 59 | .path("data.books") 60 | .entityList(Book.class) 61 | .hasSize(4); 62 | } 63 | 64 | @Test 65 | void testFindById() { 66 | Book savedBook = bookRepository.findAll().get(0); 67 | 68 | String query = """ 69 | query($id: ID!) { 70 | book(id: $id) { 71 | id 72 | title 73 | author 74 | publishedYear 75 | } 76 | } 77 | """; 78 | 79 | graphQlTester.document(query) 80 | .variable("id", savedBook.getId().toString()) 81 | .execute() 82 | .path("data.book") 83 | .entity(Book.class) 84 | .satisfies(book -> { 85 | assert book.getId().equals(savedBook.getId()); 86 | assert book.getTitle().equals(savedBook.getTitle()); 87 | }); 88 | } 89 | 90 | @Test 91 | void testFindByExample_ExactTitle() { 92 | String document = """ 93 | query($bookInput: BookInput!) { 94 | books(book: $bookInput) { 95 | id 96 | title 97 | author 98 | publishedYear 99 | } 100 | } 101 | """; 102 | 103 | graphQlTester.document(document) 104 | .variable("bookInput", Map.of("title", "Spring in Action")) 105 | .execute() 106 | .path("data.books") 107 | .entityList(Book.class) 108 | .hasSize(1) 109 | .satisfies(books -> { 110 | assert books.get(0).getTitle().equals("Spring in Action"); 111 | }); 112 | } 113 | 114 | @Test 115 | void testFindByExample_PublishedYearAndAuthorPattern() { 116 | String document = """ 117 | query($bookInput: BookInput!) { 118 | books(book: $bookInput) { 119 | id 120 | title 121 | author 122 | publishedYear 123 | } 124 | } 125 | """; 126 | 127 | graphQlTester.document(document) 128 | .variable("bookInput", Map.of("publishedYear",2023,"author","Eve Porcello")) 129 | .execute() 130 | .path("data.books") 131 | .entityList(Book.class) 132 | .hasSize(1) 133 | .satisfies(books -> { 134 | assert books.get(0).getPublishedYear().equals(2023); 135 | assert books.get(0).getAuthor().contains("Eve Porcello"); 136 | assert books.get(0).getTitle().equals("Learning GraphQL"); 137 | }); 138 | } 139 | 140 | 141 | @Test 142 | void testFindByExample_NoMatch() { 143 | String document = """ 144 | query($bookInput: BookInput!) { 145 | books(book: $bookInput) { 146 | id 147 | title 148 | author 149 | publishedYear 150 | } 151 | } 152 | """; 153 | 154 | graphQlTester.document(document) 155 | .variable("bookInput", Map.of("title","Non Existent Book")) 156 | .execute() 157 | .path("data.books") 158 | .entityList(Book.class) 159 | .hasSize(0); 160 | } 161 | 162 | } 163 | --------------------------------------------------------------------------------