Bael-3893 AWS AppSync with Spring Boot

This commit is contained in:
Kyle Doyle 2020-04-22 21:10:52 -04:00
parent 3746a53285
commit 8b6c667ce0
3 changed files with 38 additions and 63 deletions

View File

@ -22,12 +22,6 @@
<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>

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-es2s6oj4mzhbxk5yu26ss2ruj4";
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

@ -1,14 +1,9 @@
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;
@ -17,48 +12,16 @@ import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class AwsAppSyncApplicationTests {
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 givenGraphQuery_whenListEvents_thenReturnAllEvents() {
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("query", "query ListEvents { listEvents { items { id name where when description } } }");
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();
String bodyString = AppSyncClientHelper.getResponseBodySpec(requestBody)
.bodyToMono(String.class).block();
assertNotNull(bodyString);
assertTrue(bodyString.contains("My First Event"));
@ -69,18 +32,8 @@ class AwsAppSyncApplicationTests {
@Test
void givenGraphAdd_whenMutation_thenReturnIdNameDesc() {
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" +
" description\n" +
" }\n" +
"}";
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<>();
@ -88,11 +41,7 @@ class AwsAppSyncApplicationTests {
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();
WebClient.ResponseSpec response = AppSyncClientHelper.getResponseBodySpec(requestBody);
String bodyString = response.bodyToMono(String.class).block();