diff --git a/packages/core/__tests__/diff/argument.ts b/packages/core/__tests__/diff/argument.ts new file mode 100644 index 0000000000..4468ad718b --- /dev/null +++ b/packages/core/__tests__/diff/argument.ts @@ -0,0 +1,63 @@ +import {buildSchema} from 'graphql'; + +import {findFirstChangeByPath} from '../../utils/testing'; +import {diff} from '../../src/index'; +import {CriticalityLevel} from '../../src/diff/changes/change'; + +describe('argument', () => { + describe('default value', () => { + test('added', async () => { + const a = buildSchema(/* GraphQL */ ` + input Foo { + a: String! + } + + type Dummy { + field(foo: Foo): String + } + `); + const b = buildSchema(/* GraphQL */ ` + input Foo { + a: String! + } + + type Dummy { + field(foo: Foo = {a: "a"}): String + } + `); + + const change = findFirstChangeByPath(await diff(a, b), 'Dummy.field.foo'); + + expect(change.criticality.level).toEqual(CriticalityLevel.Dangerous); + expect(change.type).toEqual('FIELD_ARGUMENT_DEFAULT_CHANGED'); + expect(change.message).toEqual('Default value \'[Object: null prototype] { a: \'a\' }\' was added to argument \'foo\' on field \'Dummy.field\''); + }); + + test('changed', async () => { + const a = buildSchema(/* GraphQL */ ` + input Foo { + a: String! + } + + type Dummy { + field(foo: Foo = {a: "a"}): String + } + `); + const b = buildSchema(/* GraphQL */ ` + input Foo { + a: String! + } + + type Dummy { + field(foo: Foo = {a: "new-value"}): String + } + `); + + const change = findFirstChangeByPath(await diff(a, b), 'Dummy.field.foo'); + + expect(change.criticality.level).toEqual(CriticalityLevel.Dangerous); + expect(change.type).toEqual('FIELD_ARGUMENT_DEFAULT_CHANGED'); + 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\' }\''); + }); + }); +}); diff --git a/packages/core/src/diff/changes/argument.ts b/packages/core/src/diff/changes/argument.ts index c1b26a3f8d..1a28b01632 100644 --- a/packages/core/src/diff/changes/argument.ts +++ b/packages/core/src/diff/changes/argument.ts @@ -7,6 +7,7 @@ import { import {Change, CriticalityLevel, ChangeType} from './change'; import {safeChangeForInputValue} from '../../utils/graphql'; +import {safeString} from '../../utils/string'; export function fieldArgumentDescriptionChanged( type: GraphQLObjectType | GraphQLInterfaceType, @@ -39,8 +40,8 @@ export function fieldArgumentDefaultChanged( type: ChangeType.FieldArgumentDefaultChanged, message: typeof oldArg.defaultValue === 'undefined' - ? `Default value '${newArg.defaultValue}' was added to argument '${newArg.name}' on field '${type.name}.${field.name}'` - : `Default value for argument '${newArg.name}' on field '${type.name}.${field.name}' changed from '${oldArg.defaultValue}' to '${newArg.defaultValue}'`, + ? `Default value '${safeString(newArg.defaultValue)}' was added to argument '${newArg.name}' on field '${type.name}.${field.name}'` + : `Default value for argument '${newArg.name}' on field '${type.name}.${field.name}' changed from '${safeString(oldArg.defaultValue)}' to '${safeString(newArg.defaultValue)}'`, path: [type.name, field.name, oldArg.name].join('.'), }; }