A Quick Start Guide to Getting a Java GraphQL API up and Running in no time Using Apifi. What is Apifi and Why Should You Care? is a Java (8+) annotation processing framework which auto generates APIs for based data models. It spans the full API stack; from data access to client side consumption. Apifi is centered around one simple goal: To eliminate the need for generic CRUD related boilerplate compromising on control and customizability. Apifi GraphQL JPA without This means no service beans implementing generic CRUD logic, no manual GraphQL setup, no JpaRepositories, no web controller. All of that’s taken care of. You just focus on what makes your project unique, no boilerplate required. Apifi can instantly turn any JPA data model into a GraphQL API, no boilerplate code required. TLDR; What We’ll be Doing In this article we’ll be demonstrating how to rapidly build a simple eCommerce Java GraphQL API using Apifi and Spring Boot. Before we start I’ll just note that Apifi is built on top of , , and . You’ll probably be able to understand this guide regardless, but I’d strongly recommend being at least somewhat familiar with these tools in order to understand what’s actually going on under the hood. graphql-java SPQR Spring Data JPA Maven Dependencies dev.sanda apifi 0.0.5.4 com.h2database h2 runtime org.projectlombok lombok < > dependencies < > dependency < > groupId </ > groupId < > artifactId </ > artifactId < > version </ > version </ > dependency < > dependency < > groupId </ > groupId < > artifactId </ > artifactId < > scope </ > scope </ > dependency < > dependency < > groupId </ > groupId < > artifactId </ > artifactId </ > dependency </ > dependencies API Requirements and Data Model The eCommerce API needs to be able to: Allow Customer sign up (i.e. Create customer). Get a Customer by ID. Add products to the inventory (i.e. Create products). Allow a Customer to add or remove products to and from their shopping cart (i.e. associate and disassociate existing products with and from a shopping cart). Allow a customer to complete their purchase by checking out (i.e. create a new order, empty the shopping cart, calculate the subtotal, and remove the selected products from the inventory). The data model consists of 4 classes: Customer Product is an interface defined within Apifi containing two methods: Archivable ; ; Boolean getIsArchived () void setIsArchived (Boolean isArchived) This allows us (and Apifi) to mark objects as archived instead of full-on deleting them from the database. This will come in handy when we implement the customer checkout logic and want to keep track of ordered products after removing (i.e. archiving) the sold items. ShoppingCart CustomerOrder Note the @Transient ShoppingCart “fromCart” field in CustomerOrder. Transient fields exist in memory only and are not included in the database schema. We’ll demonstrate why this one is here a bit further down. In order to turn this data model into a fully functional GraphQL API, we just need to add a few annotations. The first annotation which we’ll see as applied to the class is : Customer @WithCrudEndpoints(...) This annotation allows us to define GraphQL queries and mutations with respect to the annotated class. In this case: createCustomer ( input CustomerInput ) Customer customerById ( input Long ) Customer Next, we’ll use the same annotation on the class to define a Product createProducts ( input [ProductInput] ) [Product] mutation: Next up is . We’ll be using the @EntityCollectionApi(...) annotation this time: ShoppingCart This annotation allows us to define GraphQL queries and mutations with respect to the annotated type field. In this case: Collection associateProductsWithShoppingCart ( owner ShoppingCartInput, input [ProductInput] ) [Product] removeProductsFromShoppingCart ( owner ShoppingCartInput, input [ProductInput] ) [Product] The associatePreExistingOnly = true flag denotes that only preexisting objects are allowed as input to the mutation (This makes sense in our case — you can’t add products to a shopping cart if they’re not already in the inventory). If a non existent object were to be passed to the mutation an exception would be thrown, terminating and rolling back the operation. Product associateProductsWithShoppingCart Product associateProductsWithShoppingCart Last but not least, customers need to check out and complete their purchase after filling up their shopping carts. To do this, we’ll once again use the annotation: @WithCrudEndpoints(...) This will create a createCustomerOrder ( input CustomerOrderInput ) CustomerOrder mutation. The only requirement now left to complete is the customer checkout logic. As briefly mentioned above, when a customer checks out by creating a new the following should happen: CustomerOrder, The products in their shopping cart are to be associated with the new object. CustomerOrder The prices of these products are added up to calculate the subtotal. The shopping cart is emptied (all products are disassociated). The products are marked as archived in order to remove them from the available inventory. In order to apply the required logic to the mutation, we create a bean class which we’ll call This class will implement the interface, and override the method. is an interface we can implement in order to inject custom logic before and after CRUD operations. createCustomerOrder CustomerOrderApiHooks. ApiHooks<CustomerOrder> preCreate ApiHooks CustomerOrderApiHooks And that’s it, all that’s left now is to see it in action: If you've made it this far kudos to you and thanks for reading! Also published behind a paywall at: https://medium.com/swlh/instantly-deploy-java-graphql-apis-using-apifi-quick-start-eb2406f6859