Skip to content

Instantly share code, notes, and snippets.

@sugimomoto
Created January 21, 2019 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugimomoto/8d194765bfe9fbe01eeed34735be6b45 to your computer and use it in GitHub Desktop.
Save sugimomoto/8d194765bfe9fbe01eeed34735be6b45 to your computer and use it in GitHub Desktop.
package com.company;
import okhttp3.*;
import java.util.List;
public class Main {
public static final String url = "https://sampledbforjavawebapi.herokuapp.com/v1alpha1/graphql";
public static void main(String[] args) throws Exception {
String graphqlRequest = GraphQLSchema.query(query -> query.orders(
orders -> orders
.orderId()
.orderDate()
.customerId()
.employeeId()
.orderDetails(orderDetails -> orderDetails
.productId()
.unitPrice()
.quantity()
.discount()
)
)).toString();
OkHttpClient client = new OkHttpClient();
MediaType MIMEType= MediaType.parse("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create(MIMEType,"{\"query\": \"" + graphqlRequest + "\"}");
Response response = client.newCall(
new Request.Builder().post(requestBody).url(url).build()
).execute();
GraphQLSchema.QueryResponse queryResponse = GraphQLSchema.QueryResponse.fromJson(response.body().string());
List<GraphQLSchema.orders> orderList = queryResponse.getData().getOrders();
for (GraphQLSchema.orders order: orderList) {
System.out.println("-----------------------------------------");
System.out.println("OrderId : " + order.getOrderId());
System.out.println("OrderDate : " + order.getOrderDate());
System.out.println("CustomerId : " + order.getCustomerId());
System.out.println("EmployeedId : " + order.getEmployeeId());
for(GraphQLSchema.order_details order_detail : order.getOrderDetails()){
System.out.println("order_details ProderctId : " + order_detail.getProductId());
System.out.println("order_details Discount : " + order_detail.getDiscount());
System.out.println("order_details Quantity : " + order_detail.getQuantity());
System.out.println("order_details UnitPrice : " + order_detail.getUnitPrice());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment