Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Community Advisor

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);
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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/15ed0388d5900ab4dda8c56bcc41f0d2c1b9c4d...

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

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/15ed0388d5900ab4dda8c56bcc41f0d2c1b9c4d...

Avatar

Community Advisor

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.



Avatar

Administrator

@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