Skip to content

Commit fe3fc99

Browse files
committed
chore: fix errors
1 parent 6a200a6 commit fe3fc99

File tree

29 files changed

+290
-240
lines changed

29 files changed

+290
-240
lines changed

dgs-codegen/src/main/java/com/example/demo/gql/directives/UppercaseDirectiveWiring.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import com.netflix.graphql.dgs.DgsDirective;
44
import graphql.schema.DataFetcherFactories;
5+
import graphql.schema.FieldCoordinates;
56
import graphql.schema.GraphQLFieldDefinition;
67
import graphql.schema.idl.SchemaDirectiveWiring;
78
import graphql.schema.idl.SchemaDirectiveWiringEnvironment;
89
import lombok.extern.slf4j.Slf4j;
9-
import org.springframework.stereotype.Component;
1010

1111
@DgsDirective(name = "uppercase")
1212
@Slf4j
@@ -16,8 +16,8 @@ public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFi
1616

1717
var field = env.getElement();
1818
var parentType = env.getFieldsContainer();
19-
20-
var originalDataFetcher = env.getCodeRegistry().getDataFetcher(parentType, field);
19+
var fieldCoordinates = FieldCoordinates.coordinates(parentType, field.getName());
20+
var originalDataFetcher = env.getCodeRegistry().getDataFetcher(fieldCoordinates, field);
2121
var dataFetcher = DataFetcherFactories.wrapDataFetcher(
2222
originalDataFetcher,
2323
(dataFetchingEnvironment, value) -> {
@@ -29,7 +29,7 @@ public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFi
2929
}
3030
);
3131

32-
env.getCodeRegistry().dataFetcher(parentType, field, dataFetcher);
32+
env.getCodeRegistry().dataFetcher(fieldCoordinates, dataFetcher);
3333
return field;
3434
}
3535
}

dgs-kotlin-co/src/main/kotlin/com/example/demo/DemoApplication.kt

+16-13
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import com.example.demo.model.PostEntity
44
import com.example.demo.repository.CommentRepository
55
import com.example.demo.repository.PostRepository
66
import kotlinx.coroutines.flow.toList
7-
import kotlinx.coroutines.runBlocking
87
import org.slf4j.LoggerFactory
9-
import org.springframework.boot.ApplicationArguments
10-
import org.springframework.boot.ApplicationRunner
118
import org.springframework.boot.autoconfigure.SpringBootApplication
9+
import org.springframework.boot.context.event.ApplicationReadyEvent
1210
import org.springframework.boot.runApplication
1311
import org.springframework.context.annotation.Bean
1412
import org.springframework.context.annotation.Configuration
13+
import org.springframework.context.event.EventListener
1514
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity
1615
import org.springframework.security.config.web.server.ServerHttpSecurity
1716
import org.springframework.security.web.server.SecurityWebFilterChain
@@ -26,20 +25,24 @@ fun main(args: Array<String>) {
2625
}
2726

2827
@Component
29-
class DataInitializer(val posts: PostRepository, val comments: CommentRepository) : ApplicationRunner {
30-
private val log = LoggerFactory.getLogger(DataInitializer::class.java)
31-
override fun run(args: ApplicationArguments?) {
28+
class DataInitializer(val posts: PostRepository, val comments: CommentRepository) {
29+
30+
companion object {
31+
private val log = LoggerFactory.getLogger(DataInitializer::class.java)
32+
}
33+
34+
@EventListener(ApplicationReadyEvent::class)
35+
suspend fun init() {
3236
val data = listOf(
3337
PostEntity(title = "Learn Spring", content = "content of Learn Spring"),
3438
PostEntity(title = "Learn Dgs framework", content = "content of Learn Dgs framework")
3539
)
36-
runBlocking {
37-
comments.deleteAll()
38-
posts.deleteAll()
3940

40-
val saved = posts.saveAll(data).toList()
41-
saved.forEach { log.debug("saved: {}", it) }
42-
}
41+
comments.deleteAll()
42+
posts.deleteAll()
43+
44+
val saved = posts.saveAll(data).toList()
45+
saved.forEach { log.debug("saved: {}", it) }
4346
}
4447
}
4548

@@ -51,7 +54,7 @@ class SecurityConfig {
5154
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
5255
return http
5356
.csrf { it.disable() }
54-
.httpBasic{}
57+
.httpBasic {}
5558
.securityMatcher(PathPatternParserServerWebExchangeMatcher("/graphql"))
5659
.build()
5760
}

dgs-kotlin-co/src/main/kotlin/com/example/demo/gql/ExceptionHandlers.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ExceptionHandlers : DataFetcherExceptionHandler {
1818
when (val exception = handlerParameters.exception) {
1919
is PostNotFoundException, is AuthorNotFoundException -> {
2020
val graphqlError = TypedGraphQLError.newNotFoundBuilder()
21-
.message(exception.message)
21+
.message(exception.message?: "Not Found")
2222
.path(handlerParameters.path)
2323
.build();
2424
CompletableFuture.completedFuture(

dgs-kotlin-co/src/main/kotlin/com/example/demo/gql/datafetcher/AuthorsDataFetcher.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class AuthorsDataFetcher(
1919

2020
@DgsData(parentType = DgsConstants.AUTHOR.TYPE_NAME, field = DgsConstants.AUTHOR.Posts)
2121
suspend fun posts(dfe: DgsDataFetchingEnvironment): List<Post> {
22-
val a: Author = dfe.getSource()
22+
val a: Author = dfe.getSource()!!
2323
return postService.getPostsByAuthorId(a.id).toList()
2424
}
2525
}

dgs-kotlin-co/src/main/kotlin/com/example/demo/gql/datafetcher/PostsDataFetcher.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ class PostsDataFetcher(val postService: PostService) {
2323
fun author(dfe: DgsDataFetchingEnvironment): CompletableFuture<Author> {
2424
val dataLoader = dfe.getDataLoader<String, Author>("authorsLoader")
2525
val post = dfe.getSource<Post>()
26-
return dataLoader.load(post.authorId)
26+
return dataLoader!!.load(post!!.authorId)
2727
}
2828

2929
@DgsData(parentType = DgsConstants.POST.TYPE_NAME, field = DgsConstants.POST.Comments)
3030
fun comments(dfe: DgsDataFetchingEnvironment): CompletableFuture<List<Comment>> {
3131
val dataLoader = dfe.getDataLoader<String, List<Comment>>(CommentsDataLoader::class.java)
32-
val (id) = dfe.getSource<Post>()
32+
val (id) = dfe.getSource<Post>()!!
3333
return dataLoader.load(id)
3434
}
3535

dgs-kotlin/src/main/kotlin/com/example/demo/DemoApplication.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ val beans = beans {
192192
if (!passwordEncoder.matches(password, user.password)) {
193193
throw BadCredentialsException("username or password was not matched.")
194194
}
195-
if (!user.isEnabled()) {
195+
if (!user.isEnabled) {
196196
throw DisabledException("user is not enabled.")
197197
}
198198
AuthenticationTokenWithId(user.id, username, user.authorities)

dgs-kotlin/src/main/kotlin/com/example/demo/gql/ExceptionHandlers.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ExceptionHandlers : DataFetcherExceptionHandler {
1919
return when (val exception = handlerParameters.exception) {
2020
is PostNotFoundException, is AuthorNotFoundException -> {
2121
val graphqlError = TypedGraphQLError.newNotFoundBuilder()
22-
.message(exception.message)
22+
.message(exception.message?: "Not found")
2323
.path(handlerParameters.path)
2424
.build();
2525
val result = DataFetcherExceptionHandlerResult.newResult()

dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/AuthDataFetcher.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AuthDataFetcher(
2020

2121
@DgsMutation
2222
fun signIn(@InputArgument credentials: Credentials, dfe: DgsDataFetchingEnvironment): Map<String, Any> {
23-
var auth = authenticationManager.authenticate(
23+
val auth = authenticationManager.authenticate(
2424
UsernamePasswordAuthenticationToken(
2525
credentials.username,
2626
credentials.password

dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/AuthorsDataFetcher.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.example.demo.gql.datafetchers
22

33
import com.example.demo.gql.DgsConstants
44
import com.example.demo.gql.types.*
5+
import com.example.demo.service.AuthorNotFoundException
56
import com.example.demo.service.AuthorService
67
import com.example.demo.service.PostService
78
import com.netflix.graphql.dgs.*
@@ -19,7 +20,7 @@ class AuthorsDataFetcher(
1920

2021
@DgsData(parentType = DgsConstants.AUTHOR.TYPE_NAME, field = DgsConstants.AUTHOR.Posts)
2122
fun posts(dfe: DgsDataFetchingEnvironment): List<Post> {
22-
val a: Author = dfe.getSource()
23+
val a: Author = dfe.getSource()!!
2324
return postService.getPostsByAuthorId(a.id)
2425
}
2526

@@ -30,7 +31,7 @@ class AuthorsDataFetcher(
3031

3132
@DgsData(parentType = DgsConstants.AUTHOR.TYPE_NAME, field = DgsConstants.AUTHOR.Profile)
3233
fun profile(dfe: DgsDataFetchingEnvironment): Profile? {
33-
val a: Author = dfe.getSource()
34+
val a: Author = dfe.getSource()!!
3435
return authorService.getProfileByUserId(a.id)
3536
}
3637
}

dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/PostsDataFetcher.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ class PostsDataFetcher(val postService: PostService) {
2121
@DgsData(parentType = DgsConstants.POST.TYPE_NAME, field = DgsConstants.POST.Author)
2222
fun author(dfe: DgsDataFetchingEnvironment): CompletableFuture<Author> {
2323
val dataLoader = dfe.getDataLoader<String, Author>("authorsLoader")
24-
val post = dfe.getSource<Post>()
25-
return dataLoader.load(post.authorId)
24+
val post = dfe.getSource<Post>()!!
25+
return dataLoader!!.load(post.authorId)
2626
}
2727

2828
@DgsData(parentType = DgsConstants.POST.TYPE_NAME, field = DgsConstants.POST.Comments)
2929
fun comments(dfe: DgsDataFetchingEnvironment): CompletableFuture<List<Comment>> {
3030
val dataLoader = dfe.getDataLoader<String, List<Comment>>(
3131
CommentsDataLoader::class.java
3232
)
33-
val (id) = dfe.getSource<Post>()
33+
val (id) = dfe.getSource<Post>()!!
3434
return dataLoader.load(id)
3535
}
3636

dgs-kotlin/src/main/kotlin/com/example/demo/gql/dataloaders/AuthorsDataLoader.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import com.example.demo.gql.types.Author
44
import com.example.demo.service.AuthorService
55
import com.netflix.graphql.dgs.DgsDataLoader
66
import org.dataloader.BatchLoader
7+
import java.util.concurrent.CompletableFuture.completedFuture
78
import java.util.concurrent.CompletableFuture.supplyAsync
89
import java.util.concurrent.CompletionStage
910

1011
@DgsDataLoader(name = "authorsLoader")
1112
class AuthorsDataLoader(val authorService: AuthorService) : BatchLoader<String, Author> {
12-
override fun load(keys: List<String>): CompletionStage<List<Author>> = supplyAsync {
13+
override fun load(keys: List<String>): CompletionStage<List<Author>> = completedFuture(
1314
authorService.getAuthorByIdIn(keys)
14-
}
15+
)
1516
}
1617

dgs-kotlin/src/main/kotlin/com/example/demo/gql/dataloaders/CommentsDataLoader.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CommentsDataLoader(val postService: PostService) : MappedBatchLoader<Strin
1919
mappedComments[it] = comments.filter { (_, _, postId) -> postId == it }
2020
}
2121
log.info("mapped comments: {}", mappedComments)
22-
return CompletableFuture.supplyAsync { mappedComments }
22+
return CompletableFuture.completedFuture(mappedComments)
2323
}
2424

2525
companion object {
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
11
package com.example.demo.gql.scalars
22

33
import com.netflix.graphql.dgs.DgsScalar
4+
import graphql.GraphQLContext
5+
import graphql.execution.CoercedVariables
46
import graphql.language.StringValue
7+
import graphql.language.Value
58
import graphql.schema.Coercing
69
import graphql.schema.CoercingParseLiteralException
710
import graphql.schema.CoercingParseValueException
811
import graphql.schema.CoercingSerializeException
912
import java.time.LocalDateTime
1013
import java.time.format.DateTimeFormatter
11-
12-
13-
//@DgsComponent
14-
//class DateTimeScalar {
15-
// @DgsRuntimeWiring
16-
// fun addScalar(builder: RuntimeWiring.Builder): RuntimeWiring.Builder {
17-
// return builder.scalar(ExtendedScalars.DateTime)
18-
// }
19-
//}
14+
import java.util.*
2015

2116
@DgsScalar(name = "LocalDateTime")
2217
class LocalDateTimeScalar : Coercing<LocalDateTime, String> {
23-
@Throws(CoercingSerializeException::class)
24-
override fun serialize(dataFetcherResult: Any): String? {
18+
override fun serialize(dataFetcherResult: Any, graphQLContext: GraphQLContext, locale: Locale): String? {
2519
return when (dataFetcherResult) {
2620
is LocalDateTime -> dataFetcherResult.format(DateTimeFormatter.ISO_DATE_TIME)
2721
else -> throw CoercingSerializeException("Not a valid DateTime")
2822
}
2923
}
3024

31-
@Throws(CoercingParseValueException::class)
32-
override fun parseValue(input: Any): LocalDateTime {
25+
override fun parseValue(input: Any, graphQLContext: GraphQLContext, locale: Locale): LocalDateTime? {
3326
return LocalDateTime.parse(input.toString(), DateTimeFormatter.ISO_DATE_TIME)
3427
}
3528

36-
@Throws(CoercingParseLiteralException::class)
37-
override fun parseLiteral(input: Any): LocalDateTime {
29+
override fun parseLiteral(
30+
input: Value<*>,
31+
variables: CoercedVariables,
32+
graphQLContext: GraphQLContext,
33+
locale: Locale
34+
): LocalDateTime? {
3835
when (input) {
3936
is StringValue -> return LocalDateTime.parse(input.value, DateTimeFormatter.ISO_DATE_TIME)
4037
else -> throw CoercingParseLiteralException("Value is not a valid ISO date time")
4138
}
4239
}
40+
41+
override fun valueToLiteral(input: Any, graphQLContext: GraphQLContext, locale: Locale): Value<*> {
42+
return when (input) {
43+
is String -> StringValue.newStringValue(input).build()
44+
else -> throw CoercingParseValueException("Value is not a string")
45+
}
46+
}
4347
}

0 commit comments

Comments
 (0)