Skip to content

Commit

Permalink
fix: do not handle fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
cnwangjie committed Jul 6, 2023
1 parent c58bddc commit f36bf01
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
76 changes: 41 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,49 @@ export const normalizeFieldAccess = (ast: DocumentNode) => {
const fieldAliasMap: Map<string, Map<string, string>> = 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
},
},
})

Expand Down
40 changes: 40 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
})
})
5 changes: 5 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const messUpAccessFields = (query: string) => {
}
return newField
},
FragmentDefinition: {
enter() {
return false
},
},
})

return print(messedUpAst)
Expand Down

0 comments on commit f36bf01

Please sign in to comment.