Skip to content

Commit 26cc504

Browse files
committed
fix: graphql operations handling
1 parent b46ab95 commit 26cc504

File tree

24 files changed

+90
-93
lines changed

24 files changed

+90
-93
lines changed

src/apitypes/graphql/graphql.changes.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { isEmpty, removeFirstSlash, slugify, takeIf } from '../../utils'
17+
import { isEmpty, slugify, takeIf } from '../../utils'
1818
import { apiDiff, Diff, DIFF_META_KEY, DIFFS_AGGREGATED_META_KEY } from '@netcracker/qubership-apihub-api-diff'
1919
import { NORMALIZE_OPTIONS, ORIGINS_SYMBOL } from '../../consts'
2020
import { GraphApiOperation, GraphApiSchema } from '@netcracker/qubership-apihub-graphapi'
@@ -45,6 +45,8 @@ export const compareDocuments = async (
4545
const prevDocSchema = prevFile && buildSchema(await prevFile.text(), { noLocation: true })
4646
const currDocSchema = currFile && buildSchema(await currFile.text(), { noLocation: true })
4747

48+
const collectOnlyChangedOperations = Boolean(prevDoc && currDoc)
49+
4850
let prevDocData = prevDocSchema && (await buildGraphQLDocument({
4951
...prevDoc,
5052
source: prevFile,
@@ -81,8 +83,6 @@ export const compareDocuments = async (
8183
return { operationChanges: [], tags: new Set() }
8284
}
8385

84-
let operationDiffs: Diff[] = []
85-
8686
const { currentGroup, previousGroup } = ctx
8787

8888
const tags = new Set<string>()
@@ -97,17 +97,19 @@ export const compareDocuments = async (
9797
const methodData = operationsByType[operationKey]
9898

9999
const { current, previous } = operationsMap[operationId] ?? {}
100-
if (current && previous) {
100+
if (!current && !previous) {
101+
throw new Error(`Can't find the ${operationId} operation from documents pair ${prevDoc?.fileId} and ${currDoc?.fileId}`)
102+
}
103+
const operationChanged = Boolean(current && previous)
104+
const operationAddedOrRemoved = !operationChanged
105+
106+
let operationDiffs: Diff[] = []
107+
if (operationChanged && collectOnlyChangedOperations) {
101108
operationDiffs = [...(methodData as WithAggregatedDiffs<GraphApiOperation>)[DIFFS_AGGREGATED_META_KEY]]
102-
} else if (current || previous) {
103-
for (const type of GRAPHQL_TYPE_KEYS) {
104-
const operationsByType = (merged[type] as WithDiffMetaRecord<Record<string, GraphApiOperation>>)?.[DIFF_META_KEY]
105-
if (!operationsByType) { continue }
106-
operationDiffs.push(...Object.values(operationsByType))
107-
}
108-
if (isEmpty(operationDiffs)) {
109-
throw new Error('should not happen')
110-
}
109+
}
110+
if (operationAddedOrRemoved && !collectOnlyChangedOperations) {
111+
const operationAddedOrRemovedDiff = (merged[type] as WithDiffMetaRecord<Record<string, GraphApiOperation>>)[DIFF_META_KEY]?.[operationKey]
112+
operationAddedOrRemovedDiff && operationDiffs.push(operationAddedOrRemovedDiff)
111113
}
112114

113115
if (isEmpty(operationDiffs)) {

src/apitypes/rest/rest.changes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export const compareDocuments = async (
143143
const prevNormalizedOperationId = slugify(`${normalizePath(previousBasePath + path)}-${inferredMethod}`, [], IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER)
144144
const currNormalizedOperationId = slugify(`${normalizePath(currentBasePath + path)}-${inferredMethod}`, [], IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER)
145145

146-
const { current, previous } = operationsMap[prevNormalizedOperationId] ?? operationsMap[currNormalizedOperationId] /*?? operationsMap[operationId]*/ ?? {}
146+
const { current, previous } = operationsMap[prevNormalizedOperationId] ?? operationsMap[currNormalizedOperationId] ?? {}
147147
if (!current && !previous) {
148148
const missingOperations = prevNormalizedOperationId === currNormalizedOperationId ? `the ${prevNormalizedOperationId} operation` : `the ${prevNormalizedOperationId} and ${currNormalizedOperationId} operations`
149149
throw new Error(`Can't find ${missingOperations} from documents pair ${prevDoc?.fileId} and ${currDoc?.fileId}`)

test/changes.test.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,20 @@ describe('Changes test', () => {
7373
})
7474

7575
describe('Added/removed/changed operations handling', () => {
76-
test('Add method', async () => {
77-
const result = await buildChangelogPackage('changelog/add-method')
76+
test('Add operation', async () => {
77+
const result = await buildChangelogPackage('changelog/add-operation')
7878
expect(result).toEqual(changesSummaryMatcher({ [NON_BREAKING_CHANGE_TYPE]: 1 }))
7979
expect(result).toEqual(numberOfImpactedOperationsMatcher({ [NON_BREAKING_CHANGE_TYPE]: 1 }))
8080
})
8181

82-
test('Remove method', async () => {
83-
const result = await buildChangelogPackage('changelog/remove-method')
82+
test('Remove operation', async () => {
83+
const result = await buildChangelogPackage('changelog/remove-operation')
8484
expect(result).toEqual(changesSummaryMatcher({ [BREAKING_CHANGE_TYPE]: 1 }))
8585
expect(result).toEqual(numberOfImpactedOperationsMatcher({ [BREAKING_CHANGE_TYPE]: 1 }))
8686
})
8787

88-
test('Change method content', async () => {
89-
const result = await buildChangelogPackage('changelog/change-inside-method')
88+
test('Change operation content', async () => {
89+
const result = await buildChangelogPackage('changelog/change-inside-operation')
9090

9191
expect(result).toEqual(changesSummaryMatcher({
9292
[BREAKING_CHANGE_TYPE]: 1,
@@ -226,8 +226,8 @@ describe('Changes test', () => {
226226
})
227227
})
228228

229-
test('Operation changes fields are correct (REST)', async () => {
230-
const result = await buildChangelogPackage('changelog/operation-changes-fields-rest')
229+
test('Operation changes fields are correct', async () => {
230+
const result = await buildChangelogPackage('changelog/operation-changes-fields')
231231

232232
expect(result).toEqual(operationChangesMatcher([
233233
expect.objectContaining({
@@ -250,30 +250,6 @@ describe('Changes test', () => {
250250
]))
251251
})
252252

253-
test('Operation changes fields are correct (GQL)', async () => {
254-
const result = await buildChangelogPackage('changelog/operation-changes-fields-gql', [{ fileId: 'before.graphql' }], [{ fileId: 'after.graphql' }])
255-
256-
expect(result).toEqual(operationChangesMatcher([
257-
expect.objectContaining({
258-
previousOperationId: 'query-fruits',
259-
operationId: 'query-fruits',
260-
previousMetadata: {
261-
'title': 'Fruits',
262-
'tags': ['queries'],
263-
'method': 'fruits',
264-
'type': 'query',
265-
},
266-
metadata: {
267-
'title': 'Fruits',
268-
'tags': ['queries'],
269-
'method': 'fruits',
270-
'type': 'query',
271-
},
272-
// rest of the fields are covered by dedicated tests
273-
}),
274-
]))
275-
})
276-
277253
test('Tags are not duplicated', async () => {
278254
const result = await buildChangelogPackage('changelog/tags')
279255

test/graphql-changes.test.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,56 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { buildChangelogPackage, changesSummaryMatcher, numberOfImpactedOperationsMatcher } from './helpers'
17+
import {
18+
buildGqlChangelogPackage,
19+
changesSummaryMatcher,
20+
numberOfImpactedOperationsMatcher,
21+
operationChangesMatcher,
22+
} from './helpers'
1823
import { BREAKING_CHANGE_TYPE, GRAPHQL_API_TYPE, NON_BREAKING_CHANGE_TYPE } from '../src'
1924

2025
describe('Graphql changes test', () => {
21-
test('add mutation', async () => {
22-
const result = await buildChangelogPackage(
23-
'graphql-changes-test/add-mutation', [{ fileId: 'before.gql' }], [{ fileId: 'after.gql' }],
24-
)
25-
expect(result).toEqual(changesSummaryMatcher({ [NON_BREAKING_CHANGE_TYPE]: 1 }, GRAPHQL_API_TYPE))
26-
expect(result).toEqual(numberOfImpactedOperationsMatcher({ [NON_BREAKING_CHANGE_TYPE]: 1 }, GRAPHQL_API_TYPE))
26+
describe('Added/removed/changed operations handling', () => {
27+
test('Add operation', async () => {
28+
const result = await buildGqlChangelogPackage('graphql-changes/add-operation')
29+
expect(result).toEqual(changesSummaryMatcher({ [NON_BREAKING_CHANGE_TYPE]: 1 }, GRAPHQL_API_TYPE))
30+
expect(result).toEqual(numberOfImpactedOperationsMatcher({ [NON_BREAKING_CHANGE_TYPE]: 1 }, GRAPHQL_API_TYPE))
31+
})
32+
33+
test('Remove operation', async () => {
34+
const result = await buildGqlChangelogPackage('graphql-changes/remove-operation')
35+
expect(result).toEqual(changesSummaryMatcher({ [BREAKING_CHANGE_TYPE]: 1 }, GRAPHQL_API_TYPE))
36+
expect(result).toEqual(numberOfImpactedOperationsMatcher({ [BREAKING_CHANGE_TYPE]: 1 }, GRAPHQL_API_TYPE))
37+
})
38+
39+
test('Change operation content', async () => {
40+
const result = await buildGqlChangelogPackage('graphql-changes/change-inside-operation')
41+
expect(result).toEqual(changesSummaryMatcher({ [BREAKING_CHANGE_TYPE]: 1 }, GRAPHQL_API_TYPE))
42+
expect(result).toEqual(numberOfImpactedOperationsMatcher({ [BREAKING_CHANGE_TYPE]: 1 }, GRAPHQL_API_TYPE))
43+
})
2744
})
2845

29-
test('remove mutation', async () => {
30-
const result = await buildChangelogPackage(
31-
'graphql-changes-test/remove-mutation', [{ fileId: 'before.gql' }], [{ fileId: 'after.gql' }],
32-
)
33-
expect(result).toEqual(changesSummaryMatcher({ [BREAKING_CHANGE_TYPE]: 1 }, GRAPHQL_API_TYPE))
34-
expect(result).toEqual(numberOfImpactedOperationsMatcher({ [BREAKING_CHANGE_TYPE]: 1 }, GRAPHQL_API_TYPE))
46+
test('Operation changes fields are correct', async () => {
47+
const result = await buildGqlChangelogPackage('graphql-changes/operation-changes-fields')
48+
49+
expect(result).toEqual(operationChangesMatcher([
50+
expect.objectContaining({
51+
previousOperationId: 'query-fruits',
52+
operationId: 'query-fruits',
53+
previousMetadata: {
54+
'title': 'Fruits',
55+
'tags': ['queries'],
56+
'method': 'fruits',
57+
'type': 'query',
58+
},
59+
metadata: {
60+
'title': 'Fruits',
61+
'tags': ['queries'],
62+
'method': 'fruits',
63+
'type': 'query',
64+
},
65+
// rest of the fields are covered by dedicated tests
66+
}),
67+
]))
3568
})
3669
})

0 commit comments

Comments
 (0)