Skip to content

Instantly share code, notes, and snippets.

@roshane
Created July 14, 2022 23:36
Show Gist options
  • Save roshane/94b2d40a5f169638a55688fe919a6995 to your computer and use it in GitHub Desktop.
Save roshane/94b2d40a5f169638a55688fe919a6995 to your computer and use it in GitHub Desktop.
DGS dynamic GraphQL schema
server:
port: 9000
dgs:
graphql:
schema-locations:
- "file:///D:/IdeaProjects/Temp/schema.graphqls"
spring:
application:
name: dynamic-gql-schema
package com.example.skks.dgql
import com.netflix.graphql.dgs.DgsComponent
import com.netflix.graphql.dgs.DgsQuery
import com.netflix.graphql.dgs.internal.DefaultDgsQueryExecutor
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.client.RestTemplate
@SpringBootApplication
@Controller
class DynamicGqlServiceBoot {
// @Value("\${schema.path}")
// lateinit var schemaResource: Resource
@GetMapping("/", produces = [MediaType.TEXT_PLAIN_VALUE])
@ResponseBody
fun index(): String = "Hello There"
// @GetMapping("/schema", produces = [MediaType.TEXT_PLAIN_VALUE])
// @ResponseBody
// fun schema(): String = schemaResource.file.readLines().joinToString(separator = "\n")
}
fun main(args: Array<String>) {
runApplication<DynamicGqlServiceBoot>(*args)
}
@Configuration
class AppConfig {
@Bean
fun restTemplate(builder: RestTemplateBuilder) = builder.build()
@Bean
fun reloadSchemaIndicator(): DefaultDgsQueryExecutor.ReloadSchemaIndicator =
DefaultDgsQueryExecutor.ReloadSchemaIndicator {
val result = true
println("ReloadSchemaIndicator($result)")
result
}
}
@DgsComponent
class UserDataFetcher(private val restTemplate: RestTemplate) {
@DgsQuery
fun users(): List<HttpModels.User> {
val users = restTemplate
.getForEntity("https://jsonplaceholder.typicode.com/users", Array<HttpModels.User>::class.java)
return users.body?.toList().orEmpty()
}
}
object HttpModels {
data class Todo(
val id: Int,
val userId: Int,
val title: String,
val completed: Boolean
)
data class User(
val id: Int,
val name: String,
val username: String,
val email: String,
val address: Address,
val phone: String,
val website: String,
val company: Company
)
data class Post(
val id: Int,
val userId: Int,
val title: String,
val body: String
)
data class Comment(
val id: Int,
val postId: Int,
val name: String,
val email: String,
val body: String
)
data class Address(
val street: String,
val suite: String,
val city: String,
val zipcode: String,
val geo: Geo
)
data class Geo(
val lat: String,
val lng: String
)
data class Company(
val name: String,
val catchPhrase: String,
val bs: String
)
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>kitchen-sink</artifactId>
<groupId>com.example.skks</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dynamic-gql</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.graphql.dgs</groupId>
<artifactId>graphql-dgs-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-graphql</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment