Skip to content

feat: add scalar builder method to graphql query schema builder #318

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
Dec 19, 2022
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 @@ -15,6 +15,7 @@
*/
package com.introproventures.graphql.jpa.query.schema;

import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;

/**
Expand Down Expand Up @@ -57,7 +58,17 @@ public interface GraphQLSchemaBuilder {
* @return this builder instance
*/
GraphQLSchemaBuilder namingStrategy(NamingStrategy instance);



/**
* Register Java type scalar
*
* @param javaType class
* @param scalarType GraphQL scalar instance
* @return this builder instance
*/
GraphQLSchemaBuilder scalar(Class<?> javaType, GraphQLScalarType scalarType);

/**
* Builds {code #GraphQLSchema} instance
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@

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

import java.beans.Introspector;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.persistence.Convert;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EmbeddableType;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnore;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreFilter;
import com.introproventures.graphql.jpa.query.annotation.GraphQLIgnoreOrder;
Expand All @@ -41,6 +66,7 @@
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
import graphql.schema.GraphQLTypeReference;
Expand All @@ -49,31 +75,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.Convert;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EmbeddableType;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;
import java.beans.Introspector;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static graphql.Scalars.GraphQLBoolean;
import static graphql.schema.GraphQLArgument.newArgument;
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;
Expand Down Expand Up @@ -133,6 +134,7 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder {
private boolean enableResultStream = false;

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

private final Relay relay = new Relay();

Expand All @@ -151,6 +153,8 @@ public GraphQLJpaSchemaBuilder(EntityManager entityManager) {
*/
@Override
public GraphQLSchema build() {
scalars.forEach((javaType, scalarType) -> JavaScalars.register(javaType, scalarType));

GraphQLSchema.Builder schema = GraphQLSchema.newSchema()
.query(getQueryType());

Expand All @@ -165,6 +169,12 @@ public GraphQLSchema build() {
return schema.build();
}

public GraphQLJpaSchemaBuilder scalar(Class<?> javaType, GraphQLScalarType scalarType) {
scalars.put(javaType, scalarType);

return this;
}

private GraphQLObjectType getQueryType() {
GraphQLObjectType.Builder queryType =
GraphQLObjectType.newObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

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

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Optional;
import javax.persistence.EntityManager;

import com.introproventures.graphql.jpa.query.AbstractSpringBootTestSupport;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
import graphql.Scalars;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,13 +33,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;

import com.introproventures.graphql.jpa.query.AbstractSpringBootTestSupport;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;

import graphql.Scalars;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
public class StarwarsSchemaBuildTest extends AbstractSpringBootTestSupport {
Expand Down Expand Up @@ -188,5 +187,25 @@ public void testBuildSchema(){
//then
assertThat(schema).isNotNull();
}

@Test
public void scalar() {
// given
GraphQLScalarType scalarType = GraphQLScalarType.newScalar()
.name("TestObject")
.coercing(new JavaScalars.GraphQLObjectCoercing())
.build();

// when
builder.scalar(Object.class, scalarType)
.build();

// then
Optional<GraphQLScalarType> result = JavaScalars.of(scalarType.getName());

assertThat(result).isNotEmpty()
.get()
.isEqualTo(scalarType);
}

}