Skip to content

feat: Provide default GraphQLObjectCoercing fallback for non-existing Java classes #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ public class JavaScalars {
}

public static GraphQLScalarType of(Class<?> key) {
return scalarsRegistry.get(key);
return scalarsRegistry.computeIfAbsent(key, JavaScalars::computeGraphQLScalarType);
}

protected static GraphQLScalarType computeGraphQLScalarType(Class<?> key) {
String typeName = key.getSimpleName();
String description = typeName+" Scalar Object Type";

return new GraphQLScalarType(typeName, description, new GraphQLObjectCoercing());
}

public static JavaScalars register(Class<?> key, GraphQLScalarType value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.introproventures.graphql.jpa.query.converter.model.VariableValue;
import com.introproventures.graphql.jpa.query.schema.JavaScalars.GraphQLObjectCoercing;
import graphql.language.StringValue;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;
import org.junit.Test;

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

//then
//then
coercing.parseValue("");
coercing.parseValue("not a time");
coercing.parseValue(new Object());

fail("Should throw CoercingParseValueException");
}

@Test
public void testNonExistingJavaScalarShouldDefaultToObjectCoercing() {
//given
GraphQLScalarType scalarType = JavaScalars.of(VariableValue.class);

//then
Coercing<?,?> coercing = scalarType.getCoercing();

assertThat(coercing).isInstanceOf(GraphQLObjectCoercing.class);
assertThat(scalarType.getName()).isEqualTo("VariableValue");
}

@Test
public void testRegisterJavaScalarWithObjectCoercing() {
//given
JavaScalars.register(Map.class, new GraphQLScalarType("Map", "Map Object Type", new GraphQLObjectCoercing()));

//when
GraphQLScalarType scalarType = JavaScalars.of(Map.class);

//then
Coercing<?,?> coercing = scalarType.getCoercing();

assertThat(coercing).isInstanceOf(GraphQLObjectCoercing.class);
assertThat(scalarType.getName()).isEqualTo("Map");
}
}