Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/broadleaf-framework-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,9 @@
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*-
* #%L
* BroadleafCommerce Framework Web
* %%
* Copyright (C) 2009 - 2026 Broadleaf Commerce
* %%
* Licensed under the Broadleaf Fair Use License Agreement, Version 1.0
* (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt)
* unless the restrictions on use therein are violated and require payment to Broadleaf in which case
* the Broadleaf End User License Agreement (EULA), Version 1.1
* (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt)
* shall apply.
*
* Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License")
* between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license.
* #L%
*/
package org.broadleafcommerce.core.web.graphql;

import org.broadleafcommerce.common.web.BroadleafRequestContext;
import org.broadleafcommerce.core.order.domain.Order;
import org.broadleafcommerce.core.order.service.OrderService;
import org.broadleafcommerce.core.web.order.CartState;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.broadleafcommerce.profile.core.service.CustomerService;
import org.broadleafcommerce.profile.web.core.CustomerState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.graphql.server.WebGraphQlInterceptor;
import org.springframework.graphql.server.WebGraphQlRequest;
import org.springframework.graphql.server.WebGraphQlResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import reactor.core.publisher.Mono;

/**
* Populates the Broadleaf customer and cart context for GraphQL requests. This mirrors the identity
* resolution pattern used by {@code RestApiCustomerStateFilter} so that query/mutation resolvers
* can rely on {@link CustomerState} and {@link CartState}.
*/
@Component
public class GraphQLContextInterceptor implements WebGraphQlInterceptor {

@Autowired
@Qualifier("blCustomerService")
protected CustomerService customerService;

@Autowired
@Qualifier("blOrderService")
protected OrderService orderService;

@Override
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
if (attributes instanceof ServletRequestAttributes servletAttributes) {
HttpServletRequest httpRequest = servletAttributes.getRequest();
HttpServletResponse httpResponse = servletAttributes.getResponse();
populateContext(httpRequest, httpResponse);
}
return chain.next(request);
}

protected void populateContext(HttpServletRequest request, HttpServletResponse response) {
BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
if (brc != null && brc.getWebRequest() == null) {
brc.setWebRequest(new ServletWebRequest(request, response));
}

Customer customer = CustomerState.getCustomer();
if (customer == null) {
customer = customerService.createCustomer();
CustomerState.setCustomer(customer);
}

if (CartState.getCart() == null && customer != null && customer.getId() != null) {
Order cart = orderService.findCartForCustomer(customer);
if (cart != null) {
CartState.setCart(cart);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*-
* #%L
* BroadleafCommerce Framework Web
* %%
* Copyright (C) 2009 - 2026 Broadleaf Commerce
* %%
* Licensed under the Broadleaf Fair Use License Agreement, Version 1.0
* (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt)
* unless the restrictions on use therein are violated and require payment to Broadleaf in which case
* the Broadleaf End User License Agreement (EULA), Version 1.1
* (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt)
* shall apply.
*
* Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License")
* between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license.
* #L%
*/
package org.broadleafcommerce.core.web.graphql;

import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import graphql.schema.DataFetchingEnvironment;
import org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException;
import org.broadleafcommerce.core.order.service.exception.AddToCartException;
import org.broadleafcommerce.core.order.service.exception.IllegalCartOperationException;
import org.broadleafcommerce.core.order.service.exception.RemoveFromCartException;
import org.broadleafcommerce.core.order.service.exception.UpdateCartException;
import org.broadleafcommerce.core.pricing.service.exception.PricingException;
import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter;
import org.springframework.graphql.execution.ErrorType;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class GraphQLExceptionResolver extends DataFetcherExceptionResolverAdapter {

@Override
protected GraphQLError resolveToSingleError(Throwable ex, DataFetchingEnvironment env) {
String code = resolveCode(ex);
Map<String, Object> extensions = new HashMap<>();
extensions.put("code", code);
return GraphqlErrorBuilder.newError(env)
.errorType(ErrorType.INTERNAL_ERROR)
.message(ex.getMessage() != null ? ex.getMessage() : code)
.extensions(extensions)
.build();
}

protected String resolveCode(Throwable ex) {
if (ex instanceof OfferMaxUseExceededException) {
return "OFFER_MAX_USE_EXCEEDED";
}
if (ex instanceof AddToCartException) {
return "ADD_TO_CART_ERROR";
}
if (ex instanceof RemoveFromCartException) {
return "REMOVE_FROM_CART_ERROR";
}
if (ex instanceof UpdateCartException) {
return "UPDATE_CART_ERROR";
}
if (ex instanceof IllegalCartOperationException) {
return "ILLEGAL_CART_OPERATION";
}
if (ex instanceof PricingException) {
return "PRICING_ERROR";
}
return "INTERNAL_ERROR";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*-
* #%L
* BroadleafCommerce Framework Web
* %%
* Copyright (C) 2009 - 2026 Broadleaf Commerce
* %%
* Licensed under the Broadleaf Fair Use License Agreement, Version 1.0
* (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt)
* unless the restrictions on use therein are violated and require payment to Broadleaf in which case
* the Broadleaf End User License Agreement (EULA), Version 1.1
* (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt)
* shall apply.
*
* Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License")
* between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license.
* #L%
*/
package org.broadleafcommerce.core.web.graphql.dto;

import java.util.List;

public class AddToCartInput {

private String productId;
private String skuId;
private int quantity;
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
private List<ItemAttributeInput> itemAttributes;

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getSkuId() {
return skuId;
}

public void setSkuId(String skuId) {
this.skuId = skuId;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public List<ItemAttributeInput> getItemAttributes() {
return itemAttributes;
}

public void setItemAttributes(List<ItemAttributeInput> itemAttributes) {
this.itemAttributes = itemAttributes;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*-
* #%L
* BroadleafCommerce Framework Web
* %%
* Copyright (C) 2009 - 2026 Broadleaf Commerce
* %%
* Licensed under the Broadleaf Fair Use License Agreement, Version 1.0
* (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt)
* unless the restrictions on use therein are violated and require payment to Broadleaf in which case
* the Broadleaf End User License Agreement (EULA), Version 1.1
* (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt)
* shall apply.
*
* Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License")
* between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license.
* #L%
*/
package org.broadleafcommerce.core.web.graphql.dto;

public class ItemAttributeInput {

private String name;
private String value;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-
* #%L
* BroadleafCommerce Framework Web
* %%
* Copyright (C) 2009 - 2026 Broadleaf Commerce
* %%
* Licensed under the Broadleaf Fair Use License Agreement, Version 1.0
* (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt)
* unless the restrictions on use therein are violated and require payment to Broadleaf in which case
* the Broadleaf End User License Agreement (EULA), Version 1.1
* (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt)
* shall apply.
*
* Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License")
* between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license.
* #L%
*/
package org.broadleafcommerce.core.web.graphql.dto;

import org.broadleafcommerce.core.order.domain.Order;

public class PromoCodeResult {

private final Order order;
private final boolean promoAdded;
private final String errorMessage;

public PromoCodeResult(Order order, boolean promoAdded, String errorMessage) {
this.order = order;
this.promoAdded = promoAdded;
this.errorMessage = errorMessage;
}

public Order getOrder() {
return order;
}

public boolean isPromoAdded() {
return promoAdded;
}

public String getErrorMessage() {
return errorMessage;
}

}
Loading