-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
93 deletions.
There are no files selected for viewing
76 changes: 0 additions & 76 deletions
76
spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/ValidationConfig.kt
This file was deleted.
Oops, something went wrong.
22 changes: 16 additions & 6 deletions
22
...hql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/gql/scalars/LocalDateTimeScalar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
spring-graphql-rsocket-kotlin-co/src/test/kotlin/com/example/demo/MutationTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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.mockk.coEvery | ||
import io.mockk.coVerify | ||
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.graphql.test.tester.GraphQlTester | ||
import java.time.LocalDateTime | ||
import java.util.* | ||
|
||
@GraphQlTest(controllers = [PostController::class]) | ||
internal class MutationTests { | ||
companion object { | ||
private val log = LoggerFactory.getLogger(MutationTests::class.java) | ||
} | ||
|
||
@Autowired | ||
lateinit var graphQlTester: GraphQlTester | ||
|
||
@MockkBean | ||
lateinit var postService: PostService | ||
|
||
@MockkBean | ||
lateinit var authorService: AuthorService | ||
|
||
@Test | ||
fun createPosts() = runTest { | ||
coEvery { postService.createPost(any()) } returns | ||
Post( | ||
id = UUID.randomUUID(), | ||
title = "test title", | ||
content = "test content", | ||
status = PostStatus.DRAFT, | ||
createdAt = LocalDateTime.now() | ||
) | ||
|
||
val inputHolder = "\$input" | ||
val query = """ | ||
mutation createPost($inputHolder: CreatePostInput!){ | ||
createPost(createPostInput:$inputHolder){ | ||
id, | ||
title, | ||
content | ||
} | ||
} | ||
""".trimIndent() | ||
graphQlTester.document(query) | ||
.variable( | ||
"input", | ||
mapOf( | ||
"title" to "test title", | ||
"content" to "test content" | ||
) | ||
) | ||
.execute() | ||
.path("data.createPost.title") | ||
.entity(String::class.java) | ||
.isEqualTo("TEST TITLE") | ||
|
||
coVerify(exactly = 1) { postService.createPost(any()) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters