-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a registry based mechanism for registering Custom GraphQL scalars (
#1131) * Added support for OffsetDateTimeScalar scalar and refactored GraphQLConversionUtils implementation to support runtime detection and registration of GraphQl Scalars * Added ElideCoercing Interface which has method to allow implementations to return Serde for a given Scalar Added usesSerdeOfType field in ElideScalarType annotation to give user flexibility to specify Type for which Serde is written * Adding test cases for OffsetDateTime scalar and GraphQLConversionUtils * Applied style check suggestion * Applied Codacy suggestion * Update OffsetDateTimeSerde.java Fixing checkstyles. * Elide Annotaion now supports subtypes Elide Annotaion now registers Serde instead of ElideCoercing Auto-scan happens on Elide initialization * Applied PR Review Suggestions 1. Fixed typos 2. Removed unused method from ClassScanner 3. Added suggested refactored Serde scanning code 4. CoerceUtil now returns Immutable Serde map * Fixing checkstyle (extra space) Co-authored-by: Aaron Klish <[email protected]>
- Loading branch information
1 parent
5c43ee8
commit cd2a159
Showing
11 changed files
with
340 additions
and
3 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
12 changes: 12 additions & 0 deletions
12
elide-core/src/main/java/com/yahoo/elide/core/exceptions/UnableToAddSerdeException.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,12 @@ | ||
/* | ||
* Copyright 2020, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.core.exceptions; | ||
|
||
public class UnableToAddSerdeException extends RuntimeException { | ||
public UnableToAddSerdeException(String message) { | ||
super(message); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/ElideTypeConverter.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,20 @@ | ||
/* | ||
* Copyright 2020, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.utils.coerce.converters; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface ElideTypeConverter { | ||
Class<?> type(); | ||
String name(); | ||
String description() default "Custom Elide type"; | ||
Class<?> [] subTypes() default {}; | ||
} |
23 changes: 23 additions & 0 deletions
23
elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/OffsetDateTimeSerde.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,23 @@ | ||
/* | ||
* Copyright 2020, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.utils.coerce.converters; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
@ElideTypeConverter(type = OffsetDateTime.class, name = "OffsetDateTime") | ||
public class OffsetDateTimeSerde implements Serde<String, OffsetDateTime> { | ||
|
||
@Override | ||
public OffsetDateTime deserialize(String val) { | ||
return OffsetDateTime.parse(val, DateTimeFormatter.ISO_OFFSET_DATE_TIME); | ||
} | ||
|
||
@Override | ||
public String serialize(OffsetDateTime val) { | ||
return val.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
elide-core/src/test/java/com/yahoo/elide/ElideCustomSerdeRegistrationTest.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,52 @@ | ||
/* | ||
* Copyright 2018, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import com.yahoo.elide.core.datastore.inmemory.HashMapDataStore; | ||
import com.yahoo.elide.core.datastore.inmemory.InMemoryDataStore; | ||
import com.yahoo.elide.utils.coerce.CoerceUtil; | ||
import com.yahoo.elide.utils.coerce.converters.ElideTypeConverter; | ||
import com.yahoo.elide.utils.coerce.converters.Serde; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class Dummy { | ||
} | ||
|
||
class DummyTwo extends Dummy { | ||
} | ||
|
||
class DummyThree extends Dummy { | ||
} | ||
|
||
@ElideTypeConverter(type = Dummy.class, name = "Dummy", subTypes = {DummyThree.class, DummyTwo.class}) | ||
class DummySerde implements Serde<String, Dummy> { | ||
|
||
@Override | ||
public Dummy deserialize(String val) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String serialize(Dummy val) { | ||
return null; | ||
} | ||
} | ||
|
||
public class ElideCustomSerdeRegistrationTest { | ||
@Test | ||
public void testRegisterCustomSerde() { | ||
HashMapDataStore wrapped = new HashMapDataStore(Dummy.class.getPackage()); | ||
InMemoryDataStore store = new InMemoryDataStore(wrapped); | ||
ElideSettings elideSettings = new ElideSettingsBuilder(store).build(); | ||
new Elide(elideSettings); | ||
assertNotNull(CoerceUtil.lookup(Dummy.class)); | ||
assertNotNull(CoerceUtil.lookup(DummyTwo.class)); | ||
assertNotNull(CoerceUtil.lookup(DummyThree.class)); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
elide-core/src/test/java/com/yahoo/elide/utils/coerce/converters/OffsetDateTimeTest.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,40 @@ | ||
/* | ||
* Copyright 2018, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.utils.coerce.converters; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
|
||
public class OffsetDateTimeTest { | ||
|
||
@Test | ||
public void testGraphQLOffsetDateTimeSerialize() { | ||
OffsetDateTime offsetDateTime = | ||
OffsetDateTime.of(1995, 11, 2, | ||
16, 45, 4, 56, | ||
ZoneOffset.ofHoursMinutes(5, 30)); | ||
String expected = "1995-11-02T16:45:04.000000056+05:30"; | ||
OffsetDateTimeSerde offsetDateTimeScalar = new OffsetDateTimeSerde(); | ||
Object actualDate = offsetDateTimeScalar.serialize(offsetDateTime); | ||
assertEquals(expected, actualDate); | ||
} | ||
|
||
@Test | ||
public void testGraphQLOffsetDateTimeDeserialize() { | ||
OffsetDateTime actualDate = | ||
OffsetDateTime.of(1995, 11, 2, | ||
16, 45, 4, 56, | ||
ZoneOffset.ofHoursMinutes(5, 30)); | ||
String actual = "1995-11-02T16:45:04.000000056+05:30"; | ||
OffsetDateTimeSerde offsetDateTimeScalar = new OffsetDateTimeSerde(); | ||
Object expected = offsetDateTimeScalar.deserialize(actual); | ||
assertEquals(expected, actualDate); | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
elide-graphql/src/main/java/com/yahoo/elide/graphql/SerdeCoercing.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,47 @@ | ||
/* | ||
* Copyright 2020, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.graphql; | ||
|
||
import com.yahoo.elide.utils.coerce.converters.Serde; | ||
|
||
import graphql.language.FloatValue; | ||
import graphql.language.IntValue; | ||
import graphql.language.StringValue; | ||
import graphql.schema.Coercing; | ||
import graphql.schema.CoercingParseValueException; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class SerdeCoercing<I, O> implements Coercing<I, O> { | ||
private String errorMessage; | ||
private Serde<O, I> serde; | ||
|
||
@Override | ||
public O serialize(Object dataFetcherResult) { | ||
return serde.serialize((I) dataFetcherResult); | ||
} | ||
|
||
@Override | ||
public I parseValue(Object input) { | ||
return serde.deserialize((O) input); | ||
} | ||
|
||
public I parseLiteral(Object o) { | ||
Object input; | ||
if (o instanceof IntValue) { | ||
input = ((IntValue) o).getValue().longValue(); | ||
} else if (o instanceof StringValue) { | ||
input = ((StringValue) o).getValue(); | ||
} else if (o instanceof FloatValue) { | ||
input = ((FloatValue) o).getValue().floatValue(); | ||
} else { | ||
throw new CoercingParseValueException(errorMessage); | ||
} | ||
return parseValue(input); | ||
} | ||
} |
Oops, something went wrong.