How can I add headers when I am running execute method with MagentoGraphqlClient | Community
Skip to main content
Nikhil-Kumar
Community Advisor
Community Advisor
February 12, 2024
Solved

How can I add headers when I am running execute method with MagentoGraphqlClient

  • February 12, 2024
  • 3 replies
  • 707 views

How can I add headers when I am running execute method with MagentoGraphqlClient. For example below:

 

Below query is an object of MagentoGraphqlClient and  genetateQuery() gives me the Graphql query in String format.
I can not see any option in MagentoGrahpqlClient object to set the headers.

if (query == null) {
query = generateQuery();
}
return client.execute(query);
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SureshDhulipudi

MagentoGraphqlClient class does not have the option of or method to directly to set headers.

 

 

 

/**
* This is a wrapper class for {@link GraphqlClient}. The constructor adapts a {@link Resource} to
* the GraphqlClient class and also looks for the <code>magentoStore</code> property on the resource
* path in order to set the Magento <code>Store</code> HTTP header. This wrapper also sets the custom
* Magento Gson deserializer from {@link QueryDeserializer}.
*/

 

Please check this:

 

 

https://github.com/vladbailescu/aem-core-cif-components/blob/15ed0388d5900ab4dda8c56bcc41f0d2c1b9c4d1/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java#L60

3 replies

SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
February 12, 2024

MagentoGraphqlClient class does not have the option of or method to directly to set headers.

 

 

 

/**
* This is a wrapper class for {@link GraphqlClient}. The constructor adapts a {@link Resource} to
* the GraphqlClient class and also looks for the <code>magentoStore</code> property on the resource
* path in order to set the Magento <code>Store</code> HTTP header. This wrapper also sets the custom
* Magento Gson deserializer from {@link QueryDeserializer}.
*/

 

Please check this:

 

 

https://github.com/vladbailescu/aem-core-cif-components/blob/15ed0388d5900ab4dda8c56bcc41f0d2c1b9c4d1/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/client/MagentoGraphqlClient.java#L60

Raja_Reddy
Community Advisor
Community Advisor
February 13, 2024

Hi @nikhil-kumar 

You can add headers to the MagentoGraphqlClient by creating a HttpHeaders object and setting the desired headers on it. Then, you can pass this HttpHeaders object to the execute method of the MagentoGraphqlClient.

// Create a HttpHeaders object
HttpHeaders headers = new HttpHeaders();

// Set the desired headers on the HttpHeaders object
headers.set("Authorization", "Bearer <your_access_token>");
headers.set("Content-Type", "application/json");

// Create a MagentoGraphqlClient object
MagentoGraphqlClient client = new MagentoGraphqlClient(<magento_graphql_url>);

// Set the HttpHeaders object on the MagentoGraphqlClient object
client.setHeaders(headers);

// Execute the query with the headers
if (query == null) {
    query = generateQuery();
}
return client.execute(query);

In this example, we're setting the Authorization and Content-Type headers on the HttpHeaders object, and then setting the HttpHeaders object on the MagentoGraphqlClient object using the setHeaders method.

kautuk_sahni
Community Manager
Community Manager
February 16, 2024

@nikhil-kumar Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni