GraphQL
- Anand Nerurkar
- Jun 20, 2024
- 2 min read
Updated: Oct 4, 2024
GraphQL is a query language for your API.
Say API return 100 fields in a response , we have various client and each client need different fileds in their response, then GraphQL describe those need and return those only fileds as a response.
Client just need to describe which field they need, GraphQL will take care of that.
need to create schema file for gaphql as below in graphql folder.
schema.graphql
==
type Product{
id: ID,
name:String
category:String
price:Float
stock:Int
}
type Query{
getProducts:[Product]
getProductsByCategory(category:String):[Product]
}
type Mutation{
updateStock(id:ID,stock:Int):Product
receiveNewShipment(id:ID,quantity:Int):Product
}
#Enabling GraphQL playground
spring.graphql.graphiql.enabled=true
spring.graphql.graphiql.path=/graphiql
Replace RestController with only Controller , GetMapping with QuryMapping, PostMapping with MutationMapping
@Controller
public class ProductController {
@Autowired
private ProductService service;
@QueryMapping/fetch/retireve operation
public List<Product> getProducts() {
return service.getProducts();
}
@QueryMapping//fetch/retireve operation
public List<Product> getProductsByCategory(@Argument String category) {
return service.getProductsByCategory(category);
}
@MutationMapping//for update/delete operation
public Product updateStock(@Argument int id, @Argument int stock) {
return service.updateStock(id, stock);
}
@MutationMapping//for update/delete operation
@SubscriptionMapping
public Product receiveNewShipment(@Argument int id, @Argument int quantity) {
return service.receiveNewShipment(id, quantity);
}
}
How to consume GraphQL service with graphql client
==
As a part of configuration, get graphqlclient bean as below
@Configuration
public class GraphQLClientConfig {
@Bean
public HttpGraphQlClient graphQlClient() {
WebClient webClient = WebClient.builder()
.baseUrl("http://localhost:9191/graphql")
.build();
return HttpGraphQlClient.builder(webClient).build();
}
}
let us write a client class
==
@Component
public class InventoryClient {
@Autowired
private HttpGraphQlClient graphQlClient;
public List<Item> viewProducts() {
String graphQLQuery = "query GetProducts {\n" +
" getProducts {\n" +
" name\n" +
" price\n" +
" }\n" +
"}";
return graphQlClient.document(graphQLQuery)
.retrieve("getProducts")
.toEntityList(Item.class).block();
}
public List<Item> viewProductsByCategory(String category) {
String graphQLQuery = String.format("query GetProductsByCategory {\n" +
" getProductsByCategory(category: \"%s\") {\n" +
" name\n" +
" category\n" +
" price\n" +
" stock\n" +
" }\n" +
"}\n", category);
return graphQlClient.document(graphQLQuery)
.retrieve("getProductsByCategory")
.toEntityList(Item.class).block();
}
public Item receiveNewShipment(ItemRequestDTO itemRequest) {
String graphQlQuery = String.format("mutation ReceiveNewShipment {\n" +
" receiveNewShipment(id: \"%s\", quantity: %d) {\n" +
" name\n" +
" price\n" +
" stock\n" +
" }\n" +
"}\n", itemRequest.getId(), itemRequest.getQty());
return graphQlClient.document(graphQlQuery)
.retrieve("receiveNewShipment")
.toEntity(Item.class).block();
}
}
Comments