Skip to content

[APPENG-1215] Add the function to handle directive usage changed type changes #1

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified packages/cli/src/index.ts
100644 → 100755
Empty file.
108 changes: 108 additions & 0 deletions packages/core/__tests__/diff/directive-usage-argument.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { buildSchema } from 'graphql';
import { CriticalityLevel, diff } from '@graphql-inspector/core';
import { findFirstChangeByPath } from '../../utils/testing';
import { expect } from 'vitest';

describe('directive-usage-argument', () => {
describe('enum-value-level directives', () => {
test('directive argument unchanged', async () => {
const a = buildSchema(/* GraphQL */ `
directive @external(argumentOne: String, argumentTwo: String) on ENUM_VALUE
enum enumA {
A @external(argumentOne: "")
B
}
`);

const b = buildSchema(/* GraphQL */ `
directive @external(argumentOne: String, argumentTwo: String) on ENUM_VALUE
enum enumA {
A @external(argumentOne: "")
B
}
`);

const changes = await diff(a, b);

expect(changes.length).toEqual(0);
});

test('added directive argument', async () => {
const a = buildSchema(/* GraphQL */ `
directive @external(argumentOne: String, argumentTwo: String) on ENUM_VALUE
enum enumA {
A @external(argumentOne: "")
B
}
`);

const b = buildSchema(/* GraphQL */ `
directive @external(argumentOne: String, argumentTwo: String) on ENUM_VALUE
enum enumA {
A @external(argumentOne: "", argumentTwo: "")
B
}
`);

const changes = await diff(a, b);
const change = findFirstChangeByPath(changes, 'enumA.A.external');

expect(changes.length).toEqual(1);
expect(change.criticality.level).toEqual(CriticalityLevel.Breaking);
expect(change.criticality.reason).toBeDefined();
expect(change.message).toEqual(`Argument 'argumentTwo' was added to directive 'external' used on enum value 'enumA.A'`)
});

test('removed directive argument', async () => {
const a = buildSchema(/* GraphQL */ `
directive @external(argumentOne: String, argumentTwo: String) on ENUM_VALUE
enum enumA {
A @external(argumentOne: "", argumentTwo: "")
B
}
`);

const b = buildSchema(/* GraphQL */ `
directive @external(argumentOne: String, argumentTwo: String) on ENUM_VALUE
enum enumA {
A @external(argumentOne: "")
B
}
`);

const changes = await diff(a, b);
const change = findFirstChangeByPath(changes, 'enumA.A.external');

expect(changes.length).toEqual(1);
expect(change.criticality.level).toEqual(CriticalityLevel.Breaking);
expect(change.criticality.reason).toBeDefined();
expect(change.message).toEqual(`Argument 'argumentTwo' was removed from directive 'external' used on enum value 'enumA.A'`)
});

test('changed directive argument', async () => {
const a = buildSchema(/* GraphQL */ `
directive @external(argumentOne: String, argumentTwo: String) on ENUM_VALUE
enum enumA {
A @external(argumentOne: "originalValue")
B
}
`);

const b = buildSchema(/* GraphQL */ `
directive @external(argumentOne: String, argumentTwo: String) on ENUM_VALUE
enum enumA {
A @external(argumentOne: "changedValue")
B
}
`);

const changes = await diff(a, b);
const change = findFirstChangeByPath(changes, 'enumA.A.external.argumentOne');

expect(changes.length).toEqual(1);
expect(change.criticality.level).toEqual(CriticalityLevel.Breaking);
expect(change.criticality.reason).toBeDefined();
expect(change.message).toEqual(`Argument 'argumentOne' was changed from 'originalValue' (StringValue) to 'changedValue' (StringValue) in directive 'external' used on enum value 'enumA.A'`)
});
})
})
1 change: 1 addition & 0 deletions packages/core/src/diff/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function changesInArgument(
addChange(fieldArgumentTypeChanged(type, field, oldArg, newArg));
}

// TODO: Heshan -> Introduce a mutual option to this function to detect changes in directive usage
if (oldArg.astNode?.directives && newArg.astNode?.directives) {
compareLists(oldArg.astNode.directives || [], newArg.astNode.directives || [], {
onAdded(directive) {
Expand Down
Loading