Skip to content

Commit

Permalink
Make sure default argument values are printable (#2017)
Browse files Browse the repository at this point in the history
  • Loading branch information
ertrzyiks authored Sep 29, 2021
1 parent f641ef0 commit 276d890
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
63 changes: 63 additions & 0 deletions packages/core/__tests__/diff/argument.ts
Original file line number Diff line number Diff line change
@@ -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\' }\'');
});
});
});
5 changes: 3 additions & 2 deletions packages/core/src/diff/changes/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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('.'),
};
}
Expand Down

0 comments on commit 276d890

Please sign in to comment.