|
1 | 1 | package com.example.demo.gql.scalars
|
2 | 2 |
|
3 | 3 | import com.netflix.graphql.dgs.DgsScalar
|
| 4 | +import graphql.GraphQLContext |
| 5 | +import graphql.execution.CoercedVariables |
4 | 6 | import graphql.language.StringValue
|
| 7 | +import graphql.language.Value |
5 | 8 | import graphql.schema.Coercing
|
6 | 9 | import graphql.schema.CoercingParseLiteralException
|
7 | 10 | import graphql.schema.CoercingParseValueException
|
8 | 11 | import graphql.schema.CoercingSerializeException
|
9 | 12 | import java.time.LocalDateTime
|
10 | 13 | 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.* |
20 | 15 |
|
21 | 16 | @DgsScalar(name = "LocalDateTime")
|
22 | 17 | 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? { |
25 | 19 | return when (dataFetcherResult) {
|
26 | 20 | is LocalDateTime -> dataFetcherResult.format(DateTimeFormatter.ISO_DATE_TIME)
|
27 | 21 | else -> throw CoercingSerializeException("Not a valid DateTime")
|
28 | 22 | }
|
29 | 23 | }
|
30 | 24 |
|
31 |
| - @Throws(CoercingParseValueException::class) |
32 |
| - override fun parseValue(input: Any): LocalDateTime { |
| 25 | + override fun parseValue(input: Any, graphQLContext: GraphQLContext, locale: Locale): LocalDateTime? { |
33 | 26 | return LocalDateTime.parse(input.toString(), DateTimeFormatter.ISO_DATE_TIME)
|
34 | 27 | }
|
35 | 28 |
|
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? { |
38 | 35 | when (input) {
|
39 | 36 | is StringValue -> return LocalDateTime.parse(input.value, DateTimeFormatter.ISO_DATE_TIME)
|
40 | 37 | else -> throw CoercingParseLiteralException("Value is not a valid ISO date time")
|
41 | 38 | }
|
42 | 39 | }
|
| 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 | + } |
43 | 47 | }
|
0 commit comments