Skip to content

fix: add enable result stream feature for JPA queries #317

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

import static com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder.SELECT_DISTINCT_PARAM_NAME;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.getObjectField;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isAfterArgument;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isDistinctArgument;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isFirstArgument;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isLogicalArgument;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isPageArgument;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isWhereArgument;
import static graphql.introspection.Introspection.SchemaMetaFieldDef;
import static graphql.introspection.Introspection.TypeMetaFieldDef;
import static graphql.introspection.Introspection.TypeNameMetaFieldDef;
import static java.util.stream.Collectors.groupingBy;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
Expand All @@ -52,7 +39,6 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.AbstractQuery;
Expand All @@ -74,11 +60,6 @@
import javax.persistence.metamodel.PluralAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;

import graphql.execution.CoercedVariables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.introproventures.graphql.jpa.query.annotation.GraphQLDefaultOrderBy;
import com.introproventures.graphql.jpa.query.introspection.ReflectionUtil;
import com.introproventures.graphql.jpa.query.schema.JavaScalars;
Expand All @@ -87,8 +68,8 @@
import com.introproventures.graphql.jpa.query.schema.impl.EntityIntrospector.EntityIntrospectionResult.AttributePropertyDescriptor;
import com.introproventures.graphql.jpa.query.schema.impl.PredicateFilter.Criteria;
import com.introproventures.graphql.jpa.query.support.GraphQLSupport;

import graphql.GraphQLException;
import graphql.execution.CoercedVariables;
import graphql.execution.MergedField;
import graphql.execution.ValuesResolver;
import graphql.language.Argument;
Expand All @@ -113,6 +94,21 @@
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder.SELECT_DISTINCT_PARAM_NAME;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.getObjectField;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isAfterArgument;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isDistinctArgument;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isFirstArgument;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isLogicalArgument;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isPageArgument;
import static com.introproventures.graphql.jpa.query.support.GraphQLSupport.isWhereArgument;
import static graphql.introspection.Introspection.SchemaMetaFieldDef;
import static graphql.introspection.Introspection.TypeMetaFieldDef;
import static graphql.introspection.Introspection.TypeNameMetaFieldDef;
import static java.util.stream.Collectors.groupingBy;

/**
* Provides implemetation for GraphQL JPA Query Factory
Expand Down Expand Up @@ -143,6 +139,7 @@ public final class GraphQLJpaQueryFactory {
private final GraphQLObjectType entityObjectType;
private final int defaultFetchSize;
private final RestrictedKeysProvider restrictedKeysProvider;
private final boolean resultStream;

private GraphQLJpaQueryFactory(Builder builder) {
this.entityManager = builder.entityManager;
Expand All @@ -153,6 +150,7 @@ private GraphQLJpaQueryFactory(Builder builder) {
this.defaultDistinct = builder.defaultDistinct;
this.defaultFetchSize = builder.defaultFetchSize;
this.restrictedKeysProvider = builder.restrictedKeysProvider;
this.resultStream = builder.resultStream;
}

public DataFetchingEnvironment getQueryEnvironment(DataFetchingEnvironment environment,
Expand Down Expand Up @@ -260,7 +258,12 @@ protected <T> Stream<T> getResultStream(TypedQuery<T> query,
logger.info("\nGraphQL JPQL Fetch Query String:\n {}", getJPQLQueryString(query));
}

// Let's execute query and get wrap result into stream
if (resultStream) {
return query.getResultStream()
.peek(entityManager::detach);
}

// Let's execute query and wrap result into stream
return query.getResultList()
.stream()
.peek(entityManager::detach);
Expand Down Expand Up @@ -1952,6 +1955,13 @@ public interface IBuildStage {
*/
public IBuildStage withDefaultDistinct(boolean defaultDistinct);

/**
* Builder method for resultStream parameter.
* @param resultStream field to set
* @return builder
*/
public IBuildStage withResultStream(boolean resultStream);

/**
* Builder method for defaultFetchSize parameter.
* @param defaultFetchSize field to set
Expand Down Expand Up @@ -1987,6 +1997,7 @@ public static final class Builder implements IEntityManagerStage, IEntityTypeSta
private boolean toManyDefaultOptional = true;
private boolean defaultDistinct = true;
private int defaultFetchSize = 100;
private boolean resultStream = false;

private Builder() {
}
Expand Down Expand Up @@ -2027,6 +2038,12 @@ public IBuildStage withDefaultDistinct(boolean defaultDistinct) {
return this;
}

@Override
public IBuildStage withResultStream(boolean resultStream) {
this.resultStream = resultStream;
return this;
}

@Override
public IBuildStage withDefaultFetchSize(int defaultFetchSize) {
this.defaultFetchSize = defaultFetchSize;
Expand All @@ -2038,7 +2055,7 @@ public IBuildStage withRestrictedKeysProvider(RestrictedKeysProvider restrictedK
this.restrictedKeysProvider = restrictedKeysProvider;
return this;
}

@Override
public GraphQLJpaQueryFactory build() {
Objects.requireNonNull(restrictedKeysProvider, "restrictedKeysProvider must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder {
private int defaultFetchSize = 100;
private int defaultPageLimitSize = 100;
private boolean enableDefaultMaxResults = true;
private boolean enableResultStream = false;

private RestrictedKeysProvider restrictedKeysProvider = (entityDescriptor) -> Optional.of(Collections.emptyList());

Expand Down Expand Up @@ -216,6 +217,7 @@ private GraphQLFieldDefinition getQueryFieldByIdDefinition(EntityType<?> entityT
.withSelectNodeName(entityObjectType.getName())
.withToManyDefaultOptional(toManyDefaultOptional)
.withRestrictedKeysProvider(restrictedKeysProvider)
.withResultStream(enableResultStream)
.build();

DataFetcher<Object> dataFetcher = GraphQLJpaSimpleDataFetcher.builder()
Expand Down Expand Up @@ -259,6 +261,7 @@ private GraphQLFieldDefinition getQueryFieldSelectDefinition(EntityType<?> entit
.withDefaultDistinct(isDefaultDistinct)
.withDefaultFetchSize(defaultFetchSize)
.withRestrictedKeysProvider(restrictedKeysProvider)
.withResultStream(enableResultStream)
.build();

if(enableRelay) {
Expand Down Expand Up @@ -337,6 +340,7 @@ private GraphQLFieldDefinition getQueryFieldStreamDefinition(EntityType<?> entit
.withToManyDefaultOptional(toManyDefaultOptional)
.withDefaultDistinct(isDefaultDistinct)
.withRestrictedKeysProvider(restrictedKeysProvider)
.withResultStream(enableResultStream)
.build();

DataFetcher<Object> dataFetcher = GraphQLJpaStreamDataFetcher.builder()
Expand Down Expand Up @@ -939,6 +943,7 @@ && isNotIgnoredOrder(attribute) ) {
.withSelectNodeName(entityObjectType.getName())
.withDefaultDistinct(isDefaultDistinct)
.withRestrictedKeysProvider(restrictedKeysProvider)
.withResultStream(enableResultStream)
.build();

String dataLoaderKey = baseEntity.getName() + "." + attribute.getName();
Expand Down Expand Up @@ -975,6 +980,7 @@ else if (attribute instanceof PluralAttribute
.withSelectNodeName(entityObjectType.getName())
.withDefaultDistinct(isDefaultDistinct)
.withRestrictedKeysProvider(restrictedKeysProvider)
.withResultStream(enableResultStream)
.build();

String dataLoaderKey = baseEntity.getName() + "." + attribute.getName();
Expand Down Expand Up @@ -1460,6 +1466,16 @@ public GraphQLJpaSchemaBuilder restrictedKeysProvider(RestrictedKeysProvider res

public RestrictedKeysProvider getRestrictedKeysProvider() {
return restrictedKeysProvider;
}
}

public boolean isEnableResultStream() {
return enableResultStream;
}

public GraphQLJpaSchemaBuilder enableResultStream(boolean enableResultStream) {
this.enableResultStream = enableResultStream;

return this;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.introproventures.graphql.jpa.query;

import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,35 @@

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.api.BDDAssertions.thenCode;

import java.util.Optional;

import javax.persistence.EntityManager;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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 com.introproventures.graphql.jpa.query.schema.model.book.Author;
import com.introproventures.graphql.jpa.query.schema.model.book.Book;
import com.introproventures.graphql.jpa.query.schema.model.book_superclass.SuperAuthor;
import com.introproventures.graphql.jpa.query.schema.model.book_superclass.SuperBook;
import com.introproventures.graphql.jpa.query.schema.model.uuid.Thing;

import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLSchema;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.api.BDDAssertions.thenCode;

@SpringBootTest
public class BooksSchemaBuildTest extends AbstractSpringBootTestSupport {

@SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
static class TestConfiguration {
@Bean
public GraphQLSchemaBuilder graphQLSchemaBuilder(EntityManager entityManager) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
package com.introproventures.graphql.jpa.query.schema;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.util.Lists.list;

import java.util.Optional;

import javax.persistence.EntityManager;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.util.Assert;

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

import graphql.ExecutionResult;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLSchema;
import graphql.validation.ValidationErrorType;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.util.Assert;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.util.Lists.list;

@SpringBootTest
public class CalculatedEntityTests extends AbstractSpringBootTestSupport {
@SpringBootApplication

@SpringBootConfiguration
@EnableAutoConfiguration
static class Application {
@Bean
public GraphQLExecutor graphQLExecutor(final GraphQLSchemaBuilder graphQLSchemaBuilder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
package com.introproventures.graphql.jpa.query.schema;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.Mockito.when;

import java.util.NoSuchElementException;
import java.util.Optional;

import javax.persistence.EntityManager;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import com.introproventures.graphql.jpa.query.AbstractSpringBootTestSupport;
import com.introproventures.graphql.jpa.query.schema.impl.EntityIntrospector;
import com.introproventures.graphql.jpa.query.schema.impl.EntityIntrospector.EntityIntrospectionResult;
import com.introproventures.graphql.jpa.query.schema.impl.EntityIntrospector.EntityIntrospectionResult.AttributePropertyDescriptor;
import com.introproventures.graphql.jpa.query.schema.model.calculated.CalculatedEntity;
import com.introproventures.graphql.jpa.query.schema.model.calculated.ParentCalculatedEntity;
import com.introproventures.graphql.jpa.query.schema.model.metamodel.ClassWithCustomMetamodel;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.Mockito.when;

@SpringBootTest
@SpringBootTest(classes = EntityIntrospectorTest.Application.class)
public class EntityIntrospectorTest extends AbstractSpringBootTestSupport {

@SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
static class Application {
}

Expand Down
Loading