-
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
14 changed files
with
235 additions
and
185 deletions.
There are no files selected for viewing
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
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
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
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
34 changes: 19 additions & 15 deletions
34
dgs-kotlin/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,43 +1,47 @@ | ||
package com.example.demo.gql.scalars | ||
|
||
import com.netflix.graphql.dgs.DgsScalar | ||
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 | ||
|
||
|
||
//@DgsComponent | ||
//class DateTimeScalar { | ||
// @DgsRuntimeWiring | ||
// fun addScalar(builder: RuntimeWiring.Builder): RuntimeWiring.Builder { | ||
// return builder.scalar(ExtendedScalars.DateTime) | ||
// } | ||
//} | ||
import java.util.* | ||
|
||
@DgsScalar(name = "LocalDateTime") | ||
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 when (input) { | ||
is String -> StringValue.newStringValue(input).build() | ||
else -> throw CoercingParseValueException("Value is not a string") | ||
} | ||
} | ||
} |
85 changes: 52 additions & 33 deletions
85
...subscription-ws/src/test/kotlin/com/example/demo/DemoApplicationTestsWithGraphQLClient.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,76 +1,95 @@ | ||
package com.example.demo | ||
|
||
import com.jayway.jsonpath.TypeRef | ||
import com.netflix.graphql.dgs.DgsQueryExecutor | ||
import com.netflix.graphql.dgs.client.WebClientGraphQLClient | ||
import com.netflix.graphql.dgs.client.WebSocketGraphQLClient | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.intellij.lang.annotations.Language | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.web.server.LocalServerPort | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient | ||
import reactor.test.StepVerifier | ||
import java.time.Duration | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class DemoApplicationTestsWithGraphQLClient { | ||
|
||
@Autowired | ||
lateinit var dgsQueryExecutor: DgsQueryExecutor | ||
|
||
lateinit var webClientGraphQLClient: WebClientGraphQLClient | ||
lateinit var socketGraphQLClient: WebSocketGraphQLClient | ||
|
||
@LocalServerPort | ||
var port: Int = 0 | ||
|
||
@BeforeEach | ||
fun setup() { | ||
this.socketGraphQLClient = WebSocketGraphQLClient("ws://localhost:$port/subscriptions", ReactorNettyWebSocketClient()) | ||
this.webClientGraphQLClient = WebClientGraphQLClient(WebClient.create("http://localhost:$port/graphql")) | ||
this.socketGraphQLClient = | ||
WebSocketGraphQLClient("ws://localhost:$port/subscriptions", ReactorNettyWebSocketClient()) | ||
} | ||
|
||
@Test | ||
fun testMessages() { | ||
//Hooks.onOperatorDebug(); | ||
val query = "subscription { messageSent { body } }" | ||
@Language("graphql") val messageSentSubscriptionQuery = """ | ||
subscription { | ||
messageSent { | ||
body | ||
} | ||
} | ||
""".trimIndent() | ||
val variables = emptyMap<String, Any>() | ||
val executionResult = socketGraphQLClient.reactiveExecuteQuery(query, variables) | ||
val executionResult = socketGraphQLClient.reactiveExecuteQuery(messageSentSubscriptionQuery, variables) | ||
.map { | ||
it.extractValueAsObject( | ||
"data.messageSent", | ||
object : TypeRef<Map<String, Any>>() {} | ||
)["body"] as String | ||
} | ||
|
||
val message1 = "text1" | ||
val message2 = "text2" | ||
val verifier = StepVerifier.create(executionResult) | ||
.consumeNextWith { assertThat(it).isEqualTo("text1 message") } | ||
// .consumeNextWith { assertThat(it).isEqualTo("text2 message") } | ||
.thenAwait(Duration.ofMillis(1000)) // see: https://github.com/Netflix/dgs-framework/issues/657 | ||
.consumeNextWith { assertThat(it).isEqualTo(message1) } | ||
.consumeNextWith { assertThat(it).isEqualTo(message2) } | ||
.thenCancel() | ||
.verifyLater() | ||
|
||
val sendText1 = dgsQueryExecutor.executeAndExtractJsonPath<String>( | ||
"mutation sendMessage(\$msg: TextMessageInput!) { send(message:\$msg) { body}}", | ||
"data.send.body", | ||
mapOf("msg" to (mapOf("body" to "text1 message"))) | ||
) | ||
assertThat(sendText1).contains("text1"); | ||
@Language("graphql") val sendMessageQuery = """ | ||
mutation sendMessage(${'$'}msg: TextMessageInput!) { | ||
send(message:${'$'}msg) { | ||
body | ||
} | ||
} | ||
""".trimIndent() | ||
webClientGraphQLClient.reactiveExecuteQuery(sendMessageQuery, mapOf("msg" to (mapOf("body" to message1)))) | ||
.map { it.extractValueAsObject("data.send.body", String::class.java) } | ||
.`as` { StepVerifier.create(it) } | ||
.consumeNextWith { assertThat(it).isEqualTo(message1) } | ||
.verifyComplete() | ||
|
||
// val sendText2 = dgsQueryExecutor.executeAndExtractJsonPath<String>( | ||
// "mutation sendMessage(\$msg: TextMessageInput!) { send(message:\$msg) { body}}", | ||
// "data.send.body", | ||
// mapOf("msg" to (mapOf("body" to "text2 message"))) | ||
// ) | ||
// assertThat(sendText2).contains("text2"); | ||
webClientGraphQLClient.reactiveExecuteQuery(sendMessageQuery, mapOf("msg" to (mapOf("body" to message2)))) | ||
.map { it.extractValueAsObject("data.send.body", String::class.java) } | ||
.`as` { StepVerifier.create(it) } | ||
.consumeNextWith { assertThat(it).isEqualTo(message2) } | ||
.verifyComplete() | ||
|
||
//verify it now. | ||
verifier.verify() | ||
|
||
val msgs = dgsQueryExecutor.executeAndExtractJsonPath<List<String>>( | ||
" { messages { body }}", | ||
"data.messages[*].body" | ||
) | ||
assertThat(msgs).allMatch { s: String -> | ||
s.contains( | ||
"message" | ||
) | ||
} | ||
@Language("graphql") val allMessagesQuery = """ | ||
query allMessages { | ||
messages { | ||
body | ||
} | ||
} | ||
""".trimIndent() | ||
webClientGraphQLClient.reactiveExecuteQuery(allMessagesQuery) | ||
.map { it.extractValueAsObject("data.messages[*].body", object : TypeRef<List<String>>() {}) } | ||
.`as` { StepVerifier.create(it) } | ||
.consumeNextWith { assertThat(it).isEqualTo(message1) } | ||
.consumeNextWith { assertThat(it).isEqualTo(message2) } | ||
.verifyComplete() | ||
} | ||
} |
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
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
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
Oops, something went wrong.