From 43737daa63f86228a226f42fc54b678a709b5d1b Mon Sep 17 00:00:00 2001 From: hantsy Date: Mon, 25 Nov 2024 20:15:06 +0800 Subject: [PATCH] chore: fix errors --- .../src/main/kotlin/com/example/demo/DemoApplication.kt | 2 +- .../main/kotlin/com/example/demo/gql/ExceptionHandlers.kt | 2 +- .../com/example/demo/gql/datafetchers/AuthDataFetcher.kt | 2 +- .../com/example/demo/gql/datafetchers/AuthorsDataFetcher.kt | 5 +++-- .../com/example/demo/gql/datafetchers/PostsDataFetcher.kt | 6 +++--- .../com/example/demo/gql/dataloaders/AuthorsDataLoader.kt | 5 +++-- .../com/example/demo/gql/dataloaders/CommentsDataLoader.kt | 2 +- 7 files changed, 13 insertions(+), 11 deletions(-) diff --git a/dgs-kotlin/src/main/kotlin/com/example/demo/DemoApplication.kt b/dgs-kotlin/src/main/kotlin/com/example/demo/DemoApplication.kt index 9739d31ac..2624668bb 100644 --- a/dgs-kotlin/src/main/kotlin/com/example/demo/DemoApplication.kt +++ b/dgs-kotlin/src/main/kotlin/com/example/demo/DemoApplication.kt @@ -192,7 +192,7 @@ val beans = beans { if (!passwordEncoder.matches(password, user.password)) { throw BadCredentialsException("username or password was not matched.") } - if (!user.isEnabled()) { + if (!user.isEnabled) { throw DisabledException("user is not enabled.") } AuthenticationTokenWithId(user.id, username, user.authorities) diff --git a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/ExceptionHandlers.kt b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/ExceptionHandlers.kt index 4b5f48ffc..5ea112f95 100644 --- a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/ExceptionHandlers.kt +++ b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/ExceptionHandlers.kt @@ -19,7 +19,7 @@ class ExceptionHandlers : DataFetcherExceptionHandler { return when (val exception = handlerParameters.exception) { is PostNotFoundException, is AuthorNotFoundException -> { val graphqlError = TypedGraphQLError.newNotFoundBuilder() - .message(exception.message) + .message(exception.message?: "Not found") .path(handlerParameters.path) .build(); val result = DataFetcherExceptionHandlerResult.newResult() diff --git a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/AuthDataFetcher.kt b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/AuthDataFetcher.kt index 75a3d70b9..5f2f0bdcc 100644 --- a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/AuthDataFetcher.kt +++ b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/AuthDataFetcher.kt @@ -20,7 +20,7 @@ class AuthDataFetcher( @DgsMutation fun signIn(@InputArgument credentials: Credentials, dfe: DgsDataFetchingEnvironment): Map { - var auth = authenticationManager.authenticate( + val auth = authenticationManager.authenticate( UsernamePasswordAuthenticationToken( credentials.username, credentials.password diff --git a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/AuthorsDataFetcher.kt b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/AuthorsDataFetcher.kt index 2a08a14bb..f6de7f3aa 100644 --- a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/AuthorsDataFetcher.kt +++ b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/AuthorsDataFetcher.kt @@ -2,6 +2,7 @@ package com.example.demo.gql.datafetchers import com.example.demo.gql.DgsConstants import com.example.demo.gql.types.* +import com.example.demo.service.AuthorNotFoundException import com.example.demo.service.AuthorService import com.example.demo.service.PostService import com.netflix.graphql.dgs.* @@ -19,7 +20,7 @@ class AuthorsDataFetcher( @DgsData(parentType = DgsConstants.AUTHOR.TYPE_NAME, field = DgsConstants.AUTHOR.Posts) fun posts(dfe: DgsDataFetchingEnvironment): List { - val a: Author = dfe.getSource() + val a: Author = dfe.getSource()!! return postService.getPostsByAuthorId(a.id) } @@ -30,7 +31,7 @@ class AuthorsDataFetcher( @DgsData(parentType = DgsConstants.AUTHOR.TYPE_NAME, field = DgsConstants.AUTHOR.Profile) fun profile(dfe: DgsDataFetchingEnvironment): Profile? { - val a: Author = dfe.getSource() + val a: Author = dfe.getSource()!! return authorService.getProfileByUserId(a.id) } } diff --git a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/PostsDataFetcher.kt b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/PostsDataFetcher.kt index 8ee7237b9..ebb7afaee 100644 --- a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/PostsDataFetcher.kt +++ b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/datafetchers/PostsDataFetcher.kt @@ -21,8 +21,8 @@ class PostsDataFetcher(val postService: PostService) { @DgsData(parentType = DgsConstants.POST.TYPE_NAME, field = DgsConstants.POST.Author) fun author(dfe: DgsDataFetchingEnvironment): CompletableFuture { val dataLoader = dfe.getDataLoader("authorsLoader") - val post = dfe.getSource() - return dataLoader.load(post.authorId) + val post = dfe.getSource()!! + return dataLoader!!.load(post.authorId) } @DgsData(parentType = DgsConstants.POST.TYPE_NAME, field = DgsConstants.POST.Comments) @@ -30,7 +30,7 @@ class PostsDataFetcher(val postService: PostService) { val dataLoader = dfe.getDataLoader>( CommentsDataLoader::class.java ) - val (id) = dfe.getSource() + val (id) = dfe.getSource()!! return dataLoader.load(id) } diff --git a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/dataloaders/AuthorsDataLoader.kt b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/dataloaders/AuthorsDataLoader.kt index f6d3b5ec5..e261d191d 100644 --- a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/dataloaders/AuthorsDataLoader.kt +++ b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/dataloaders/AuthorsDataLoader.kt @@ -4,13 +4,14 @@ import com.example.demo.gql.types.Author import com.example.demo.service.AuthorService import com.netflix.graphql.dgs.DgsDataLoader import org.dataloader.BatchLoader +import java.util.concurrent.CompletableFuture.completedFuture import java.util.concurrent.CompletableFuture.supplyAsync import java.util.concurrent.CompletionStage @DgsDataLoader(name = "authorsLoader") class AuthorsDataLoader(val authorService: AuthorService) : BatchLoader { - override fun load(keys: List): CompletionStage> = supplyAsync { + override fun load(keys: List): CompletionStage> = completedFuture( authorService.getAuthorByIdIn(keys) - } + ) } diff --git a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/dataloaders/CommentsDataLoader.kt b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/dataloaders/CommentsDataLoader.kt index f2e0aea21..81bbb85da 100644 --- a/dgs-kotlin/src/main/kotlin/com/example/demo/gql/dataloaders/CommentsDataLoader.kt +++ b/dgs-kotlin/src/main/kotlin/com/example/demo/gql/dataloaders/CommentsDataLoader.kt @@ -19,7 +19,7 @@ class CommentsDataLoader(val postService: PostService) : MappedBatchLoader postId == it } } log.info("mapped comments: {}", mappedComments) - return CompletableFuture.supplyAsync { mappedComments } + return CompletableFuture.completedFuture(mappedComments) } companion object {