LoginSignup
13
12

More than 3 years have passed since last update.

SpringBoot + Kotlin + GraphQLに雑入門

Last updated at Posted at 2019-05-04

記事としてはかなり粗いです。
サーバーサイド Kotlinに入門するため勉強記事になります。

ちなみに当方は、Androidエンジニアでサーバーサイド経験はゼロ

使用するもの

SpringBoot
Kotlin
GraphQL

環境

IntelliJ IDEA 2019.1 (Ultimate Edition)
JRE: 1.8.0_202-release-1483-b39 x86_64
macOS 10.14.4

実装1 まずはシンプルに

Graphqlのリクエストにversionがあれば、常に1.0を返す処理を作成する。

まずプロジェクト作成
プロジェクト名はkotlin-graphql-sampleとした

graphqlライブラリを追加

build.gradleを以下のように修正

コミット
https://github.com/ikemura23/kotlin-graphql-sample/commit/f34c03d9344b9258b489f5ef0e9e98d20c3e8c2f

build.gradle
// buildscriptを追加
buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        classpath "org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE"
    }
}

// apply pluginを追加
apply plugin: 'kotlin'
apply plugin: "kotlin-spring"
apply plugin: 'kotlin-kapt'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group 'kotlin'
version '1.0-SNAPSHOT'

repositories {
    jcenter() // 追加
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    // ここから追加
    compile 'com.graphql-java-kickstart:graphql-spring-boot-starter:5.7.3'

    // to embed Altair tool
    runtime 'com.graphql-java-kickstart:altair-spring-boot-starter:5.7.3'

    // to embed GraphiQL tool
    runtime 'com.graphql-java-kickstart:graphiql-spring-boot-starter:5.7.3'

    // to embed Voyager tool
    runtime 'com.graphql-java-kickstart:voyager-spring-boot-starter:5.7.3'

    implementation 'org.springframework.boot:spring-boot-devtools'

    // testing facilities
    testCompile 'com.graphql-java-kickstart:graphql-spring-boot-starter-test:5.7.3'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

MainApplicationクラスを作成

package demo

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication //アノテーション追加
class MainApplication {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            SpringApplication.run(MainApplication::class.java, *args)
        }
    }
} 

試しにMainApplicationを実行してみる。
ビルドログにエラーが出てなければOK

graphqlのschemeを作成する

schemeというのか分かってないが、queryファイルを作成する

query.graphqls
type Query {
    version: String!
} 

version というリクエストに対してString型を返す、という意味らしい
String!の!は「Nullではない」という事のようだ

graphqlのKotlinコードを作成

graphqlのリクエストに対して、Kotlinの処理を記述する。

Query.kt
@Component
class Query : GraphQLQueryResolver {
    // versionというリクエストに対して
    fun version(): String {
        return "1.0" // 常に1.0を返す
    }
}

この時点でMainApplicationを再度ビルドして実行する。
エラーが出ていなければ、以下のspring-boot-devtoolsにアクセスできる。

使い方は以下の通り

  1. 左側でQueryを記述
  2. ▶ボタンでリクエスト
  3. 右側にレスポンスが表示される
  4. スクリーンショット 2019-05-04 14.47.09.png

これでシンプルなSpringBoot + Kotlin + GraphQLが作成できた。

完成物

本当はもっと色々試したが、今回はここまで。
GitHubにコードが全て載っているので見たければどうぞ

13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12