Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CIF-1654 - CIF React Core Components should support GraphQL GET requests #426

Merged
merged 18 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.adobe.cq.commerce.graphql.client.CachingStrategy;
import com.adobe.cq.commerce.graphql.client.CachingStrategy.DataFetchingPolicy;
import com.adobe.cq.commerce.graphql.client.GraphqlClient;
import com.adobe.cq.commerce.graphql.client.GraphqlClientConfiguration;
import com.adobe.cq.commerce.graphql.client.GraphqlRequest;
import com.adobe.cq.commerce.graphql.client.GraphqlResponse;
import com.adobe.cq.commerce.graphql.client.HttpMethod;
Expand Down Expand Up @@ -207,6 +208,15 @@ public GraphqlResponse<Query, Error> execute(String query, HttpMethod httpMethod
return graphqlClient.execute(new GraphqlRequest(query), Query.class, Error.class, options);
}

/**
* Returns the complete configuration of the GraphQL client.
*
* @return GraphQL client configuration.
*/
public GraphqlClientConfiguration getConfiguration() {
return graphqlClient.getConfiguration();
}

private String readFallBackConfiguration(Resource resource, String propertyName) {

InheritanceValueMap properties;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*******************************************************************************
*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
******************************************************************************/

@Version("1.6.0")
package com.adobe.cq.commerce.core.components.client;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;

import com.adobe.cq.commerce.core.components.client.MagentoGraphqlClient;
import com.adobe.cq.commerce.core.components.models.storeconfigexporter.StoreConfigExporter;
import com.adobe.cq.commerce.core.components.services.ComponentsConfiguration;
import com.adobe.cq.commerce.graphql.client.GraphqlClientConfiguration;
import com.adobe.cq.commerce.graphql.client.HttpMethod;
import com.adobe.cq.wcm.launches.utils.LaunchUtils;
import com.day.cq.wcm.api.Page;

Expand All @@ -40,11 +43,16 @@ public class StoreConfigExporterImpl implements StoreConfigExporter {
@Inject
private Page currentPage;

@Inject
private Resource resource;

private String storeView;
private String graphqlEndpoint = "/magento/graphql";
private HttpMethod method = HttpMethod.POST;

@PostConstruct
void initModel() {
// Get configuration from CIF Sling CA config
Resource pageContent = currentPage.getContentResource();
ComponentsConfiguration properties = null;
if (LaunchUtils.isLaunchBasedPath(currentPage.getPath())) {
Expand All @@ -55,6 +63,13 @@ void initModel() {

storeView = properties.get(STORE_CODE_PROPERTY, "default");
graphqlEndpoint = properties.get(GRAPHQL_ENDPOINT_PROPERTY, "/magento/graphql");

// Get configuration from GraphQL client
MagentoGraphqlClient magentoGraphqlClient = MagentoGraphqlClient.create(resource, currentPage);
if (magentoGraphqlClient != null) {
GraphqlClientConfiguration graphqlClientConfiguration = magentoGraphqlClient.getConfiguration();
method = graphqlClientConfiguration.httpMethod();
}
}

@Override
Expand All @@ -66,4 +81,9 @@ public String getStoreView() {
public String getGraphqlEndpoint() {
return graphqlEndpoint;
}

@Override
public String getMethod() {
return method.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public interface StoreConfigExporter {
*/
String getGraphqlEndpoint();

/**
* Returns the HTTP method to be used for GraphQL requests.
*/
String getMethod();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*******************************************************************************
*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
******************************************************************************/

@Version("2.0.0")
package com.adobe.cq.commerce.core.components.models.storeconfigexporter;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.adobe.cq.commerce.core.search.internal.services.SearchFilterServiceImpl;
import com.adobe.cq.commerce.core.search.internal.services.SearchResultsServiceImpl;
import com.adobe.cq.commerce.graphql.client.GraphqlClient;
import com.adobe.cq.commerce.graphql.client.GraphqlClientConfiguration;
import com.adobe.cq.commerce.graphql.client.HttpMethod;
import com.adobe.cq.commerce.graphql.client.impl.GraphqlClientImpl;
import com.adobe.cq.commerce.magento.graphql.gson.QueryDeserializer;
Expand Down Expand Up @@ -153,10 +154,14 @@ public void testPageMetadataModelOnCategoryPageOnLaunch() throws Exception {

private void testPageMetadataModelOnCategoryPage(String pagePath) throws Exception {
HttpClient httpClient = Mockito.mock(HttpClient.class);

GraphqlClientConfiguration graphqlClientConfiguration = mock(GraphqlClientConfiguration.class);
when(graphqlClientConfiguration.httpMethod()).thenReturn(HttpMethod.POST);

graphqlClient = Mockito.spy(new GraphqlClientImpl());
Whitebox.setInternalState(graphqlClient, "gson", QueryDeserializer.getGson());
Whitebox.setInternalState(graphqlClient, "client", httpClient);
Whitebox.setInternalState(graphqlClient, "httpMethod", HttpMethod.POST);
Whitebox.setInternalState(graphqlClient, "configuration", graphqlClientConfiguration);

Utils.setupHttpResponse("graphql/magento-graphql-introspection-result.json", httpClient, HttpStatus.SC_OK, "{__type");
Utils.setupHttpResponse("graphql/magento-graphql-attributes-result.json", httpClient, HttpStatus.SC_OK, "{customAttributeMetadata");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.adobe.cq.commerce.core.components.services.UrlProvider;
import com.adobe.cq.commerce.core.components.testing.Utils;
import com.adobe.cq.commerce.graphql.client.GraphqlClient;
import com.adobe.cq.commerce.graphql.client.GraphqlClientConfiguration;
import com.adobe.cq.commerce.graphql.client.HttpMethod;
import com.adobe.cq.commerce.graphql.client.impl.GraphqlClientImpl;
import com.adobe.cq.commerce.magento.graphql.ProductInterface;
Expand Down Expand Up @@ -131,10 +132,13 @@ private void setUp(String graphqlResponse) throws IOException {
Query rootQuery = Utils.getQueryFromResource(graphqlResponse);
ProductInterface product = rootQuery.getProducts().getItems().get(0);

GraphqlClientConfiguration graphqlClientConfiguration = mock(GraphqlClientConfiguration.class);
when(graphqlClientConfiguration.httpMethod()).thenReturn(HttpMethod.POST);

GraphqlClient graphqlClient = new GraphqlClientImpl();
Whitebox.setInternalState(graphqlClient, "gson", QueryDeserializer.getGson());
Whitebox.setInternalState(graphqlClient, "client", httpClient);
Whitebox.setInternalState(graphqlClient, "httpMethod", HttpMethod.POST);
Whitebox.setInternalState(graphqlClient, "configuration", graphqlClientConfiguration);

Utils.setupHttpResponse(graphqlResponse, httpClient, 200);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.adobe.cq.commerce.core.components.services.UrlProvider;
import com.adobe.cq.commerce.core.components.testing.Utils;
import com.adobe.cq.commerce.graphql.client.GraphqlClient;
import com.adobe.cq.commerce.graphql.client.GraphqlClientConfiguration;
import com.adobe.cq.commerce.graphql.client.HttpMethod;
import com.adobe.cq.commerce.graphql.client.impl.GraphqlClientImpl;
import com.adobe.cq.commerce.magento.graphql.ComplexTextValue;
Expand Down Expand Up @@ -136,10 +137,13 @@ public void setUp() throws Exception {
Query rootQuery = Utils.getQueryFromResource("graphql/magento-graphql-product-result.json");
product = rootQuery.getProducts().getItems().get(0);

GraphqlClientConfiguration graphqlClientConfiguration = mock(GraphqlClientConfiguration.class);
when(graphqlClientConfiguration.httpMethod()).thenReturn(HttpMethod.POST);

graphqlClient = Mockito.spy(new GraphqlClientImpl());
Whitebox.setInternalState(graphqlClient, "gson", QueryDeserializer.getGson());
Whitebox.setInternalState(graphqlClient, "client", httpClient);
Whitebox.setInternalState(graphqlClient, "httpMethod", HttpMethod.POST);
Whitebox.setInternalState(graphqlClient, "configuration", graphqlClientConfiguration);

Utils.setupHttpResponse("graphql/magento-graphql-product-result.json", httpClient, 200);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.adobe.cq.commerce.core.search.models.Sorter;
import com.adobe.cq.commerce.core.search.models.SorterKey;
import com.adobe.cq.commerce.graphql.client.GraphqlClient;
import com.adobe.cq.commerce.graphql.client.GraphqlClientConfiguration;
import com.adobe.cq.commerce.graphql.client.GraphqlRequest;
import com.adobe.cq.commerce.graphql.client.HttpMethod;
import com.adobe.cq.commerce.graphql.client.impl.GraphqlClientImpl;
Expand Down Expand Up @@ -141,10 +142,13 @@ public void setUp() throws Exception {
category = Utils.getQueryFromResource("graphql/magento-graphql-search-result-with-category.json").getCategory();
products = Utils.getQueryFromResource("graphql/magento-graphql-search-result-with-category.json").getProducts();

GraphqlClientConfiguration graphqlClientConfiguration = mock(GraphqlClientConfiguration.class);
when(graphqlClientConfiguration.httpMethod()).thenReturn(HttpMethod.POST);

graphqlClient = Mockito.spy(new GraphqlClientImpl());
Whitebox.setInternalState(graphqlClient, "gson", QueryDeserializer.getGson());
Whitebox.setInternalState(graphqlClient, "client", httpClient);
Whitebox.setInternalState(graphqlClient, "httpMethod", HttpMethod.POST);
Whitebox.setInternalState(graphqlClient, "configuration", graphqlClientConfiguration);

Utils.setupHttpResponse("graphql/magento-graphql-introspection-result.json", httpClient, HttpStatus.SC_OK, "{__type");
Utils.setupHttpResponse("graphql/magento-graphql-attributes-result.json", httpClient, HttpStatus.SC_OK, "{customAttributeMetadata");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.adobe.cq.commerce.core.search.models.Sorter;
import com.adobe.cq.commerce.core.search.models.SorterKey;
import com.adobe.cq.commerce.graphql.client.GraphqlClient;
import com.adobe.cq.commerce.graphql.client.GraphqlClientConfiguration;
import com.adobe.cq.commerce.graphql.client.HttpMethod;
import com.adobe.cq.commerce.graphql.client.impl.GraphqlClientImpl;
import com.adobe.cq.commerce.magento.graphql.gson.QueryDeserializer;
Expand Down Expand Up @@ -124,10 +125,13 @@ public void setUp() throws Exception {
context.currentResource(SEARCHRESULTS);
Resource searchResultsResource = context.resourceResolver().getResource(SEARCHRESULTS);

GraphqlClientConfiguration graphqlClientConfiguration = mock(GraphqlClientConfiguration.class);
when(graphqlClientConfiguration.httpMethod()).thenReturn(HttpMethod.POST);

graphqlClient = Mockito.spy(new GraphqlClientImpl());
Whitebox.setInternalState(graphqlClient, "gson", QueryDeserializer.getGson());
Whitebox.setInternalState(graphqlClient, "client", httpClient);
Whitebox.setInternalState(graphqlClient, "httpMethod", HttpMethod.POST);
Whitebox.setInternalState(graphqlClient, "configuration", graphqlClientConfiguration);

Utils.setupHttpResponse("graphql/magento-graphql-introspection-result.json", httpClient, HttpStatus.SC_OK, "{__type");
Utils.setupHttpResponse("graphql/magento-graphql-attributes-result.json", httpClient, HttpStatus.SC_OK, "{customAttributeMetadata");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import com.adobe.cq.commerce.core.components.client.MockLaunch;
import com.adobe.cq.commerce.core.components.services.ComponentsConfiguration;
import com.adobe.cq.commerce.graphql.client.GraphqlClient;
import com.adobe.cq.commerce.graphql.client.GraphqlClientConfiguration;
import com.adobe.cq.commerce.graphql.client.HttpMethod;
import com.adobe.cq.launches.api.Launch;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.scripting.WCMBindingsConstants;
Expand All @@ -33,10 +36,14 @@
import io.wcm.testing.mock.aem.junit.AemContext;
import io.wcm.testing.mock.aem.junit.AemContextCallback;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class StoreConfigExporterTest {

private static final ValueMap MOCK_CONFIGURATION = new ValueMapDecorator(
ImmutableMap.of("magentoGraphqlEndpoint", "/my/magento/graphql", "magentoStore", "my-magento-store"));
ImmutableMap.of("magentoGraphqlEndpoint", "/my/magento/graphql", "magentoStore", "my-magento-store", "cq:graphqlClient",
"my-graphql-client"));
private static final ComponentsConfiguration MOCK_CONFIGURATION_OBJECT = new ComponentsConfiguration(MOCK_CONFIGURATION);

@Rule
Expand All @@ -47,7 +54,7 @@ private static AemContext createContext(String contentPath) {
(AemContextCallback) context -> {
context.load().json(contentPath, "/content");
context.registerAdapter(Resource.class, ComponentsConfiguration.class,
(Function<Resource, ComponentsConfiguration>) input -> input.getValueMap().get("cq:conf", String.class) != null
(Function<Resource, ComponentsConfiguration>) input -> input.getPath().contains("pageH")
? MOCK_CONFIGURATION_OBJECT
: ComponentsConfiguration.EMPTY);
},
Expand All @@ -56,7 +63,7 @@ private static AemContext createContext(String contentPath) {

@Test
public void testStoreView() {
setupWithPage("/content/pageH");
setupWithPage("/content/pageH", HttpMethod.POST);
StoreConfigExporterImpl storeConfigExporter = context.request().adaptTo(StoreConfigExporterImpl.class);
Assert.assertEquals("my-magento-store", storeConfigExporter.getStoreView());
}
Expand All @@ -65,38 +72,60 @@ public void testStoreView() {
public void testStoreViewOnLaunchPage() {
context.registerAdapter(Resource.class, Launch.class, (Function<Resource, Launch>) resource -> new MockLaunch(resource));

setupWithPage("/content/launches/2020/09/14/mylaunch/content/pageH");
setupWithPage("/content/launches/2020/09/14/mylaunch/content/pageH", HttpMethod.POST);
StoreConfigExporterImpl storeConfigExporter = context.request().adaptTo(StoreConfigExporterImpl.class);
Assert.assertEquals("my-magento-store", storeConfigExporter.getStoreView());
}

@Test
public void testStoreViewDefault() {
setupWithPage("/content/pageD");
setupWithPage("/content/pageD", HttpMethod.POST);

StoreConfigExporterImpl storeConfigExporter = context.request().adaptTo(StoreConfigExporterImpl.class);
Assert.assertEquals("default", storeConfigExporter.getStoreView());
}

@Test
public void testGraphqlEndpoint() {
setupWithPage("/content/pageH");
setupWithPage("/content/pageH", HttpMethod.POST);
StoreConfigExporterImpl storeConfigExporter = context.request().adaptTo(StoreConfigExporterImpl.class);

Assert.assertEquals("/my/magento/graphql", storeConfigExporter.getGraphqlEndpoint());
}

@Test
public void testGraphqlEndpointDefault() {
setupWithPage("/content/pageD");
setupWithPage("/content/pageD", HttpMethod.POST);
StoreConfigExporterImpl storeConfigExporter = context.request().adaptTo(StoreConfigExporterImpl.class);
Assert.assertEquals("/magento/graphql", storeConfigExporter.getGraphqlEndpoint());
}

private void setupWithPage(String pagetPath) {
Page page = context.pageManager().getPage(pagetPath);
@Test
public void testGraphqlMethodGet() {
setupWithPage("/content/pageH", HttpMethod.GET);
StoreConfigExporterImpl storeConfigExporter = context.request().adaptTo(StoreConfigExporterImpl.class);
Assert.assertEquals("GET", storeConfigExporter.getMethod());
}

@Test
public void testGraphqlMethodPost() {
setupWithPage("/content/pageH", HttpMethod.POST);
StoreConfigExporterImpl storeConfigExporter = context.request().adaptTo(StoreConfigExporterImpl.class);
Assert.assertEquals("POST", storeConfigExporter.getMethod());
}

private void setupWithPage(String pagePath, HttpMethod method) {
Page page = context.pageManager().getPage(pagePath);
SlingBindings slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());
slingBindings.put(WCMBindingsConstants.NAME_CURRENT_PAGE, page);
slingBindings.setResource(page.getContentResource());

GraphqlClientConfiguration graphqlClientConfiguration = mock(GraphqlClientConfiguration.class);
when(graphqlClientConfiguration.httpMethod()).thenReturn(method);
GraphqlClient graphqlClient = mock(GraphqlClient.class);
when(graphqlClient.getConfiguration()).thenReturn(graphqlClientConfiguration);

context.registerAdapter(Resource.class, GraphqlClient.class, (Function<Resource, GraphqlClient>) input -> input.getValueMap().get(
"cq:graphqlClient", String.class) != null ? graphqlClient : null);
}
}
Loading