-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add offsetTime Scalar to Typefunction (#84)
Add offsetTime Scalar to Typefunction
- Loading branch information
1 parent
e7d83ae
commit e91dff6
Showing
5 changed files
with
158 additions
and
12 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
78 changes: 78 additions & 0 deletions
78
...c/main/java/org/hypertrace/core/graphql/common/schema/typefunctions/OffsetTimeScalar.java
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,78 @@ | ||
package org.hypertrace.core.graphql.common.schema.typefunctions; | ||
|
||
import graphql.GraphqlErrorException; | ||
import graphql.annotations.processor.ProcessingElementsContainer; | ||
import graphql.annotations.processor.typeFunctions.TypeFunction; | ||
import graphql.language.StringValue; | ||
import graphql.schema.Coercing; | ||
import graphql.schema.CoercingParseLiteralException; | ||
import graphql.schema.CoercingParseValueException; | ||
import graphql.schema.CoercingSerializeException; | ||
import graphql.schema.GraphQLScalarType; | ||
import java.lang.reflect.AnnotatedType; | ||
import java.time.DateTimeException; | ||
import java.time.OffsetTime; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.function.Function; | ||
|
||
public class OffsetTimeScalar implements TypeFunction { | ||
|
||
private static final GraphQLScalarType OFFSET_TIME_SCALAR = | ||
GraphQLScalarType.newScalar() | ||
.name("OffsetTime") | ||
.description("An ISO-8601 formatted OffsetTime Scalar") | ||
.coercing( | ||
new Coercing<OffsetTime, String>() { | ||
@Override | ||
public String serialize(Object fetcherResult) throws CoercingSerializeException { | ||
return toOffsetTime(fetcherResult, CoercingSerializeException::new).toString(); | ||
} | ||
|
||
@Override | ||
public OffsetTime parseValue(Object input) throws CoercingParseValueException { | ||
return toOffsetTime(input, CoercingParseValueException::new); | ||
} | ||
|
||
@Override | ||
public OffsetTime parseLiteral(Object input) throws CoercingParseLiteralException { | ||
return toOffsetTime(input, CoercingParseLiteralException::new); | ||
} | ||
|
||
private <E extends GraphqlErrorException> OffsetTime toOffsetTime( | ||
Object offsetInput, Function<Exception, E> errorWrapper) throws E { | ||
try { | ||
if (offsetInput instanceof TemporalAccessor) { | ||
return OffsetTime.from((TemporalAccessor) offsetInput); | ||
} | ||
if (offsetInput instanceof CharSequence) { | ||
return OffsetTime.parse((CharSequence) offsetInput); | ||
} | ||
if (offsetInput instanceof StringValue) { | ||
return OffsetTime.parse(((StringValue) offsetInput).getValue()); | ||
} | ||
} catch (DateTimeException exception) { | ||
throw errorWrapper.apply(exception); | ||
} | ||
throw errorWrapper.apply( | ||
new DateTimeException( | ||
String.format( | ||
"Cannot convert provided format '%s' to OffsetTime", | ||
offsetInput.getClass().getCanonicalName()))); | ||
} | ||
}) | ||
.build(); | ||
|
||
@Override | ||
public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) { | ||
return OffsetTime.class.isAssignableFrom(aClass); | ||
} | ||
|
||
@Override | ||
public GraphQLScalarType buildType( | ||
boolean input, | ||
Class<?> aClass, | ||
AnnotatedType annotatedType, | ||
ProcessingElementsContainer container) { | ||
return OFFSET_TIME_SCALAR; | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
...src/test/java/org/hypertrace/core/graphql/common/schema/scalars/OffsetTimeScalarTest.java
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,72 @@ | ||
package org.hypertrace.core.graphql.common.schema.scalars; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import graphql.annotations.processor.ProcessingElementsContainer; | ||
import graphql.language.StringValue; | ||
import graphql.schema.GraphQLScalarType; | ||
import java.lang.reflect.AnnotatedType; | ||
import java.time.OffsetTime; | ||
import java.time.ZoneOffset; | ||
import org.hypertrace.core.graphql.common.schema.typefunctions.OffsetTimeScalar; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class OffsetTimeScalarTest { | ||
|
||
private static final String TEST_SCALAR_TIME_STRING = "21:30:12.748+12:30"; | ||
private static final OffsetTime TEST_OFFSET_TIME = OffsetTime.parse(TEST_SCALAR_TIME_STRING); | ||
private OffsetTimeScalar offsetTimeFunction; | ||
private GraphQLScalarType offsetTimeType; | ||
@Mock AnnotatedType mockAnnotatedType; | ||
// Can't actually mock class, but it's not used so to convey intent using the Mock class. | ||
private final Class<?> mockAnnotatedClass = Mock.class; | ||
@Mock ProcessingElementsContainer mockProcessingElementsContainer; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
this.offsetTimeFunction = new OffsetTimeScalar(); | ||
// Can't actually mock class, but it's not used so to convey intent using | ||
this.offsetTimeType = | ||
this.offsetTimeFunction.buildType( | ||
false, mockAnnotatedClass, mockAnnotatedType, mockProcessingElementsContainer); | ||
} | ||
|
||
@Test | ||
void canDetermineIfConvertible() { | ||
assertTrue(this.offsetTimeFunction.canBuildType(OffsetTime.class, this.mockAnnotatedType)); | ||
} | ||
|
||
@Test | ||
void canConvertFromLiteral() { | ||
assertEquals( | ||
TEST_OFFSET_TIME, offsetTimeType.getCoercing().parseLiteral(TEST_SCALAR_TIME_STRING)); | ||
} | ||
|
||
@Test | ||
void canSerialize() { | ||
assertEquals(TEST_SCALAR_TIME_STRING, offsetTimeType.getCoercing().serialize(TEST_OFFSET_TIME)); | ||
assertEquals( | ||
TEST_SCALAR_TIME_STRING, offsetTimeType.getCoercing().serialize(TEST_SCALAR_TIME_STRING)); | ||
|
||
assertEquals( | ||
TEST_SCALAR_TIME_STRING, | ||
offsetTimeType | ||
.getCoercing() | ||
.serialize(TEST_OFFSET_TIME.withOffsetSameLocal(ZoneOffset.ofHoursMinutes(12, 30)))); | ||
} | ||
|
||
@Test | ||
void canConvertFromValue() { | ||
assertEquals( | ||
TEST_OFFSET_TIME, | ||
offsetTimeType | ||
.getCoercing() | ||
.parseValue(StringValue.newStringValue().value(TEST_SCALAR_TIME_STRING).build())); | ||
} | ||
} |