Skip to content

Commit 276d890

Browse files
authored
Make sure default argument values are printable (#2017)
1 parent f641ef0 commit 276d890

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {buildSchema} from 'graphql';
2+
3+
import {findFirstChangeByPath} from '../../utils/testing';
4+
import {diff} from '../../src/index';
5+
import {CriticalityLevel} from '../../src/diff/changes/change';
6+
7+
describe('argument', () => {
8+
describe('default value', () => {
9+
test('added', async () => {
10+
const a = buildSchema(/* GraphQL */ `
11+
input Foo {
12+
a: String!
13+
}
14+
15+
type Dummy {
16+
field(foo: Foo): String
17+
}
18+
`);
19+
const b = buildSchema(/* GraphQL */ `
20+
input Foo {
21+
a: String!
22+
}
23+
24+
type Dummy {
25+
field(foo: Foo = {a: "a"}): String
26+
}
27+
`);
28+
29+
const change = findFirstChangeByPath(await diff(a, b), 'Dummy.field.foo');
30+
31+
expect(change.criticality.level).toEqual(CriticalityLevel.Dangerous);
32+
expect(change.type).toEqual('FIELD_ARGUMENT_DEFAULT_CHANGED');
33+
expect(change.message).toEqual('Default value \'[Object: null prototype] { a: \'a\' }\' was added to argument \'foo\' on field \'Dummy.field\'');
34+
});
35+
36+
test('changed', async () => {
37+
const a = buildSchema(/* GraphQL */ `
38+
input Foo {
39+
a: String!
40+
}
41+
42+
type Dummy {
43+
field(foo: Foo = {a: "a"}): String
44+
}
45+
`);
46+
const b = buildSchema(/* GraphQL */ `
47+
input Foo {
48+
a: String!
49+
}
50+
51+
type Dummy {
52+
field(foo: Foo = {a: "new-value"}): String
53+
}
54+
`);
55+
56+
const change = findFirstChangeByPath(await diff(a, b), 'Dummy.field.foo');
57+
58+
expect(change.criticality.level).toEqual(CriticalityLevel.Dangerous);
59+
expect(change.type).toEqual('FIELD_ARGUMENT_DEFAULT_CHANGED');
60+
expect(change.message).toEqual('Default value for argument \'foo\' on field \'Dummy.field\' changed from \'[Object: null prototype] { a: \'a\' }\' to \'[Object: null prototype] { a: \'new-value\' }\'');
61+
});
62+
});
63+
});

packages/core/src/diff/changes/argument.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77

88
import {Change, CriticalityLevel, ChangeType} from './change';
99
import {safeChangeForInputValue} from '../../utils/graphql';
10+
import {safeString} from '../../utils/string';
1011

1112
export function fieldArgumentDescriptionChanged(
1213
type: GraphQLObjectType | GraphQLInterfaceType,
@@ -39,8 +40,8 @@ export function fieldArgumentDefaultChanged(
3940
type: ChangeType.FieldArgumentDefaultChanged,
4041
message:
4142
typeof oldArg.defaultValue === 'undefined'
42-
? `Default value '${newArg.defaultValue}' was added to argument '${newArg.name}' on field '${type.name}.${field.name}'`
43-
: `Default value for argument '${newArg.name}' on field '${type.name}.${field.name}' changed from '${oldArg.defaultValue}' to '${newArg.defaultValue}'`,
43+
? `Default value '${safeString(newArg.defaultValue)}' was added to argument '${newArg.name}' on field '${type.name}.${field.name}'`
44+
: `Default value for argument '${newArg.name}' on field '${type.name}.${field.name}' changed from '${safeString(oldArg.defaultValue)}' to '${safeString(newArg.defaultValue)}'`,
4445
path: [type.name, field.name, oldArg.name].join('.'),
4546
};
4647
}

0 commit comments

Comments
 (0)