Croot Blog

Home About Tech Hobby Archive

Gradle + SpringBoot+ GraphQL

๐Ÿ’ก
์ „์ฒด ์†Œ์Šค ์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ  ํ•ด์ฃผ์„ธ์š”.

ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ

  • VSCode 1.86.0
  • Gradle 8.6
  • SpringBoot 3.2.3
  • java 17
  • mariaDB (feat. dbeaver)

์„ค์น˜

Database

  1. DBeaver ์„ค์น˜
  2. โ€œachroโ€ ์ด๋ฆ„์˜ Database ์ƒ์„ฑ (๋ณธ ์˜ˆ์ œ๋Š” MariaDB ๊ธฐ๋ณธ ์„ค์ • ์ด์šฉ)

JDK 17 ์„ค์น˜

https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html

VSCode Extensions

VSCode settings

// ~/.vscode/settings.json

{
    "java.compile.nullAnalysis.mode": "automatic",
    "java.configuration.updateBuildConfiguration": "automatic"
}

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

  1. Ctrl + Shift + P or Command + Shift + P
  2. Java: Create Java Projectโ€ฆ ์„ ํƒ
  3. Select the project type โ‡’ Spring Boot
  4. Select prject type. โ‡’ Gradle project
  5. Specify Spring Boot version โ‡’ 3.2.3
  6. Specify project language. โ‡’ Java
  7. Input Group Id for your project. โ‡’ com.croot
  8. Input Artifact Id for your project. โ‡’ achro
  9. Specify packaging type. โ‡’ Jar
  10. Specify Java version โ‡’ 17
  11. Search dependencies. โ‡’ none

Gradle ์„ค์ •

~/build.gradle

// build.gradle

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.2.3'
	id 'io.spring.dependency-management' version '1.1.4'
}

java {
	group = 'com.achro'
	version = '0.0.1-SNAPSHOT'
	sourceCompatibility = '17'
}

repositories {
    mavenCentral()
}

dependencies {
	// Spring Boot
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'com.graphql-java-kickstart:graphql-spring-boot-starter:15.0.0'
    testImplementation 'com.graphql-java-kickstart:graphql-spring-boot-starter-test:15.0.0'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'

	// DB
	runtimeOnly 'org.mariadb.jdbc:mariadb-java-client:3.3.3'
}

Gradle ์˜์กด์„ฑ ๊ด€๋ฆฌ ์†์„ฑ

- `complieClasspath` : ํ”„๋กœ์ ํŠธ ์†Œ์Šค ์ฝ”๋“œ ์ปดํŒŒ์ผ ์‹œ ํ•„์š”ํ•œ ๋ชจ๋“  ํด๋ž˜์Šค, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
- `runtimeClasspath` : ํ”„๋กœ์ ํŠธ ์‹คํ–‰ ์‹œ ํ•„์š”ํ•œ ๋ชจ๋“  ํด๋ž˜์Šค, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
- `runtimeOnly` : ๋Ÿฐํƒ€์ž„ ์‹œ์ ์—๋งŒ ํ•„์š”ํ•  ๊ฒฝ์šฐ
- `compileOnly` : ์ปดํŒŒ์ผ ์‹œ์ ์—๋งŒ ํ•„์š”ํ•  ๊ฒฝ์šฐ
- `implementation` : ์ปดํŒŒ์ผ ๋ฐ ๋Ÿฐํƒ€์ž„ ๋ชจ๋‘ ํ•„์š”

ํ”„๋กœ์ ํŠธ ์„ค์ •

~/src/main/resources/application.properties

# application.properties

server.port=8000
spring.application.name=achro

graphql.servlet.enabled=true
graphql.altair.enabled=true
graphql.playground.enabled=true


# DB Properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/achro
spring.datasource.username=root
spring.datasource.password=

~/src/main/resources/graphql/schema.graphqls

// schema.graphqls

schema{
    query: Query
}

type Member{
    id: ID!
    name: String!
    age: Int!
}

type Query{
    getMember(id: ID!): Member
    getAllMembers: [Member]
}

๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ ๋ฐ ์†Œ์Šค์ฝ”๋“œ

~/src/main/java/com/croot/achro
|- model/
|- repository/
|- service/
`- ApiApplication.java

model/

`Member.java` ```java // ~/src/main/java/com/croot/achro/model/Member.java package com.croot.achro.model; import lombok.Getter; import lombok.Setter; import jakarta.persistence.*; @Entity @Table(name="MEMBER") @Getter @Setter public class Member { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "MEMBER_ID") private Integer id; @Column(name = "MEMBER_NAME") private String name; @Column(name = "MEMBER_AGE") private Integer age; } ```

repository/

`MemberRepository.java` ```java // ~/src/main/java/com/croot/achro/repository/MemberRepository.java package com.croot.achro.repository; import com.croot.achro.model.Member; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface MemberRepository extends JpaRepository<Member, Integer> { } ```

service/

`MemberService.java` ```java // ~/src/main/java/com/croot/achro/service/MemberService.java package com.croot.achro.service; import graphql.kickstart.tools.GraphQLQueryResolver; import java.util.List; import org.springframework.stereotype.Service; import com.croot.achro.model.Member; import com.croot.achro.repository.MemberRepository; @Service public class MemberService implements GraphQLQueryResolver { private final MemberRepository memberRepository; public MemberService(MemberRepository memberRepository) { this.memberRepository = memberRepository; } public List getAllMembers(){ return memberRepository.findAll(); } public Member getMember(Integer id){ return memberRepository.findById(id).get(); } } ``` </details>
`LoadDataService.java` : ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ธฐ ์œ„ํ•œ ์„œ๋น„์Šค ```java // ~/src/main/java/com/croot/achro/service/LoadDataService.java package com.croot.achro.service; import com.croot.achro.model.Member; import com.croot.achro.repository.MemberRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import jakarta.annotation.PostConstruct; @Service public class DataLoaderService { @Autowired private MemberRepository memberRepository; @PostConstruct public void load() { // ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ for (int i = 1; i <= 10; i++) { Member member = new Member(); member.setName("Member " + i); // Member ์ด๋ฆ„ ์„ค์ • member.setAge(i * 10); // Member ๋‚˜์ด ์„ค์ • memberRepository.save(member); } } } ```
## ์‹คํ–‰ 1. [http://localhost:8000/playground](http://localhost:8000/playground) ์ ‘์† 2. ์ฟผ๋ฆฌ ์ž‘์„ฑ ```graphql query { getAllMembers{ id name age } } ``` 3. ๊ฒฐ๊ณผ ํ™•์ธ ![0](/assets/img/2024-03-19-Gradle-+-SpringBoot+-GraphQL.md/0.png)_Untitled.png_ ## REFS. GraphQL Java Kickstart Getting started - [https://www.graphql-java-kickstart.com/spring-boot/getting-started/](https://www.graphql-java-kickstart.com/spring-boot/getting-started/) ## ERROR.LOG **Prob.** VSCode์—์„œ `build.gradle` ์ˆ˜์ • **Solv.** 1. ํ•ด๋‹น ํŒŒ์ผ ์šฐํด๋ฆญ 2. โ€œReload Projectsโ€ ๋ฅผ ์„ ํƒ ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค. **Prob.** Run Java ์‹œ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ ๋ฐœ์ƒ. ```javascript Type definition for root mutation type 'Mutation' not found! ``` **Solv.** `~/src/main/resources/graphql/schema.graphqls` ์— โ€œMutationโ€์„ ์ •์˜ํ–ˆ์œผ๋‚˜ ๋ฆฌ์กธ๋ฒ„(Resolver)๊ฐ€ ์—†์–ด์„œ ๋ฐœ์ƒ. 1. schema.graphqls ์ˆ˜์ • or Resolver ์ž‘์„ฑ