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

GraphQLError: Fixed originalError.extensions overriding extensions argument to constructor #3343

Merged
merged 2 commits into from Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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