Spring boot中apollo-kotlin基本使用(一个客户端使用多个graphql端点)

apollo-kotlin

一、新建Springboot项目

.
├── build.gradle.kts
└── src
    ├── main
    │   ├── graphql
    │   │   ├── service1
    │   │   │   ├── DemoQuery.graphql
    │   │   │   └── schema.json
    │   │   └── service2
    │   │       ├── Demo2Query.graphql
    │   │       └── schema.json
    │   ├── java
    │   ├── kotlin
    │   │   └── com
    │   │       └── demo
    │   │           ├── GraphqlApplication.kt
    │   │           ├── config
    │   │           │   ├── ApolloClientConfig.kt
    │   │           │   └── GraphqlProperties.kt
    │   │           └── service
    │   │               └── GraphqlApi.kt
    │   └── resources
    │       └── application.yml
    └── test
        ├── java
        ├── kotlin
        │   └── com
        │       └── demo
        │           └── XqxGraphqlApplicationTests.kt
        └── resources

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
  id("com.xqx.java-conventions")
	kotlin("jvm") version "1.6.21"
	kotlin("plugin.spring") version "1.6.21"
	id("com.apollographql.apollo3").version("3.5.0")
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter")
	api("org.jetbrains.kotlin:kotlin-reflect")
	api("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	api("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8")
	api("com.apollographql.apollo3:apollo-runtime:3.5.0")
	testImplementation("org.springframework.boot:spring-boot-starter-test")
}

apollo {

  // 如果一个客户端需要调用不同的graphql端点时可以配置多个服务(这个多服务配置不是必须的)
	generateKotlinModels.set(false)
 	// graphql端点一
	service("service1") {
		srcDir("src/main/graphql/service1")
		packageName.set("com.demo.graphql.service1")
	}

  // graphql端点一
	service("service2") {
		srcDir("src/main/graphql/service1")
		packageName.set("com.demo.graphql.service1")
	}
}

ApolloClientConfig.kt

/**
 *  graphql 客户端配置
 */
@Configuration
class ApolloClientConfig(@Autowired val graphqlProperties: GraphqlProperties) {


    @Bean
    fun service1ApolloClient(): ApolloClient {
        val apolloClient = ApolloClient.Builder()
            .serverUrl("https://your.domain1/graphql/endpoint")
            .build()

        return apolloClient
    }

    @Bean
    fun service2ApolloClient(): ApolloClient {
        val apolloClient = ApolloClient.Builder()
            .serverUrl("https://your.domain2/graphql/endpoint")
            .build()
        return apolloClient
    }
}

GraphqlApi.kt

/**
 *  调用接口
 */
class GraphqlApi(@Autowired val service1ApolloClient: ApolloClient, 
                 @Autowired val service2ApolloClient: ApolloClient) {


    fun fetchService1() {
    	// 使用service1ApolloClient调用
      service1ApolloClient...
    }

    @Bean
    fun fetchService2() {
        // 使用service2ApolloClient调用
      try {
        val response = service2ApolloClient.query(query).execute()
      } catch(exception: ApolloException) {
        // handle exception here
      }
    }
}

二、下载schema文件

方式一:使用gradle插件下载

# 端点一
./gradlew downloadApolloSchema \
  --endpoint="https://your.domain/graphql/endpoint" \
  --schema="src/main/graphql/service1/schema.graphqls"
# 端点二
  ./gradlew downloadApolloSchema \
  --endpoint="https://your.domain/graphql/endpoint" \
  --schema="src/main/graphql/service1/schema.graphqls"

方式二:使用apollo-js工具下载

npm install -g apollp 
npm install -g graphql
# 端点一
apollo schema:download --endpoint=http://server1/graphql schema.json
# 端点二
apollo schema:download --endpoint=http://server2/graphql schema.json

三、自动生成代码

gradle插件定义了generateApolloSources任务,通过运行generateApolloSources任务会自动生成代码到srcDir下。运行项目build的时候会自动运行generateApolloSources任务。

你可能感兴趣的:(GraphQL,spring,boot,kotlin,spring,boot,graphql)