├── LICENSE ├── README.md ├── pom.xml └── src └── main ├── java └── com │ └── techshard │ └── graphql │ ├── Application.java │ ├── dao │ ├── entity │ │ └── Vehicle.java │ └── repository │ │ └── VehicleRepository.java │ ├── mutation │ └── VehicleMutation.java │ ├── query │ └── VehicleQuery.java │ └── service │ └── VehicleService.java └── resources ├── application.properties └── graphql └── vehicleql.graphqls /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Swathi Prasad 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # graphql-with-spring-boot 2 | A sample application with GraphQL and Spring Boot 3 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.techshard.graphql 8 | springboot-graphql 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 2.1.6.RELEASE 15 | 16 | 17 | 18 | 19 | UTF-8 20 | UTF-8 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-jpa 31 | 32 | 33 | com.graphql-java 34 | graphql-spring-boot-starter 35 | 5.0.2 36 | 37 | 38 | com.graphql-java 39 | graphql-java-tools 40 | 5.2.4 41 | 42 | 43 | com.graphql-java 44 | graphiql-spring-boot-starter 45 | 5.0.2 46 | 47 | 48 | com.h2database 49 | h2 50 | runtime 51 | 52 | 53 | org.projectlombok 54 | lombok 55 | 1.18.8 56 | true 57 | 58 | 59 | 60 | 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-maven-plugin 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /src/main/java/com/techshard/graphql/Application.java: -------------------------------------------------------------------------------- 1 | package com.techshard.graphql; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 6 | 7 | @SpringBootApplication 8 | public class Application extends SpringBootServletInitializer { 9 | 10 | public static void main(String[] args) { 11 | SpringApplication.run(Application.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/techshard/graphql/dao/entity/Vehicle.java: -------------------------------------------------------------------------------- 1 | package com.techshard.graphql.dao.entity; 2 | 3 | import lombok.Data; 4 | import lombok.EqualsAndHashCode; 5 | 6 | import javax.persistence.*; 7 | import java.io.Serializable; 8 | import java.time.LocalDate; 9 | 10 | @Data 11 | @EqualsAndHashCode 12 | @Entity 13 | public class Vehicle implements Serializable { 14 | 15 | private static final long serialVersionUID = 1L; 16 | 17 | @Id 18 | @Column(name = "ID", nullable = false) 19 | @GeneratedValue(strategy = GenerationType.AUTO) 20 | private int id; 21 | 22 | @Column(name = "type", nullable = false) 23 | private String type; 24 | 25 | @Column(name = "model_code", nullable = false) 26 | private String modelCode; 27 | 28 | @Column(name = "brand_name") 29 | private String brandName; 30 | 31 | @Column(name = "launch_date") 32 | private LocalDate launchDate; 33 | 34 | private transient String formattedDate; 35 | 36 | // Getter and setter 37 | public String getFormattedDate() { 38 | return getLaunchDate().toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/techshard/graphql/dao/repository/VehicleRepository.java: -------------------------------------------------------------------------------- 1 | package com.techshard.graphql.dao.repository; 2 | 3 | import com.techshard.graphql.dao.entity.Vehicle; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface VehicleRepository extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/techshard/graphql/mutation/VehicleMutation.java: -------------------------------------------------------------------------------- 1 | package com.techshard.graphql.mutation; 2 | 3 | import com.coxautodev.graphql.tools.GraphQLMutationResolver; 4 | import com.techshard.graphql.dao.entity.Vehicle; 5 | import com.techshard.graphql.service.VehicleService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.time.LocalDate; 10 | 11 | @Component 12 | public class VehicleMutation implements GraphQLMutationResolver { 13 | 14 | @Autowired 15 | private VehicleService vehicleService; 16 | 17 | public Vehicle createVehicle(final String type, final String modelCode, final String brandName, final String launchDate) { 18 | return this.vehicleService.createVehicle(type, modelCode, brandName, launchDate); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/techshard/graphql/query/VehicleQuery.java: -------------------------------------------------------------------------------- 1 | package com.techshard.graphql.query; 2 | 3 | import com.coxautodev.graphql.tools.GraphQLQueryResolver; 4 | import com.techshard.graphql.dao.entity.Vehicle; 5 | import com.techshard.graphql.service.VehicleService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.util.List; 10 | import java.util.Optional; 11 | 12 | @Component 13 | public class VehicleQuery implements GraphQLQueryResolver { 14 | 15 | @Autowired 16 | private VehicleService vehicleService; 17 | 18 | public List getVehicles(final int count) { 19 | return this.vehicleService.getAllVehicles(count); 20 | } 21 | 22 | public Optional getVehicle(final int id) { 23 | return this.vehicleService.getVehicle(id); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/techshard/graphql/service/VehicleService.java: -------------------------------------------------------------------------------- 1 | package com.techshard.graphql.service; 2 | 3 | import com.techshard.graphql.dao.entity.Vehicle; 4 | import com.techshard.graphql.dao.repository.VehicleRepository; 5 | import org.springframework.stereotype.Service; 6 | import org.springframework.transaction.annotation.Transactional; 7 | 8 | import java.time.LocalDate; 9 | import java.util.List; 10 | import java.util.Optional; 11 | import java.util.stream.Collectors; 12 | 13 | @Service 14 | public class VehicleService { 15 | 16 | private final VehicleRepository vehicleRepository ; 17 | 18 | public VehicleService(final VehicleRepository vehicleRepository) { 19 | this.vehicleRepository = vehicleRepository ; 20 | } 21 | 22 | @Transactional 23 | public Vehicle createVehicle(final String type,final String modelCode, final String brandName, final String launchDate) { 24 | final Vehicle vehicle = new Vehicle(); 25 | vehicle.setType(type); 26 | vehicle.setModelCode(modelCode); 27 | vehicle.setBrandName(brandName); 28 | vehicle.setLaunchDate(LocalDate.parse(launchDate)); 29 | return this.vehicleRepository.save(vehicle); 30 | } 31 | 32 | @Transactional(readOnly = true) 33 | public List getAllVehicles(final int count) { 34 | return this.vehicleRepository.findAll().stream().limit(count).collect(Collectors.toList()); 35 | } 36 | 37 | @Transactional(readOnly = true) 38 | public Optional getVehicle(final int id) { 39 | return this.vehicleRepository.findById(id); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swathisprasad/graphql-with-spring-boot/abe49a0ca50c40d71c3ab3272d1b3972b2adc26e/src/main/resources/application.properties -------------------------------------------------------------------------------- /src/main/resources/graphql/vehicleql.graphqls: -------------------------------------------------------------------------------- 1 | type Vehicle { 2 | id: ID!, 3 | type: String, 4 | modelCode: String, 5 | brandName: String, 6 | launchDate: String 7 | } 8 | 9 | type Query { 10 | vehicles(count: Int):[Vehicle] 11 | vehicle(id: ID):Vehicle 12 | } 13 | 14 | type Mutation { 15 | createVehicle(type: String!, modelCode: String!, brandName: String, launchDate: String):Vehicle 16 | } --------------------------------------------------------------------------------