Skip to content

Commit

Permalink
fix: fill operation name when fragment before operation
Browse files Browse the repository at this point in the history
  • Loading branch information
cnwangjie committed Jul 7, 2023
1 parent f36bf01 commit ee4cc76
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 62 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.3.0",
"version": "1.4.0",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "types/index.d.ts",
Expand Down
24 changes: 15 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,41 +141,47 @@ export const normalizeVariableName = (
}

export const fillQueryOperationName = (ast: DocumentNode) => {
let hasName = false
let operationName = ''
let inOperation = false
const operationNames: (string | null)[] = []

visit(ast, {
OperationDefinition: {
enter(node) {
inOperation = true
if (node.name) {
hasName = true
operationNames.push(null)
return BREAK
}
},
leave() {
inOperation = false
},
},
Field: {
enter(node) {
operationName = node.name.value
if (!inOperation) return
operationNames.push(node.name.value)
return BREAK
},
},
})

if (hasName) return ast
let index = 0

return visit(ast, {
OperationDefinition: {
enter(node) {
const operationName = operationNames[index]
index += 1
if (!operationName) return false
return {
...node,
name: {
kind: Kind.NAME,
value: operationName,
value: operationNames,
},
}
},
leave() {
return BREAK
},
},
})
}
Expand Down
104 changes: 52 additions & 52 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('normalizeGraphQLQuery', () => {
const { server: echoServer, schema: echoSchema } = createEchoServer()

test('simple case', async () => {
const testQuery = `#graphql
const testQuery = /* GraphQL */ `
query Query(
$__a: String!
$__b: String!
Expand All @@ -16,15 +16,17 @@ describe('normalizeGraphQLQuery', () => {
$__f: Float!
$__g: Boolean!
) {
echo(input: {
a: $__a
b: $__b
c: $__c
d: $__d
e: $__e
f: $__f
g: $__g
}) {
echo(
input: {
a: $__a
b: $__b
c: $__c
d: $__d
e: $__e
f: $__f
g: $__g
}
) {
a
b
c
Expand All @@ -46,20 +48,13 @@ describe('normalizeGraphQLQuery', () => {
})

test('same name variables', async () => {
const testQuery = `#graphql
query Query(
$__a1: String!
$__a2: String!
) {
echo1: echo(input: {
a: $__a1
}) {
const testQuery = /* GraphQL */ `
query Query($__a1: String!, $__a2: String!) {
echo1: echo(input: { a: $__a1 }) {
a
}
echo2: echo(input: {
a: $__a2
}) {
echo2: echo(input: { a: $__a2 }) {
a
}
}
Expand Down Expand Up @@ -118,15 +113,9 @@ describe('normalizeGraphQLQuery', () => {
})

test('fixture#1 - unused variables', async () => {
const testQuery = `#graphql
query Query(
$a: String!
$b: String!
) {
echo(input: {
a: $a
b: $b
}) {
const testQuery = /* GraphQL */ `
query Query($a: String!, $b: String!) {
echo(input: { a: $a, b: $b }) {
a
b
}
Expand All @@ -146,15 +135,9 @@ describe('normalizeGraphQLQuery', () => {
})

test('fixture#2 - date scalar type', async () => {
const testQuery = `#graphql
query Query(
$a: String!
$o: Date!
) {
echo(input: {
a: $a
o: $o
}) {
const testQuery = /* GraphQL */ `
query Query($a: String!, $o: Date!) {
echo(input: { a: $a, o: $o }) {
a
o
}
Expand All @@ -171,19 +154,9 @@ describe('normalizeGraphQLQuery', () => {
})

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
}
}) {
const testQuery = /* GraphQL */ `
query Query($a1: String!, $a2: String!, $b: String!) {
echo(input: { a: $a1, k: { a: $a2, b: $b } }) {
...a
}
}
Expand All @@ -209,4 +182,31 @@ describe('normalizeGraphQLQuery', () => {
testVariables,
)
})

test('fixture#4 - fill operation name when fragment before operation', async () => {
const testQuery = /* GraphQL */ `
fragment a on EchoRes {
a
b
}
query ($a1: String!, $a2: String!, $b: String!) {
echo(input: { a: $a1, k: { a: $a2, b: $b } }) {
...a
k {
...a
}
}
}
`

const testVariables = generateVariables(echoSchema, testQuery)

await shouldReturnSameValueWithOriginal(
echoServer,
testQuery,
testVariables,
)
})
})

0 comments on commit ee4cc76

Please sign in to comment.