未验证 提交 c23f9e6f 编写于 作者: B Bogdan Kobylynskyi 提交者: GitHub

Add example client code using RestAssured, enhance README.md #74 (#78)

上级 7c378fda
......@@ -150,7 +150,19 @@ Sample content of the file:
}
```
### Different configuration for each graphql schema
### Examples
* GraphQL **server** code generation: [example-server](example-server)
* [Plugin configuration in build.gradle](example-server/build.gradle)
* [GraphQL Resolver classes that implement generated interfaces](example-server/src/main/java/io/github/kobylynskyi/product/graphql/resolvers)
* GraphQL **client** code generation: [example-client](example-client)
* [Plugin configuration in build.gradle](example-client/build.gradle)
* [Building GraphQL request and parsing response using Spring RestTemplate](example-client/src/main/java/io/github/kobylynskyi/order/external/ProductServiceGraphQLClient.java)
* [Building GraphQL request and parsing response using RestAssured](example-client/src/test/java/io/github/kobylynskyi/order/service/CreateProductIntegrationTest.java)
### Different configurations for graphql schemas
If you want to have different configuration for different `.graphqls` files (e.g.: different javaPackage, outputDir, etc.), then you will need to create separate gradle tasks for each set of schemas. E.g.:
......@@ -179,12 +191,6 @@ Later on you can call each task separately or together:
2. Access generated classes as normal Kotlin classes.
### Example
* GraphQL server code generation: [example-server](example-server)
* GraphQL client code generation: [example-client](example-client)
### Inspired by
[swagger-codegen](https://github.com/swagger-api/swagger-codegen)
......
......@@ -33,6 +33,10 @@ dependencies {
compileOnly "org.projectlombok:lombok:1.18.8"
annotationProcessor "org.projectlombok:lombok:1.18.8"
testImplementation "io.rest-assured:rest-assured:4.3.0"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.5.1"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.5.1"
}
/**
......
package io.github.kobylynskyi.order.service;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequest;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLResult;
import io.github.kobylynskyi.product.graphql.model.*;
import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.*;
class CreateProductIntegrationTest {
@Disabled("The test is for reference only")
@Test
void createProductUsingRestAssured() {
CreateMutationRequest createProductMutation = new CreateMutationRequest();
createProductMutation.setProductInput(getProductInput());
ProductResponseProjection responseProjection = new ProductResponseProjection()
.id().title().description().sku().price().stockStatus();
GraphQLRequest request = new GraphQLRequest(createProductMutation, responseProjection);
GraphQLResult<Map<String, ProductTO>> result = given()
.baseUri("http://localhost:8081")
.contentType(ContentType.JSON)
.body(request.toString())
.post("/graphql")
.then()
.statusCode(200)
.extract().response().as(new TypeRef<GraphQLResult<Map<String, ProductTO>>>() {});
assertFalse(result.hasErrors());
ProductTO createdProduct = result.getData().get(createProductMutation.getOperationName());
assertNotNull(createdProduct);
assertNotNull(createdProduct.getId());
assertEquals("GT FORCE ALUMINUM ELITE 27.5\" 2019", createdProduct.getTitle());
assertEquals("BI001604", createdProduct.getSku());
assertEquals(StockStatusTO.IN_STOCK, createdProduct.getStockStatus());
assertEquals(BigDecimal.valueOf(2047.5).toString(), createdProduct.getPrice());
}
private static ProductInputTO getProductInput() {
return new ProductInputTO.Builder()
.setTitle("GT FORCE ALUMINUM ELITE 27.5\" 2019")
.setSku("BI001604")
.setStockStatus(StockStatusTO.IN_STOCK)
.setPrice(BigDecimal.valueOf(2047.5).toString())
.build();
}
}
\ No newline at end of file
......@@ -112,7 +112,20 @@ Sample content of the file:
}
```
### Different configuration for each graphql schema
### Examples
* GraphQL **server** code generation: [example-server](example-server)
* [Plugin configuration in pom.xml](example-server/pom.xml)
* [GraphQL Resolver classes that implement generated interfaces](example-server/src/main/java/io/github/kobylynskyi/product/graphql/resolvers)
* GraphQL **client** code generation: [example-client](example-client)
* [Plugin configuration in pom.xml](example-client/pom.xml)
* [Building GraphQL request and parsing response using Spring RestTemplate](example-client/src/main/java/io/github/kobylynskyi/order/external/ProductServiceGraphQLClient.java)
* [Building GraphQL request and parsing response using RestAssured](example-client/src/test/java/io/github/kobylynskyi/order/service/CreateProductIntegrationTest.java)
### Different configurations for graphql schemas
If you want to have different configuration for different `.graphqls` files (e.g.: different javaPackage, outputDir, etc.), then you will need to define separate executions for each set of schemas. E.g.:
```xml
......@@ -140,11 +153,6 @@ If you want to have different configuration for different `.graphqls` files (e.g
</executions>
```
### Example
* GraphQL server code generation: [example-server](example-server)
* GraphQL client code generation: [example-client](example-client)
### Inspired by
[swagger-codegen](https://github.com/swagger-api/swagger-codegen)
......
......@@ -140,6 +140,23 @@
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.1</version>
</dependency>
</dependencies>
</project>
package io.github.kobylynskyi.order.service;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequest;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLResult;
import io.github.kobylynskyi.product.graphql.model.*;
import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.*;
class CreateProductIntegrationTest {
@Disabled("The test is for reference only")
@Test
void createProductUsingRestAssured() {
CreateMutationRequest createProductMutation = new CreateMutationRequest();
createProductMutation.setProductInput(getProductInput());
ProductResponseProjection responseProjection = new ProductResponseProjection()
.id().title().description().sku().price().stockStatus();
GraphQLRequest request = new GraphQLRequest(createProductMutation, responseProjection);
GraphQLResult<Map<String, ProductTO>> result = given()
.baseUri("http://localhost:8081")
.contentType(ContentType.JSON)
.body(request.toString())
.post("/graphql")
.then()
.statusCode(200)
.extract().response().as(new TypeRef<GraphQLResult<Map<String, ProductTO>>>() {});
assertFalse(result.hasErrors());
ProductTO createdProduct = result.getData().get(createProductMutation.getOperationName());
assertNotNull(createdProduct);
assertNotNull(createdProduct.getId());
assertEquals("GT FORCE ALUMINUM ELITE 27.5\" 2019", createdProduct.getTitle());
assertEquals("BI001604", createdProduct.getSku());
assertEquals(StockStatusTO.IN_STOCK, createdProduct.getStockStatus());
assertEquals(BigDecimal.valueOf(2047.5).toString(), createdProduct.getPrice());
}
private static ProductInputTO getProductInput() {
return new ProductInputTO.Builder()
.setTitle("GT FORCE ALUMINUM ELITE 27.5\" 2019")
.setSku("BI001604")
.setStockStatus(StockStatusTO.IN_STOCK)
.setPrice(BigDecimal.valueOf(2047.5).toString())
.build();
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册