Skip to content

Commit

Permalink
chore: clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed May 28, 2024
1 parent 0a04c19 commit 214a140
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 92 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
package com.example.demo.gql.scalars

import graphql.GraphQLContext
import graphql.execution.CoercedVariables
import graphql.language.StringValue
import graphql.language.Value
import graphql.schema.Coercing
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingSerializeException
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*

class LocalDateTimeScalar : Coercing<LocalDateTime, String> {
@Throws(CoercingSerializeException::class)
override fun serialize(dataFetcherResult: Any): String? {
override fun serialize(dataFetcherResult: Any, graphQLContext: GraphQLContext, locale: Locale): String? {
return when (dataFetcherResult) {
is LocalDateTime -> dataFetcherResult.format(DateTimeFormatter.ISO_DATE_TIME)
else -> throw CoercingSerializeException("Not a valid DateTime")
}
}

@Throws(CoercingParseValueException::class)
override fun parseValue(input: Any): LocalDateTime {
override fun parseValue(input: Any, graphQLContext: GraphQLContext, locale: Locale): LocalDateTime {
return LocalDateTime.parse(input.toString(), DateTimeFormatter.ISO_DATE_TIME)
}

@Throws(CoercingParseLiteralException::class)
override fun parseLiteral(input: Any): LocalDateTime {
override fun parseLiteral(
input: Value<*>,
variables: CoercedVariables,
graphQLContext: GraphQLContext,
locale: Locale
): LocalDateTime {
when (input) {
is StringValue -> return LocalDateTime.parse(input.value, DateTimeFormatter.ISO_DATE_TIME)
else -> throw CoercingParseLiteralException("Value is not a valid ISO date time")
}
}

override fun valueToLiteral(input: Any, graphQLContext: GraphQLContext, locale: Locale): Value<*> {
return StringValue(input.toString())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.example.demo
import com.example.demo.gql.types.Comment
import com.example.demo.gql.types.Post
import com.fasterxml.jackson.databind.ObjectMapper
import io.kotest.assertions.timing.continually
import io.kotest.assertions.nondeterministic.continually
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand All @@ -25,7 +25,6 @@ import java.util.*
import java.util.concurrent.CopyOnWriteArrayList
import kotlin.time.Duration.Companion.seconds

@OptIn(ExperimentalCoroutinesApi::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
internal class IntegrationTests {
companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@ package com.example.demo
import com.example.demo.gql.datafetchers.PostController
import com.example.demo.gql.types.Post
import com.ninjasquad.springmockk.MockkBean
import io.kotest.matchers.collections.shouldContainAll
import io.kotest.matchers.ints.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
import io.mockk.coEvery
import io.mockk.coVerify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest
import org.springframework.context.annotation.Import
import org.springframework.graphql.ResponseError
import org.springframework.graphql.test.tester.GraphQlTester
import java.time.LocalDateTime
import java.util.*

@OptIn(ExperimentalCoroutinesApi::class)
@GraphQlTest(controllers = [PostController::class])
@Import(ValidationConfig::class)
internal class QueryTests {
companion object {
private val log = LoggerFactory.getLogger(QueryTests::class.java)
Expand Down Expand Up @@ -61,9 +56,9 @@ internal class QueryTests {
// graphQlTester.document(query).execute()
// .errors().satisfy { it.forEach { error -> log.debug("error message: ${error.message}") } }
graphQlTester.document(query)
.execute()
.path("data.allPosts[*].title")
.entityList(String::class.java).hasSize(2).contains("POST 1", "POST 2")
.execute()
.path("data.allPosts[*].title")
.entityList(String::class.java).hasSize(2).contains("POST 1", "POST 2")

coVerify(exactly = 1) { postService.allPosts() }
}
Expand Down

0 comments on commit 214a140

Please sign in to comment.