Skip to content

Commit 3919543

Browse files
committed
fix: add schema builder scalar test
1 parent 0ed4352 commit 3919543

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.introproventures.graphql.jpa.query.schema;
1717

18+
import graphql.schema.GraphQLScalarType;
1819
import graphql.schema.GraphQLSchema;
1920

2021
/**
@@ -57,7 +58,17 @@ public interface GraphQLSchemaBuilder {
5758
* @return this builder instance
5859
*/
5960
GraphQLSchemaBuilder namingStrategy(NamingStrategy instance);
60-
61+
62+
63+
/**
64+
* Register Java type scalar
65+
*
66+
* @param javaType class
67+
* @param scalarType GraphQL scalar instance
68+
* @return this builder instance
69+
*/
70+
GraphQLSchemaBuilder scalar(Class<?> javaType, GraphQLScalarType scalarType);
71+
6172
/**
6273
* Builds {code #GraphQLSchema} instance
6374
*

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collection;
2525
import java.util.Collections;
2626
import java.util.HashMap;
27+
import java.util.LinkedHashMap;
2728
import java.util.List;
2829
import java.util.Map;
2930
import java.util.Optional;
@@ -133,6 +134,7 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder {
133134
private boolean enableResultStream = false;
134135

135136
private RestrictedKeysProvider restrictedKeysProvider = (entityDescriptor) -> Optional.of(Collections.emptyList());
137+
private Map<Class<?>, GraphQLScalarType> scalars = new LinkedHashMap<>();
136138

137139
private final Relay relay = new Relay();
138140

@@ -151,6 +153,8 @@ public GraphQLJpaSchemaBuilder(EntityManager entityManager) {
151153
*/
152154
@Override
153155
public GraphQLSchema build() {
156+
scalars.forEach((javaType, scalarType) -> JavaScalars.register(javaType, scalarType));
157+
154158
GraphQLSchema.Builder schema = GraphQLSchema.newSchema()
155159
.query(getQueryType());
156160

@@ -166,7 +170,7 @@ public GraphQLSchema build() {
166170
}
167171

168172
public GraphQLJpaSchemaBuilder scalar(Class<?> javaType, GraphQLScalarType scalarType) {
169-
JavaScalars.register(javaType, scalarType);
173+
scalars.put(javaType, scalarType);
170174

171175
return this;
172176
}

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

+29-10
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@
1616

1717
package com.introproventures.graphql.jpa.query.schema;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
19+
import java.util.Optional;
2120
import javax.persistence.EntityManager;
22-
21+
import com.introproventures.graphql.jpa.query.AbstractSpringBootTestSupport;
22+
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
23+
import graphql.Scalars;
24+
import graphql.schema.GraphQLInputObjectType;
25+
import graphql.schema.GraphQLObjectType;
26+
import graphql.schema.GraphQLScalarType;
27+
import graphql.schema.GraphQLSchema;
2328
import org.junit.Before;
2429
import org.junit.Test;
2530
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,13 +33,7 @@
2833
import org.springframework.boot.test.context.SpringBootTest;
2934
import org.springframework.context.annotation.Bean;
3035

31-
import com.introproventures.graphql.jpa.query.AbstractSpringBootTestSupport;
32-
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
33-
34-
import graphql.Scalars;
35-
import graphql.schema.GraphQLInputObjectType;
36-
import graphql.schema.GraphQLObjectType;
37-
import graphql.schema.GraphQLSchema;
36+
import static org.assertj.core.api.Assertions.assertThat;
3837

3938
@SpringBootTest
4039
public class StarwarsSchemaBuildTest extends AbstractSpringBootTestSupport {
@@ -188,5 +187,25 @@ public void testBuildSchema(){
188187
//then
189188
assertThat(schema).isNotNull();
190189
}
190+
191+
@Test
192+
public void scalar() {
193+
// given
194+
GraphQLScalarType scalarType = GraphQLScalarType.newScalar()
195+
.name("TestObject")
196+
.coercing(new JavaScalars.GraphQLObjectCoercing())
197+
.build();
198+
199+
// when
200+
builder.scalar(Object.class, scalarType)
201+
.build();
202+
203+
// then
204+
Optional<GraphQLScalarType> result = JavaScalars.of(scalarType.getName());
205+
206+
assertThat(result).isNotEmpty()
207+
.get()
208+
.isEqualTo(scalarType);
209+
}
191210

192211
}

0 commit comments

Comments
 (0)