Skip to content

feat(GH-235): added Relay Connection support #236

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
Jan 21, 2020
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 @@ -50,6 +50,11 @@ public class GraphQLJpaQueryProperties {
* Set default value for optional argument for join fetch collections.
*/
private boolean toManyDefaultOptional = true;

/**
* Enable or disable GraphQL Relay Connection support. Default is false
*/
private boolean enableRelay = false;

/**
* Enable or disable QraphQL module services.
Expand Down Expand Up @@ -158,5 +163,15 @@ public boolean isToManyDefaultOptional() {
public void setToManyDefaultOptional(boolean toManyDefaultOptional) {
this.toManyDefaultOptional = toManyDefaultOptional;
}


public boolean isEnableRelay() {
return enableRelay;
}


public void setEnableRelay(boolean enableRelay) {
this.enableRelay = enableRelay;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
package com.introproventures.graphql.jpa.query.boot.autoconfigure;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaConfigurer;
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLShemaRegistration;
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;
Expand All @@ -38,21 +39,22 @@
@ConditionalOnProperty(name="spring.graphql.jpa.query.enabled", havingValue="true", matchIfMissing=true)
public class GraphQLJpaQueryAutoConfiguration {

@Configuration
public static class GraphQLJpaQuerySchemaConfigurer implements GraphQLSchemaConfigurer {

private final GraphQLSchemaBuilder graphQLSchemaBuilder;

public GraphQLJpaQuerySchemaConfigurer(GraphQLSchemaBuilder graphQLSchemaBuilder) {
this.graphQLSchemaBuilder = graphQLSchemaBuilder;
}

@Override
public void configure(GraphQLShemaRegistration registry) {
registry.register(graphQLSchemaBuilder.build());
}
@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 {

Expand Down
67 changes: 67 additions & 0 deletions graphql-jpa-query-example-relay/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>graphql-jpa-query-example-relay</artifactId>

<parent>
<groupId>com.introproventures</groupId>
<artifactId>graphql-jpa-query-build</artifactId>
<version>0.3.38-SNAPSHOT</version>
<relativePath>../graphql-jpa-query-build</relativePath>
</parent>

<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<skipDocker>false</skipDocker>
</properties>

<dependencies>

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

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

<dependency>
<groupId>com.introproventures</groupId>
<artifactId>graphql-jpa-query-example-model-books</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.example.relay;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;

import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLJpaQueryProperties;
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaConfigurer;
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLShemaRegistration;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
import com.introproventures.graphql.jpa.query.schema.model.book.Book;

/**
* GraphQL JPA Query Example Relay with Spring Boot Autoconfiguration
*
* You can configure GraphQL JPA Query properties in application.yml
*
* @author Igor Dianov
*
*/
@SpringBootApplication
@EntityScan(basePackageClasses=Book.class)
public class Application {

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

@Configuration
public static class GraphQLJpaQuerySchemaConfigurer implements GraphQLSchemaConfigurer {

private final EntityManager entityManager;

@Autowired
private GraphQLJpaQueryProperties properties;

public GraphQLJpaQuerySchemaConfigurer(EntityManager entityManager) {
this.entityManager = entityManager;
}

@Override
public void configure(GraphQLShemaRegistration registry) {
registry.register(
new GraphQLJpaSchemaBuilder(entityManager)
.name(properties.getName())
.description(properties.getDescription())
.useDistinctParameter(properties.isUseDistinctParameter())
.defaultDistinct(properties.isDefaultDistinct())
.enableRelay(properties.isEnableRelay())
.build()
);
}
}

}
22 changes: 22 additions & 0 deletions graphql-jpa-query-example-relay/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
spring:
jpa:
hibernate.ddl-auto: create-drop
generate-ddl: true
show-sql: true
open-in-view: false
properties:
hibernate.format_sql: true
h2:
console.enabled: true

graphql:
jpa:
query:
name: BooksRelay
description: GraphQL Jpa Query Books Relay Example
enable-relay: true

logging:
level:
com.introproventures.graphql.jpa.query.schema: DEBUG

26 changes: 26 additions & 0 deletions graphql-jpa-query-example-relay/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Books
insert into author (id, name, genre) values (1, 'Leo Tolstoy', 'NOVEL');
insert into book (id, title, author_id, genre, publication_date, description)
values (2, 'War and Peace', 1, 'NOVEL', '1869-01-01', 'The novel chronicles the history of the French invasion of Russia and the impact of the Napoleonic era on Tsarist society through the stories of five Russian aristocratic families.');
insert into book (id, title, author_id, genre, publication_date, description)
values (3, 'Anna Karenina', 1, 'NOVEL', '1877-04-01', 'A complex novel in eight parts, with more than a dozen major characters, it is spread over more than 800 pages (depending on the translation), typically contained in two volumes.');
insert into author (id, name, genre) values (4, 'Anton Chekhov', 'PLAY');
insert into book (id, title, author_id, genre, publication_date, description)
values (5, 'The Cherry Orchard', 4, 'PLAY', '1904-01-17', 'The play concerns an aristocratic Russian landowner who returns to her family estate (which includes a large and well-known cherry orchard) just before it is auctioned to pay the mortgage.');
insert into book (id, title, author_id, genre, publication_date, description)
values (6, 'The Seagull', 4, 'PLAY', '1896-10-17', 'It dramatises the romantic and artistic conflicts between four characters');
insert into book (id, title, author_id, genre, publication_date, description)
values (7, 'Three Sisters', 4, 'PLAY', '1900-01-01', 'The play is sometimes included on the short list of Chekhov''s outstanding plays, along with The Cherry Orchard, The Seagull and Uncle Vanya.[1]');
insert into author (id, name, genre) values (8, 'Igor Dianov', 'JAVA');

insert into book_tags (book_id, tags) values (2, 'war'), (2, 'piece');
insert into book_tags (book_id, tags) values (3, 'anna'), (3, 'karenina');
insert into book_tags (book_id, tags) values (5, 'cherry'), (5, 'orchard');
insert into book_tags (book_id, tags) values (6, 'seagull');
insert into book_tags (book_id, tags) values (7, 'three'), (7, 'sisters');

insert into author_phone_numbers(phone_number, author_id) values
('1-123-1234', 1),
('1-123-5678', 1),
('4-123-1234', 4),
('4-123-5678', 4);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.introproventures.graphql.jpa.query.example.relay;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class ApplicationTest {

@Test
public void contextLoads() {
// success
}

}
Loading