Merge pull request #9128 from smokeyrobot/bael-3893

Bael 3893
This commit is contained in:
Greg 2020-05-12 13:13:06 -04:00 committed by GitHub
commit a3ec140575
5 changed files with 174 additions and 0 deletions

56
aws-app-sync/pom.xml Normal file
View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<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>Spring Boot using AWS App Sync</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,32 @@
package com.baeldung.awsappsync;
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.Map;
public class AppSyncClientHelper {
static String apiUrl = "https://m4i3b6icrrb7livfbypfspiifi.appsync-api.us-east-2.amazonaws.com";
static String apiKey = "da2-bm4rpatkkrc5jfyhvvq7itjeke";
static String API_KEY_HEADER = "x-api-key";
public static WebClient.ResponseSpec getResponseBodySpec(Map<String, Object> requestBody) {
return WebClient
.builder()
.baseUrl(apiUrl)
.defaultHeader(API_KEY_HEADER, apiKey)
.defaultHeader("Content-Type", "application/json")
.build()
.method(HttpMethod.POST)
.uri("/graphql")
.body(BodyInserters.fromValue(requestBody))
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
.acceptCharset(StandardCharsets.UTF_8)
.retrieve();
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.awsappsync;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AwsAppSyncApplication {
public static void main(String[] args) {
SpringApplication.run(AwsAppSyncApplication.class, args);
}
}

View File

@ -0,0 +1,72 @@
package com.baeldung.awsappsync;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.reactive.function.client.WebClient;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class AwsAppSyncApplicationTests {
@Test
void givenGraphQuery_whenListEvents_thenReturnAllEvents() {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("query", "query ListEvents {"
+ " listEvents {"
+ " items {"
+ " id"
+ " name"
+ " where"
+ " when"
+ " description"
+ " }"
+ " }"
+ "}");
requestBody.put("variables", "");
requestBody.put("operationName", "ListEvents");
String bodyString = AppSyncClientHelper.getResponseBodySpec(requestBody)
.bodyToMono(String.class).block();
assertNotNull(bodyString);
assertTrue(bodyString.contains("My First Event"));
assertTrue(bodyString.contains("where"));
assertTrue(bodyString.contains("when"));
}
@Test
void givenGraphAdd_whenMutation_thenReturnIdNameDesc() {
String queryString = "mutation add {"
+ " createEvent("
+ " name:\"My added GraphQL event\""
+ " where:\"Day 2\""
+ " when:\"Saturday night\""
+ " description:\"Studying GraphQL\""
+ " ){"
+ " id"
+ " name"
+ " description"
+ " }"
+ "}";
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("query", queryString);
requestBody.put("variables", "");
requestBody.put("operationName", "add");
WebClient.ResponseSpec response = AppSyncClientHelper.getResponseBodySpec(requestBody);
String bodyString = response.bodyToMono(String.class).block();
assertNotNull(bodyString);
assertTrue(bodyString.contains("My added GraphQL event"));
assertFalse(bodyString.contains("where"));
assertFalse(bodyString.contains("when"));
}
}

View File

@ -896,6 +896,7 @@
<module>atomix</module>
<module>aws</module>
<module>aws-app-sync</module>
<module>aws-lambda</module>
<module>aws-reactive</module>