diff --git a/package.json b/package.json index 4adc128..9d62706 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "normalize-graphql-query", - "version": "1.2.0", + "version": "1.3.0", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", "types": "types/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 7685d46..1f68fba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,43 +20,49 @@ export const normalizeFieldAccess = (ast: DocumentNode) => { const fieldAliasMap: Map> = new Map() const normalizedAst = visit(ast, { - enter(node) { - if (node.kind === 'Field') { - const fieldName = node.name.value - const alias = node.alias?.value - - const path = [...stack].join('.') - - const accessCountMap = - fieldAccessMap.get([...stack].join('.')) ?? new Map() - const accessCount = accessCountMap.get(fieldName) || 0 - accessCountMap.set(fieldName, accessCount + 1) - fieldAccessMap.set(path, accessCountMap) - - const newAlias = accessCount ? `${fieldName}${accessCount}` : fieldName - - const aliasMap = fieldAliasMap.get(path) ?? new Map() - fieldAliasMap.set(path, aliasMap) - aliasMap.set(newAlias, alias ?? '') - - stack.push(alias ?? fieldName) - - return { - ...node, - alias: accessCount - ? { - kind: Kind.NAME, - value: newAlias, - } - : undefined, + Field: { + enter(node) { + if (node.kind === 'Field') { + const fieldName = node.name.value + const alias = node.alias?.value + + const path = [...stack].join('.') + + const accessCountMap = + fieldAccessMap.get([...stack].join('.')) ?? new Map() + const accessCount = accessCountMap.get(fieldName) || 0 + accessCountMap.set(fieldName, accessCount + 1) + fieldAccessMap.set(path, accessCountMap) + + const newAlias = accessCount + ? `${fieldName}${accessCount}` + : fieldName + + const aliasMap = fieldAliasMap.get(path) ?? new Map() + fieldAliasMap.set(path, aliasMap) + aliasMap.set(newAlias, alias ?? '') + + stack.push(alias ?? fieldName) + + return { + ...node, + alias: accessCount + ? { + kind: Kind.NAME, + value: newAlias, + } + : undefined, + } } - } - }, - - leave(node) { - if (node.kind === 'Field') { + }, + leave() { stack.pop() - } + }, + }, + FragmentDefinition: { + enter() { + return false + }, }, }) diff --git a/test/basic.test.ts b/test/basic.test.ts index f8b73c1..b3f514f 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -169,4 +169,44 @@ describe('normalizeGraphQLQuery', () => { testVariables, ) }) + + test('fixture#3 - fragment', async () => { + const testQuery = `#graphql + query Query( + $a1: String! + $a2: String! + $b: String! + ) { + echo(input: { + a: $a1 + k: { + a: $a2 + b: $b + } + }) { + ...a + } + } + + fragment a on EchoRes { + a + k { + ...k + } + } + + fragment k on EchoRes { + a + b + } + ` + + const testVariables = generateVariables(echoSchema, testQuery) + + await shouldReturnSameValueWithOriginal( + echoServer, + testQuery, + testVariables, + ) + }) }) diff --git a/test/util.ts b/test/util.ts index f51d402..1506ee0 100644 --- a/test/util.ts +++ b/test/util.ts @@ -45,6 +45,11 @@ export const messUpAccessFields = (query: string) => { } return newField }, + FragmentDefinition: { + enter() { + return false + }, + }, }) return print(messedUpAst)