Skip to content

Commit

Permalink
Fixed graphql and catch all forward to react index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Jun 29, 2023
1 parent 5d088c8 commit 8aa4f0c
Show file tree
Hide file tree
Showing 11 changed files with 1,006 additions and 991 deletions.
2 changes: 1 addition & 1 deletion compile_and_move.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
BASEDIR=$(dirname "$0")
cd "$BASEDIR" || exit 1

./gradlew clean shadow || exit 1
./gradlew shadow || exit 1
other/scripts/set_snapshot.sh "solarthing" client/build/libs/client-0.0.1-SNAPSHOT-all.jar || exit 1
echo "Compiled and moved successfully"
8 changes: 4 additions & 4 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ dependencies {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
// https://mvnrepository.com/artifact/com.graphql-java/graphql-java-spring-boot-starter-webmvc
implementation 'com.graphql-java:graphql-java-spring-boot-starter-webmvc:2.0'
// testImplementation 'org.springframework:spring-test'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
implementation('org.springframework.boot:spring-boot-starter-graphql') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

// implementation 'com.graphql-java:graphql-java:14.0' graphql-spqr adds this dependency
implementation 'io.leangen.graphql:spqr:0.12.2' // https://github.com/leangen/graphql-spqr/releases
implementation 'io.leangen.graphql:spqr:0.12.3' // https://github.com/leangen/graphql-spqr/releases

// Although we don't need this dependency, we need to bump the version so that couchdb has the version it needs
api group: 'com.squareup.okhttp3', name: 'okhttp', version: "$okhttpVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.graphql.servlet.GraphQlWebMvcAutoConfiguration;

@SpringBootApplication
@SpringBootApplication(exclude = {GraphQlWebMvcAutoConfiguration.class})
public class SolarThingGraphQLApplication {

public SolarThingGraphQLApplication() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
package me.retrodaredevil.solarthing.rest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.graphql.ExecutionGraphQlService;
import org.springframework.graphql.execution.DefaultExecutionGraphQlService;
import org.springframework.graphql.execution.GraphQlSource;
import org.springframework.graphql.server.WebGraphQlHandler;
import org.springframework.graphql.server.webmvc.GraphQlHttpHandler;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.function.RequestPredicates;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;

@Configuration
public class WebConfiguration implements WebMvcConfigurer {
// thanks https://stackoverflow.com/a/42998817/5434860
@Override
public void addViewControllers(ViewControllerRegistry registry) {
public class WebConfiguration {

@Bean
public HandlerMapping catchAllHandlerMapping(ApplicationContext applicationContext) {
// related to https://stackoverflow.com/a/42998817/5434860
// We have to do it this way, so we can set the order so this has the lowest precedence
var registry = new ViewControllerRegistry(applicationContext) {
@Override
public SimpleUrlHandlerMapping buildHandlerMapping() {
return super.buildHandlerMapping();
}
};
registry.setOrder(5);
registry.addViewController("/{spring:[a-zA-Z0-9-_]+}")
.setViewName("forward:/");
return registry.buildHandlerMapping();
}

@Bean
public RouterFunction<ServerResponse> graphQLRouterFunction(GraphQlSource graphQlSource) {
ExecutionGraphQlService executionGraphQlService = new DefaultExecutionGraphQlService(graphQlSource);
GraphQlHttpHandler handler = new GraphQlHttpHandler(
WebGraphQlHandler.builder(executionGraphQlService)
.build()
);
return RouterFunctions.route(RequestPredicates.path("/graphql"), handler::handleRequest);
// return RouterFunctions.route()
// .POST("/graphql", RequestPredicates.contentType(MediaType.APPLICATION_JSON), handler::handleRequest)
// .build()
// ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.leangen.graphql.generator.mapping.common.NonNullMapper;
import io.leangen.graphql.metadata.strategy.query.ResolverBuilder;
import io.leangen.graphql.metadata.strategy.value.jackson.JacksonValueMapperFactory;
import jakarta.annotation.PostConstruct;
import me.retrodaredevil.solarthing.annotations.NotNull;
import me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings;
import me.retrodaredevil.solarthing.packets.collection.DefaultInstanceOptions;
Expand All @@ -26,9 +27,9 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.graphql.execution.GraphQlSource;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -156,8 +157,9 @@ static GraphQLSchemaGenerator createGraphQLSchemaGenerator(ObjectMapper objectMa


@Bean
public GraphQL graphQL() {
return graphQL;
public GraphQlSource graphQlSource() {
return new SimpleGraphQlSource(graphQL, graphQL.getGraphQLSchema());
}
private record SimpleGraphQlSource(GraphQL graphQl, GraphQLSchema schema) implements GraphQlSource{
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.retrodaredevil.solarthing.rest.spring;

import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.nio.file.Path;
import java.util.Map;

@Component
public class ApplicationConfig {

@Bean
public CustomEditorConfigurer customEditorConfigurer() {
CustomEditorConfigurer configurer = new CustomEditorConfigurer();
configurer.setCustomEditors(Map.of(
Path.class, NioPathPropertyEditorSupport.class
));

return configurer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.retrodaredevil.solarthing.rest.spring;

import java.beans.PropertyEditorSupport;
import java.nio.file.Path;

public class NioPathPropertyEditorSupport extends PropertyEditorSupport {
@Override
public String getAsText() {
Path path = (Path) getValue();
return path.toString();
}

@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Path.of(text));
}
}
2 changes: 1 addition & 1 deletion server_compile_and_move.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
BASEDIR=$(dirname "$0")
cd "$BASEDIR" || exit 1

./gradlew clean server:bootJar || exit 1
./gradlew server:bootJar || exit 1
other/scripts/set_snapshot.sh "solarthing-graphql" server/build/libs/server-0.0.1-SNAPSHOT.jar || exit 1
echo "Compiled and moved graphql boot jar successfully"
Loading

0 comments on commit 8aa4f0c

Please sign in to comment.