Skip to content

Commit 1a27eee

Browse files
authored
feat: Provide default GraphQLObjectCoercing fallback for non-existing Java classes (#130)
* feat: provide fallback Object default for JavaScalars * feat: create alias for Object Scalar type by default
1 parent 5f9da5f commit 1a27eee

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/JavaScalars.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ public class JavaScalars {
115115
}
116116

117117
public static GraphQLScalarType of(Class<?> key) {
118-
return scalarsRegistry.get(key);
118+
return scalarsRegistry.computeIfAbsent(key, JavaScalars::computeGraphQLScalarType);
119+
}
120+
121+
protected static GraphQLScalarType computeGraphQLScalarType(Class<?> key) {
122+
String typeName = key.getSimpleName();
123+
String description = typeName+" Scalar Object Type";
124+
125+
return new GraphQLScalarType(typeName, description, new GraphQLObjectCoercing());
119126
}
120127

121128
public static JavaScalars register(Class<?> key, GraphQLScalarType value) {

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/schema/JavaScalarsTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
import java.time.LocalTime;
2525
import java.time.Month;
2626
import java.time.ZoneId;
27+
import java.util.Map;
2728
import java.util.concurrent.TimeUnit;
2829

30+
import com.introproventures.graphql.jpa.query.converter.model.VariableValue;
31+
import com.introproventures.graphql.jpa.query.schema.JavaScalars.GraphQLObjectCoercing;
2932
import graphql.language.StringValue;
3033
import graphql.schema.Coercing;
3134
import graphql.schema.CoercingParseValueException;
3235
import graphql.schema.CoercingSerializeException;
36+
import graphql.schema.GraphQLScalarType;
3337
import org.junit.Test;
3438

3539
public class JavaScalarsTest {
@@ -173,12 +177,38 @@ public void testLocalTimeParseValueInvlidValue() {
173177
//given
174178
Coercing<?,?> coercing = JavaScalars.of(LocalTime.class).getCoercing();
175179

176-
//then
177180
//then
178181
coercing.parseValue("");
179182
coercing.parseValue("not a time");
180183
coercing.parseValue(new Object());
181184

182185
fail("Should throw CoercingParseValueException");
183186
}
187+
188+
@Test
189+
public void testNonExistingJavaScalarShouldDefaultToObjectCoercing() {
190+
//given
191+
GraphQLScalarType scalarType = JavaScalars.of(VariableValue.class);
192+
193+
//then
194+
Coercing<?,?> coercing = scalarType.getCoercing();
195+
196+
assertThat(coercing).isInstanceOf(GraphQLObjectCoercing.class);
197+
assertThat(scalarType.getName()).isEqualTo("VariableValue");
198+
}
199+
200+
@Test
201+
public void testRegisterJavaScalarWithObjectCoercing() {
202+
//given
203+
JavaScalars.register(Map.class, new GraphQLScalarType("Map", "Map Object Type", new GraphQLObjectCoercing()));
204+
205+
//when
206+
GraphQLScalarType scalarType = JavaScalars.of(Map.class);
207+
208+
//then
209+
Coercing<?,?> coercing = scalarType.getCoercing();
210+
211+
assertThat(coercing).isInstanceOf(GraphQLObjectCoercing.class);
212+
assertThat(scalarType.getName()).isEqualTo("Map");
213+
}
184214
}

0 commit comments

Comments
 (0)