Skip to content

Commit

Permalink
GraphQLError: Fixed a regression for extensions before the refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
klippx committed Oct 28, 2021
1 parent 7dc29fd commit 463f075
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/error/GraphQLError.js
Expand Up @@ -102,11 +102,12 @@ export class GraphQLError extends Error {

this.path = path ?? undefined;

this.extensions = extensions ?? {};

const originalExtensions = originalError?.extensions;
if (isObjectLike(originalExtensions)) {

if (extensions == null && isObjectLike(originalExtensions)) {
this.extensions = { ...originalExtensions };
} else {
this.extensions = extensions ?? {};
}

// By being enumerable, JSON.stringify will include bellow properties in the resulting output.
Expand Down
60 changes: 60 additions & 0 deletions src/error/__tests__/GraphQLError-test.js
Expand Up @@ -164,6 +164,66 @@ describe('GraphQLError', () => {
}
`);
});

it('uses the provided extensions when original extensions are undefined', () => {
const original = new Error('original');
const graphqlError = new GraphQLError(
'msg',
null,
null,
null,
null,
original,
{
someKey: 'someValue',
},
);

const e = new GraphQLError(
graphqlError.message,
null,
null,
null,
null,
graphqlError,
{ correlationId: '123-echo-tango-delta' },
);

expect(JSON.stringify(e.extensions, null, 2) + '\n').to.equal(dedent`
{
"correlationId": "123-echo-tango-delta"
}
`);
});

it('uses the provided extensions when original extensions are empty object', () => {
const original = new Error('original');
const graphqlError = new GraphQLError(
'msg',
null,
null,
null,
null,
original,
{},
);

const e = new GraphQLError(
graphqlError.message,
null,
null,
null,
null,
graphqlError,
{ correlationId: '123-echo-tango-delta' },
);

expect(JSON.stringify(e.extensions, null, 2) + '\n').to.equal(dedent`
{
"correlationId": "123-echo-tango-delta"
}
`);
});
});

describe('printError', () => {
Expand Down

0 comments on commit 463f075

Please sign in to comment.