Skip to content

Commit 7cb4319

Browse files
authored
Log errors returned by successful GraphQL calls (#15)
* Add base template * Render price, description, options and image * Update E2E tests * Update unit tests * Log errors returned by successful GraphQL calls
1 parent 8f88c76 commit 7cb4319

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

actions/utils.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ async function getConfig(context) {
231231
* @returns {Promise<object>} GraphQL response as parsed object.
232232
*/
233233
async function requestSaaS(query, operationName, variables, context, configOverrides = {}) {
234-
const { storeUrl } = context;
234+
const { storeUrl, logger } = context;
235235
const config = {
236236
... (await getConfig(context)),
237237
...configOverrides
@@ -249,7 +249,8 @@ async function requestSaaS(query, operationName, variables, context, configOverr
249249
'Magento-Is-Preview': true,
250250
};
251251
const method = 'POST';
252-
return request(
252+
253+
const response = await request(
253254
`${operationName}(${JSON.stringify(variables)})`,
254255
config['commerce-endpoint'],
255256
{
@@ -262,6 +263,15 @@ async function requestSaaS(query, operationName, variables, context, configOverr
262263
})
263264
}
264265
);
266+
267+
// Log GraphQL errors
268+
if (response?.errors) {
269+
for (const error of response.errors) {
270+
logger.error(`Request '${operationName}' returned GraphQL error`, error);
271+
}
272+
}
273+
274+
return response;
265275
}
266276

267277

test/utils.test.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('request', () => {
132132
const config = await getConfig(context);
133133
expect(config).toEqual({ testKey: 'testValue' });
134134
});
135-
135+
136136
test('requestSaaS', async () => {
137137
let requestHeaders;
138138
server.use(http.post('https://commerce-endpoint.com', async ({ request }) => {
@@ -170,7 +170,46 @@ describe('request', () => {
170170
expect(requestHeaders.get('x-api-key')).toBe('api-key');
171171
expect(requestHeaders.get('Magento-Is-Preview')).toBe('true');
172172
});
173-
173+
174+
test('requestSaaS with errors in GraphQL response', async () => {
175+
const graphqlError = {
176+
"message": "The field at path '/_entities' was declared as a non null type, but the code involved in retrieving data has wrongly returned a null value. The graphql specification requires that the parent field be set to null, or if that is non nullable that it bubble up null to its parent and so on. The non-nullable type is '[_Entity]' within parent type 'Query'",
177+
"path": ["categories", "@"],
178+
};
179+
server.use(http.post('https://commerce-endpoint.com', async () => {
180+
return HttpResponse.json({
181+
data: {
182+
result: 'success'
183+
},
184+
errors: [
185+
graphqlError
186+
],
187+
});
188+
}));
189+
190+
const context = {
191+
storeUrl: 'https://store.com',
192+
config: {
193+
'commerce-endpoint': 'https://commerce-endpoint.com',
194+
'commerce-customer-group': 'customer-group',
195+
'commerce-environment-id': 'environment-id',
196+
'commerce-store-code': 'store-code',
197+
'commerce-store-view-code': 'store-view-code',
198+
'commerce-website-code': 'website-code',
199+
'commerce-x-api-key': 'api-key'
200+
},
201+
logger: { error: jest.fn() }
202+
};
203+
204+
const query = 'query { test }';
205+
const operationName = 'TestOperation';
206+
const variables = { var1: 'value1' };
207+
208+
const response = await requestSaaS(query, operationName, variables, context);
209+
expect(response).toEqual({ data: { result: 'success' }, errors: [graphqlError] });
210+
expect(context.logger.error).toHaveBeenCalledWith(`Request 'TestOperation' returned GraphQL error`, graphqlError);
211+
});
212+
174213
test('requestSpreadsheet', async () => {
175214
server.use(http.get('https://content.com/test/config.json', async () => {
176215
return HttpResponse.json({ data: [{ key: 'testKey', value: 'testValue' }] });
@@ -201,7 +240,7 @@ describe('request', () => {
201240
const response = await request('testRequest', 'https://example.com/success', {});
202241
expect(response).toEqual({ data: 'success' });
203242
});
204-
243+
205244
test('error request', async () => {
206245
server.use(http.get('https://example.com/not-found', async () => {
207246
return new HttpResponse(null, { status: 404, statusText: 'Not Found' });

0 commit comments

Comments
 (0)