Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ target/
# Scala
.cache-main
.cache-tests

.sts4-cache
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,83 @@
*/
package com.introproventures.graphql.jpa.query.boot.autoconfigure;

import javax.persistence.EntityManager;
import java.util.function.Supplier;

import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaConfigurer;
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutionInputFactory;
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutorContextFactory;
import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutorContextFactory;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;

import graphql.GraphQL;
import graphql.GraphQLContext;
import graphql.execution.instrumentation.Instrumentation;
import graphql.schema.GraphQLSchema;
import graphql.schema.visibility.GraphqlFieldVisibility;

@Configuration
@ConditionalOnClass(GraphQL.class)
@ConditionalOnProperty(name="spring.graphql.jpa.query.enabled", havingValue="true", matchIfMissing=true)
@AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
public class GraphQLJpaQueryAutoConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnSingleCandidate(EntityManagerFactory.class)
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManagerFactory entityManagerFactory) {
return new GraphQLJpaSchemaBuilder(entityManagerFactory.createEntityManager());
}

@Bean
@ConditionalOnMissingBean
public GraphQLSchemaConfigurer graphQLJpaQuerySchemaConfigurer(GraphQLSchemaBuilder graphQLSchemaBuilder) {

return (registry) -> {
registry.register(graphQLSchemaBuilder.build());
};
}

@Configuration
public static class DefaultGraphQLJpaQueryConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnSingleCandidate(EntityManagerFactory.class)
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManagerFactory entityManagerFactory) {
return new GraphQLJpaSchemaBuilder(entityManagerFactory.createEntityManager());
}

@Bean
@ConditionalOnMissingBean(GraphQLExecutor.class)
public GraphQLExecutor graphQLExecutor(GraphQLSchema graphQLSchema) {
return new GraphQLJpaExecutor(graphQLSchema);
@ConditionalOnMissingBean
public GraphQLSchemaConfigurer graphQLJpaQuerySchemaConfigurer(GraphQLSchemaBuilder graphQLSchemaBuilder) {

return (registry) -> {
registry.register(graphQLSchemaBuilder.build());
};
}

@Bean
@ConditionalOnMissingBean
public GraphQLExecutorContextFactory graphQLExecutorContextFactory(ObjectProvider<GraphQLExecutionInputFactory> graphQLExecutionInputFactory,
ObjectProvider<Supplier<GraphqlFieldVisibility>> graphqlFieldVisibility,
ObjectProvider<Supplier<Instrumentation>> instrumentation,
ObjectProvider<Supplier<GraphQLContext>> graphqlContext) {
GraphQLJpaExecutorContextFactory bean = new GraphQLJpaExecutorContextFactory();

graphQLExecutionInputFactory.ifAvailable(bean::withExecutionInputFactory);
graphqlFieldVisibility.ifAvailable(bean::withGraphqlFieldVisibility);
instrumentation.ifAvailable(bean::withInstrumentation);
graphqlContext.ifAvailable(bean::withGraphqlContext);

return bean;
}

@Bean
@ConditionalOnMissingBean(GraphQLSchemaBuilder.class)
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManager) {
return new GraphQLJpaSchemaBuilder(entityManager);
@ConditionalOnMissingBean
public GraphQLExecutor graphQLExecutor(GraphQLSchema graphQLSchema,
GraphQLExecutorContextFactory graphQLExecutorContextFactory) {
return new GraphQLJpaExecutor(graphQLSchema,
graphQLExecutorContextFactory);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,36 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.introproventures.graphql.jpa.query.boot.autoconfigure;
package com.introproventures.graphql.jpa.query.boot.test.boot.autoconfigure;

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

import java.util.function.Supplier;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import com.introproventures.graphql.jpa.query.boot.test.starter.model.Author;
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutionInputFactory;
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutorContextFactory;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
import com.introproventures.graphql.jpa.query.starter.model.Author;

import graphql.GraphQLContext;
import graphql.execution.instrumentation.Instrumentation;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.visibility.GraphqlFieldVisibility;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
Expand All @@ -42,14 +51,41 @@ public class GraphQLJpaQueryAutoConfigurationTest {
@SpringBootApplication
@EntityScan(basePackageClasses=Author.class)
static class Application {
@MockBean
private GraphQLExecutionInputFactory mockExecutionInputFactory;

@MockBean
private Supplier<Instrumentation> mockInstrumentation;

@MockBean
private Supplier<GraphqlFieldVisibility> mockGraphqlFieldVisibility;

@MockBean
private Supplier<GraphQLContext> graphqlContext;

}

@Autowired(required=false)
private GraphQLExecutor graphQLExecutor;

@Autowired(required=false)
private GraphQLSchemaBuilder graphQLSchemaBuilder;

@Autowired(required=false)
private GraphQLJpaExecutorContextFactory executorContextFactory;

@Autowired
private ObjectProvider<GraphQLExecutionInputFactory> executionInputFactory;

@Autowired
private ObjectProvider<Supplier<Instrumentation>> instrumentation;

@Autowired
private ObjectProvider<Supplier<GraphqlFieldVisibility>> graphqlFieldVisibility;

@Autowired
private ObjectProvider<Supplier<GraphQLContext>> graphqlContext;

@Autowired
private GraphQLSchema graphQLSchema;

Expand All @@ -61,6 +97,18 @@ public void contextIsAutoConfigured() {
assertThat(graphQLSchemaBuilder).isNotNull()
.isInstanceOf(GraphQLJpaSchemaBuilder.class);

assertThat(executorContextFactory).isNotNull()
.isInstanceOf(GraphQLJpaExecutorContextFactory.class);

assertThat(executionInputFactory.getIfAvailable()).isNotNull();
assertThat(instrumentation.getIfAvailable()).isNotNull();
assertThat(graphqlFieldVisibility.getIfAvailable()).isNotNull();
assertThat(graphqlContext.getIfAvailable()).isNotNull();

assertThat(executorContextFactory.getExecutionInputFactory()).isEqualTo(executionInputFactory.getObject());
assertThat(executorContextFactory.getInstrumentation()).isEqualTo(instrumentation.getObject());
assertThat(executorContextFactory.getGraphqlFieldVisibility()).isEqualTo(graphqlFieldVisibility.getObject());
assertThat(executorContextFactory.getGraphqlContext()).isEqualTo(graphqlContext.getObject());

assertThat(graphQLSchema.getQueryType())
.extracting(GraphQLObjectType::getName, GraphQLObjectType::getDescription)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.introproventures.graphql.jpa.query.starter;
package com.introproventures.graphql.jpa.query.boot.test.starter;

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

Expand All @@ -37,7 +37,7 @@

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.introproventures.graphql.jpa.query.starter.Result.GraphQLError;
import com.introproventures.graphql.jpa.query.boot.test.starter.Result.GraphQLError;
import com.introproventures.graphql.jpa.query.web.GraphQLController.GraphQLQueryRequest;

@RunWith(SpringRunner.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.introproventures.graphql.jpa.query.starter.model;
package com.introproventures.graphql.jpa.query.boot.test.starter.model;

import java.util.Collection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.introproventures.graphql.jpa.query.starter.model;
package com.introproventures.graphql.jpa.query.boot.test.starter.model;

import javax.persistence.Entity;
import javax.persistence.EnumType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.introproventures.graphql.jpa.query.starter.model;
package com.introproventures.graphql.jpa.query.boot.test.starter.model;

public enum Genre {
NOVEL, PLAY
Expand Down
24 changes: 2 additions & 22 deletions graphql-jpa-query-example-merge/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,12 @@
<groupId>com.introproventures</groupId>
<artifactId>graphql-jpa-query-example-model-starwars</artifactId>
</dependency>

<dependency>
<groupId>com.introproventures</groupId>
<artifactId>graphql-jpa-query-web</artifactId>
</dependency>

<dependency>
<groupId>com.introproventures</groupId>
<artifactId>graphql-jpa-query-schema</artifactId>
</dependency>

<dependency>
<groupId>com.introproventures</groupId>
<artifactId>graphql-jpa-query-autoconfigure</artifactId>
<artifactId>graphql-jpa-query-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>


<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@
*/
package com.introproventures.graphql.jpa.query.example;

import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
import graphql.schema.GraphQLSchema;
import java.util.function.Supplier;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.context.annotation.RequestScope;

import graphql.GraphQLContext;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.SimpleInstrumentation;
import graphql.execution.instrumentation.tracing.TracingInstrumentation;

/**
* GraphQL JPA Query Example with Spring Boot Autoconfiguration
Expand All @@ -34,14 +43,28 @@
@SpringBootApplication
@EnableTransactionManagement
public class Application {

private static final Logger logger = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}


@Bean
@RequestScope
public Supplier<GraphQLContext> graphqlContext(HttpServletRequest request) {
return () -> GraphQLContext.newContext()
.of("request", request)
.of("user", request)
.build();
}

@Bean
public GraphQLExecutor graphQLExecutor(GraphQLSchema graphQLSchema) {
return new GraphQLJpaExecutor(graphQLSchema);
@RequestScope
public Supplier<Instrumentation> instrumentation(HttpServletRequest request) {
return () -> logger.isDebugEnabled()
? new TracingInstrumentation()
: SimpleInstrumentation.INSTANCE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import graphql.ExecutionInput;

public interface GraphQLExecutionInputFactory {

default ExecutionInput.Builder create() {
return ExecutionInput.newExecutionInput();
};

}
Loading