Bael-3893 AWS AppSync with Spring Boot
This commit is contained in:
parent
3746a53285
commit
8b6c667ce0
|
@ -22,12 +22,6 @@
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.amazonaws</groupId>
|
|
||||||
<artifactId>aws-java-sdk-appsync</artifactId>
|
|
||||||
<version>1.11.762</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,14 +1,9 @@
|
||||||
package com.baeldung.awsappsync;
|
package com.baeldung.awsappsync;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
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 org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -17,48 +12,16 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class AwsAppSyncApplicationTests {
|
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
|
@Test
|
||||||
void givenGraphQuery_whenListEvents_thenReturnAllEvents() {
|
void givenGraphQuery_whenListEvents_thenReturnAllEvents() {
|
||||||
|
|
||||||
Map<String, Object> requestBody = new HashMap<>();
|
Map<String, Object> requestBody = new HashMap<>();
|
||||||
requestBody.put("query", "query ListEvents {\n" +
|
requestBody.put("query", "query ListEvents { listEvents { items { id name where when description } } }");
|
||||||
" listEvents {\n" +
|
|
||||||
" items {\n" +
|
|
||||||
" id\n" +
|
|
||||||
" name\n" +
|
|
||||||
" where\n" +
|
|
||||||
" when\n" +
|
|
||||||
" description\n" +
|
|
||||||
" }\n" +
|
|
||||||
" }\n" +
|
|
||||||
"}");
|
|
||||||
requestBody.put("variables", "");
|
requestBody.put("variables", "");
|
||||||
requestBody.put("operationName", "ListEvents");
|
requestBody.put("operationName", "ListEvents");
|
||||||
|
|
||||||
WebClient.ResponseSpec response = requestBodySpec
|
String bodyString = AppSyncClientHelper.getResponseBodySpec(requestBody)
|
||||||
.body(BodyInserters.fromValue(requestBody))
|
.bodyToMono(String.class).block();
|
||||||
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
|
|
||||||
.acceptCharset(StandardCharsets.UTF_8)
|
|
||||||
.retrieve();
|
|
||||||
|
|
||||||
String bodyString = response.bodyToMono(String.class).block();
|
|
||||||
|
|
||||||
assertNotNull(bodyString);
|
assertNotNull(bodyString);
|
||||||
assertTrue(bodyString.contains("My First Event"));
|
assertTrue(bodyString.contains("My First Event"));
|
||||||
|
@ -69,18 +32,8 @@ class AwsAppSyncApplicationTests {
|
||||||
@Test
|
@Test
|
||||||
void givenGraphAdd_whenMutation_thenReturnIdNameDesc() {
|
void givenGraphAdd_whenMutation_thenReturnIdNameDesc() {
|
||||||
|
|
||||||
String queryString = "mutation add {\n" +
|
String queryString = "mutation add { createEvent( name:\"My added GraphQL event\" where:\"Day 2\"" +
|
||||||
" createEvent(\n" +
|
" when:\"Saturday night\" description:\"Studying GraphQL\" ){ id name description } }";
|
||||||
" 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" +
|
|
||||||
"}";
|
|
||||||
|
|
||||||
|
|
||||||
Map<String, Object> requestBody = new HashMap<>();
|
Map<String, Object> requestBody = new HashMap<>();
|
||||||
|
@ -88,11 +41,7 @@ class AwsAppSyncApplicationTests {
|
||||||
requestBody.put("variables", "");
|
requestBody.put("variables", "");
|
||||||
requestBody.put("operationName", "add");
|
requestBody.put("operationName", "add");
|
||||||
|
|
||||||
WebClient.ResponseSpec response = requestBodySpec
|
WebClient.ResponseSpec response = AppSyncClientHelper.getResponseBodySpec(requestBody);
|
||||||
.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 = response.bodyToMono(String.class).block();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue