Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Feature/fix playground webflux #521

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
1 change: 1 addition & 0 deletions example-graphql-subscription/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply plugin: "org.springframework.boot"
dependencies {
implementation(project(":graphql-spring-boot-starter"))
implementation(project(":graphiql-spring-boot-starter"))
implementation(project(":playground-spring-boot-starter"))
implementation "com.graphql-java-kickstart:graphql-java-tools:$LIB_GRAPHQL_JAVA_TOOLS_VER"

implementation "io.reactivex.rxjava2:rxjava"
Expand Down
1 change: 1 addition & 0 deletions example-webflux/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
implementation(project(":graphql-kickstart-spring-boot-starter-webflux"))
implementation(project(":graphql-kickstart-spring-boot-starter-tools"))
implementation(project(":voyager-spring-boot-starter"))
implementation(project(":playground-spring-boot-starter"))

implementation("org.springframework.boot:spring-boot-starter-webflux:$LIB_SPRING_BOOT_VER")
implementation("org.springframework.boot:spring-boot-starter-actuator:$LIB_SPRING_BOOT_VER")
Expand Down
6 changes: 5 additions & 1 deletion playground-spring-boot-autoconfigure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ dependencies{
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"

implementation "org.springframework.boot:spring-boot-autoconfigure"
implementation "org.springframework.boot:spring-boot-starter-web"
compileOnly "org.springframework.boot:spring-boot-starter-web"
compileOnly "org.springframework.boot:spring-boot-starter-webflux"
compileOnly "org.springframework.boot:spring-boot-starter-security"
implementation "org.springframework.boot:spring-boot-starter-thymeleaf"
implementation "org.springframework.boot:spring-boot-starter-validation"

testImplementation "org.springframework.boot:spring-boot-starter-web"
testImplementation "org.springframework.boot:spring-boot-starter-webflux"
testImplementation "org.springframework.boot:spring-boot-starter-test"
testImplementation "org.springframework.boot:spring-boot-starter-security"
testImplementation "org.springframework.security:spring-security-test"
testImplementation "org.jsoup:jsoup:$LIB_JSOUP_VER"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package graphql.kickstart.playground.boot;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(DispatcherServlet.class)
@EnableConfigurationProperties(PlaygroundPropertiesConfiguration.class)
public class PlaygroundAutoConfiguration {

@Bean
@ConditionalOnProperty(value = "graphql.playground.enabled", havingValue = "true", matchIfMissing = true)
PlaygroundController playgroundController(final PlaygroundPropertiesConfiguration playgroundPropertiesConfiguration,
public PlaygroundController playgroundController(final PlaygroundPropertiesConfiguration playgroundPropertiesConfiguration,
final ObjectMapper objectMapper) {
return new PlaygroundController(playgroundPropertiesConfiguration, objectMapper);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.Paths;

import static java.util.Objects.nonNull;

@Controller
@RequiredArgsConstructor
public class PlaygroundController {
Expand All @@ -23,21 +25,24 @@ public class PlaygroundController {
private static final String FAVICON_URL_ATTRIBUTE_NAME = "faviconUrl";
private static final String SCRIPT_URL_ATTRIBUTE_NAME = "scriptUrl";
private static final String LOGO_URL_ATTRIBUTE_NAME = "logoUrl";
private static final String _CSRF = "_csrf";

private final PlaygroundPropertiesConfiguration propertiesConfiguration;

private final ObjectMapper objectMapper;

@GetMapping("${graphql.playground.mapping:/playground}")
public String playground(final Model model, final HttpServletRequest request) {
public String playground(final Model model, final @RequestAttribute(value = _CSRF, required = false) Object csrf) {
if (propertiesConfiguration.getPlayground().getCdn().isEnabled()) {
setCdnUrls(model);
} else {
setLocalAssetUrls(model);
}
model.addAttribute("pageTitle", propertiesConfiguration.getPlayground().getPageTitle());
model.addAttribute("properties", objectMapper.valueToTree(propertiesConfiguration.getPlayground()));
model.addAttribute("_csrf", request.getAttribute("_csrf"));
if (nonNull(csrf)) {
model.addAttribute(_CSRF, csrf);
}
return "playground";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package graphql.kickstart.playground.boot;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.reactive.config.ViewResolverRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.thymeleaf.spring5.SpringWebFluxTemplateEngine;
import org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import java.nio.charset.StandardCharsets;

@Configuration
@Import(PlaygroundWebFluxControllerAdvice.class)
@ConditionalOnClass(WebFluxConfigurer.class)
@ConditionalOnProperty(value = "graphql.playground.enabled", havingValue = "true", matchIfMissing = true)
@RequiredArgsConstructor
public class PlaygroundWebFluxAutoConfiguration implements WebFluxConfigurer {

private final ApplicationContext applicationContext;

@Bean
public RouterFunction<ServerResponse> playgroundStaticFilesRouter() {
return RouterFunctions.resources("/vendor/playground/**", new ClassPathResource("static/vendor/playground/"));
}

@Override
public void configureViewResolvers(final ViewResolverRegistry registry) {
final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding(StandardCharsets.UTF_8.displayName());
templateResolver.setOrder(1);
templateResolver.setCheckExistence(true);
final SpringWebFluxTemplateEngine springWebFluxTemplateEngine = new SpringWebFluxTemplateEngine();
springWebFluxTemplateEngine.setTemplateResolver(templateResolver);
final ThymeleafReactiveViewResolver thymeleafReactiveViewResolver = new ThymeleafReactiveViewResolver();
thymeleafReactiveViewResolver.setDefaultCharset(StandardCharsets.UTF_8);
thymeleafReactiveViewResolver.setApplicationContext(applicationContext);
thymeleafReactiveViewResolver.setTemplateEngine(springWebFluxTemplateEngine);
thymeleafReactiveViewResolver.setViewNames(new String[] {"playground"});
registry.viewResolver(thymeleafReactiveViewResolver);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package graphql.kickstart.playground.boot;

import lombok.NoArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.security.web.reactive.result.view.CsrfRequestDataValueProcessor;
import org.springframework.security.web.server.csrf.CsrfToken;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@NoArgsConstructor
@ControllerAdvice
@ConditionalOnClass({ CsrfToken.class, CsrfRequestDataValueProcessor.class })
@ConditionalOnBean(CsrfRequestDataValueProcessor.class)
public class PlaygroundWebFluxControllerAdvice {

@ModelAttribute(CsrfRequestDataValueProcessor.DEFAULT_CSRF_ATTR_NAME)
public Mono<CsrfToken> getCsrfToken(final ServerWebExchange exchange) {
return exchange.getAttributeOrDefault(CsrfToken.class.getName(), Mono.empty());
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=graphql.kickstart.playground.boot.PlaygroundAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=graphql.kickstart.playground.boot.PlaygroundAutoConfiguration,graphql.kickstart.playground.boot.PlaygroundWebFluxAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
@SpringBootTest(classes = PlaygroundTestConfig.class)
@AutoConfigureMockMvc
@TestPropertySource("classpath:application-playground-cdn-custom-version-test.properties")
class PlaygroundCdnCustomVersionTest extends PlaygroundResourcesTestBase {
public class PlaygroundCdnCustomVersionTest extends PlaygroundResourcesTestBase {

@Test
void shouldLoadSpecifiedVersionFromCdn() throws Exception {
String LOGO_CDN_PATH = "https://cdn.jsdelivr.net/npm/[email protected]/build/logo.png";
String FAVICON_CDN_PATH = "https://cdn.jsdelivr.net/npm/[email protected]/build/favicon.png";
String SCRIPT_CDN_PATH = "https://cdn.jsdelivr.net/npm/[email protected]/build/static/js/middleware.js";
String CSS_CDN_PATH = "https://cdn.jsdelivr.net/npm/[email protected]/build/static/css/index.css";
testPlaygroundResources(CSS_CDN_PATH, SCRIPT_CDN_PATH, FAVICON_CDN_PATH, LOGO_CDN_PATH);
}
@Test
public void shouldLoadSpecifiedVersionFromCdn() throws Exception {
testPlaygroundResources(
PlaygroundTestHelper.CUSTOM_VERSION_CSS_CDN_PATH,
PlaygroundTestHelper.CUSTOM_VERSION_SCRIPT_CDN_PATH,
PlaygroundTestHelper.CUSTOM_VERSION_FAVICON_CDN_PATH,
PlaygroundTestHelper.CUSTOM_VERSION_LOGO_CDN_PATH
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
@SpringBootTest(classes = PlaygroundTestConfig.class)
@AutoConfigureMockMvc
@TestPropertySource("classpath:application-playground-cdn-test.properties")
class PlaygroundCdnTest extends PlaygroundResourcesTestBase {
public class PlaygroundCdnTest extends PlaygroundResourcesTestBase {

@Test
void shouldLoadLatestVersionFromCdn() throws Exception {
String LOGO_CDN_PATH = "https://cdn.jsdelivr.net/npm/graphql-playground-react@latest/build/logo.png";
String FAVICON_CDN_PATH = "https://cdn.jsdelivr.net/npm/graphql-playground-react@latest/build/favicon.png";
String SCRIPT_CDN_PATH = "https://cdn.jsdelivr.net/npm/graphql-playground-react@latest/build/static/js/middleware.js";
String CSS_CDN_PATH = "https://cdn.jsdelivr.net/npm/graphql-playground-react@latest/build/static/css/index.css";
testPlaygroundResources(CSS_CDN_PATH, SCRIPT_CDN_PATH, FAVICON_CDN_PATH, LOGO_CDN_PATH);
}
@Test
public void shouldLoadLatestVersionFromCdn() throws Exception {
testPlaygroundResources(
PlaygroundTestHelper.DEFAULT_CSS_CDN_PATH,
PlaygroundTestHelper.DEFAULT_SCRIPT_CDN_PATH,
PlaygroundTestHelper.DEFAULT_FAVICON_CDN_PATH,
PlaygroundTestHelper.DEFAULT_LOGO_CDN_PATH
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
@SpringBootTest(classes = PlaygroundTestConfig.class)
@AutoConfigureMockMvc
@TestPropertySource("classpath:application-playground-mapping-test.properties")
class PlaygroundCustomMappingTest {
public class PlaygroundCustomMappingTest {

private static final String CONFIGURED_MAPPING = "/test-mapping";
@Autowired
private MockMvc mockMvc;

@Autowired
private MockMvc mockMvc;

@Test
void shouldUseTheConfiguredRequestMapping() throws Exception {
mockMvc.perform(get(CONFIGURED_MAPPING))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andExpect(content().encoding(StandardCharsets.UTF_8.name()))
.andExpect(content().string(not(isEmptyString())));
}
@Test
public void shouldUseTheConfiguredRequestMapping() throws Exception {
mockMvc.perform(get(PlaygroundTestHelper.CUSTOM_MAPPING))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andExpect(content().encoding(StandardCharsets.UTF_8.name()))
.andExpect(content().string(not(isEmptyString())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
@SpringBootTest(classes = PlaygroundTestConfig.class)
@AutoConfigureMockMvc
@TestPropertySource("classpath:application-playground-custom-static-path.properties")
class PlaygroundCustomStaticPathTest extends PlaygroundResourcesTestBase {
public class PlaygroundCustomStaticPathTest extends PlaygroundResourcesTestBase {

private static final String CUSTOM_CSS_URL = "/custom-static-path/static/css/index.css";
private static final String CUSTOM_SCRIPT_URL = "/custom-static-path/static/js/middleware.js";
private static final String CUSTOM_FAVICON_URL = "/custom-static-path/favicon.png";
private static final String CUSTOM_LOGO_URL = "/custom-static-path/logo.png";

@Test
void shouldLoadStaticResourcesFromCustomPath() throws Exception {
testPlaygroundResources(CUSTOM_CSS_URL, CUSTOM_SCRIPT_URL, CUSTOM_FAVICON_URL, CUSTOM_LOGO_URL);
}
@Test
public void shouldLoadStaticResourcesFromCustomPath() throws Exception {
testPlaygroundResources(
PlaygroundTestHelper.CUSTOM_LOCAL_CSS_URL,
PlaygroundTestHelper.CUSTOM_LOCAL_SCRIPT_URL,
PlaygroundTestHelper.CUSTOM_LOCAL_FAVICON_URL,
PlaygroundTestHelper.CUSTOM_LOCAL_LOGO_URL
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,19 @@
@SpringBootTest(classes = PlaygroundTestConfig.class)
@AutoConfigureMockMvc
@TestPropertySource("classpath:application-playground-custom-title.properties")
class PlaygroundCustomTitleTest {
public class PlaygroundCustomTitleTest {

private static final String CUSTOM_TITLE = "My CustomTest Title";
@Autowired
private MockMvc mockMvc;

@Autowired
private MockMvc mockMvc;
@Test
public void shouldUseTheCustomPageTitle() throws Exception {
final MvcResult mvcResult = mockMvc.perform(get(PlaygroundTestHelper.DEFAULT_PLAYGROUND_ENDPOINT))
.andExpect(status().isOk())
.andExpect(model().attribute(PlaygroundTestHelper.PAGE_TITLE_FIELD_NAME, PlaygroundTestHelper.CUSTOM_TITLE))
.andReturn();

@Test
void shouldUseTheCustomPageTitle() throws Exception {
final MvcResult mvcResult = mockMvc
.perform(get(PlaygroundTestHelper.DEFAULT_PLAYGROUND_ENDPOINT))
.andExpect(status().isOk())
.andExpect(model().attribute(PlaygroundTestHelper.PAGE_TITLE_FIELD_NAME, CUSTOM_TITLE))
.andReturn();

final Document document = Jsoup.parse(mvcResult.getResponse().getContentAsString());
PlaygroundTestHelper.assertTitle(document, CUSTOM_TITLE);
}
final Document document = Jsoup.parse(mvcResult.getResponse().getContentAsString());
PlaygroundTestHelper.assertTitle(document, PlaygroundTestHelper.CUSTOM_TITLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.test.context.ContextConfiguration;

@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = WebFluxAutoConfiguration.class)
@EnableWebSecurity
@ContextConfiguration(classes = {PlaygroundAutoConfiguration.class, ObjectMapper.class,
ThymeleafAutoConfiguration.class})
Expand Down
Loading