Bael-3893 AWS AppSync with Spring Boot

This commit is contained in:
Kyle Doyle 2020-04-17 19:50:36 -04:00
parent 5b59902cb2
commit 6bf0d97d62
3 changed files with 102 additions and 4 deletions

View File

@ -8,17 +8,26 @@
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>aws-app-sync</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>aws-app-sync</name>
<description>Demo project for Spring Boot using AWS App Sync</description>
<description>Spring Boot using AWS App Sync</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-appsync</artifactId>
<version>1.11.762</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@ -35,6 +44,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -1,13 +1,99 @@
package com.baeldung.awsappsync;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class AwsAppSyncApplicationTests {
@Test
void contextLoads() {
static String apiUrl = "https://m4i3b6icrrb7livfbypfspiifi.appsync-api.us-east-2.amazonaws.com";
static String apiKey = "da2-es2s6oj4mzhbxk5yu26ss2ruj4";
static String API_KEY_HEADER = "x-api-key";
static WebClient.RequestBodySpec requestBodySpec;
@BeforeAll
static void setUp() {
requestBodySpec = WebClient
.builder()
.baseUrl(apiUrl)
.defaultHeader(API_KEY_HEADER, apiKey)
.build()
.method(HttpMethod.POST)
.uri("/graphql");
}
@Test
void testGraphQuery() {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("query", "query ListEvents {\n" +
" listEvents {\n" +
" items {\n" +
" id\n" +
" name\n" +
" where\n" +
" when\n" +
" description\n" +
" }\n" +
" }\n" +
"}");
requestBody.put("variables", "");
requestBody.put("operationName", "ListEvents");
WebClient.ResponseSpec response = requestBodySpec
.body(BodyInserters.fromValue(requestBody))
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
.acceptCharset(StandardCharsets.UTF_8)
.retrieve();
String bodyString = response.bodyToMono(String.class).block();
assertTrue(bodyString != null && bodyString.contains("My First Event"));
}
@Test
void testGraphAdd() {
String queryString = "mutation add {\n" +
" createEvent(\n" +
" name:\"My added GraphQL event\"\n" +
" where:\"Day 2\"\n" +
" when:\"Saturday night\"\n" +
" description:\"Studying GraphQL\"\n" +
" ){\n" +
" id\n" +
" name\n" +
" where\n" +
" when\n" +
" description\n" +
" }\n" +
"}";
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("query", queryString);
requestBody.put("variables", "");
requestBody.put("operationName", "add");
WebClient.ResponseSpec response = requestBodySpec
.body(BodyInserters.fromValue(requestBody))
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
.acceptCharset(StandardCharsets.UTF_8)
.retrieve();
String bodyString = response.bodyToMono(String.class).block();
assertTrue(bodyString != null && bodyString.contains("Day 2"));
}
}