Skip to content

Commit 56be363

Browse files
authored
feat: add scalar builder method to graphql query schema builder (#318)
* fix: add scalar registration method to GraphQLJpaSchemaBuilder * fix: add schema builder scalar test
1 parent 7315720 commit 56be363

File tree

3 files changed

+76
-36
lines changed

3 files changed

+76
-36
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

+35-25
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@
1616

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

19+
import java.beans.Introspector;
20+
import java.lang.reflect.AnnotatedElement;
21+
import java.lang.reflect.Field;
22+
import java.lang.reflect.Member;
23+
import java.util.ArrayList;
24+
import java.util.Collection;
25+
import java.util.Collections;
26+
import java.util.HashMap;
27+
import java.util.LinkedHashMap;
28+
import java.util.List;
29+
import java.util.Map;
30+
import java.util.Optional;
31+
import java.util.concurrent.atomic.AtomicInteger;
32+
import java.util.function.Supplier;
33+
import java.util.stream.Collectors;
34+
import java.util.stream.Stream;
35+
import javax.persistence.Convert;
36+
import javax.persistence.EntityManager;
37+
import javax.persistence.metamodel.Attribute;
38+
import javax.persistence.metamodel.EmbeddableType;
39+
import javax.persistence.metamodel.EntityType;
40+
import javax.persistence.metamodel.ManagedType;
41+
import javax.persistence.metamodel.PluralAttribute;
42+
import javax.persistence.metamodel.SingularAttribute;
43+
import javax.persistence.metamodel.Type;
1944
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnore;
2045
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreFilter;
2146
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreOrder;
@@ -41,6 +66,7 @@
4166
import graphql.schema.GraphQLList;
4267
import graphql.schema.GraphQLObjectType;
4368
import graphql.schema.GraphQLOutputType;
69+
import graphql.schema.GraphQLScalarType;
4470
import graphql.schema.GraphQLSchema;
4571
import graphql.schema.GraphQLType;
4672
import graphql.schema.GraphQLTypeReference;
@@ -49,31 +75,6 @@
4975
import org.slf4j.Logger;
5076
import org.slf4j.LoggerFactory;
5177

52-
import javax.persistence.Convert;
53-
import javax.persistence.EntityManager;
54-
import javax.persistence.metamodel.Attribute;
55-
import javax.persistence.metamodel.EmbeddableType;
56-
import javax.persistence.metamodel.EntityType;
57-
import javax.persistence.metamodel.ManagedType;
58-
import javax.persistence.metamodel.PluralAttribute;
59-
import javax.persistence.metamodel.SingularAttribute;
60-
import javax.persistence.metamodel.Type;
61-
import java.beans.Introspector;
62-
import java.lang.reflect.AnnotatedElement;
63-
import java.lang.reflect.Field;
64-
import java.lang.reflect.Member;
65-
import java.util.ArrayList;
66-
import java.util.Collection;
67-
import java.util.Collections;
68-
import java.util.HashMap;
69-
import java.util.List;
70-
import java.util.Map;
71-
import java.util.Optional;
72-
import java.util.concurrent.atomic.AtomicInteger;
73-
import java.util.function.Supplier;
74-
import java.util.stream.Collectors;
75-
import java.util.stream.Stream;
76-
7778
import static graphql.Scalars.GraphQLBoolean;
7879
import static graphql.schema.GraphQLArgument.newArgument;
7980
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;
@@ -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

@@ -165,6 +169,12 @@ public GraphQLSchema build() {
165169
return schema.build();
166170
}
167171

172+
public GraphQLJpaSchemaBuilder scalar(Class<?> javaType, GraphQLScalarType scalarType) {
173+
scalars.put(javaType, scalarType);
174+
175+
return this;
176+
}
177+
168178
private GraphQLObjectType getQueryType() {
169179
GraphQLObjectType.Builder queryType =
170180
GraphQLObjectType.newObject()

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)