Skip to content

Commit

Permalink
Only dedupe pagination queries if the variables match (#1378)
Browse files Browse the repository at this point in the history
Co-authored-by: Seppe Dekeyser <[email protected]>
  • Loading branch information
AlecAivazis and SeppahBaws authored Nov 22, 2024
1 parent 85f5240 commit d2dbcd2
Show file tree
Hide file tree
Showing 15 changed files with 449 additions and 54 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-worms-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini': patch
---

Add match argument to dedupe directive
24 changes: 18 additions & 6 deletions packages/houdini/src/codegen/generators/artifacts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as recast from 'recast'
import type {
CachePolicies,
Config,
DedupeMatchModes,
Document,
DocumentArtifact,
MutationArtifact,
Expand All @@ -12,6 +13,7 @@ import type {
} from '../../../lib'
import {
ArtifactKind,
DedupeMatchMode,
HoudiniError,
cleanupFiles,
fs,
Expand Down Expand Up @@ -233,12 +235,22 @@ export default function artifactGenerator(stats: {
(arg) => arg.name.value === 'cancelFirst'
)

dedupe =
cancelFirstArg &&
cancelFirstArg.value.kind === 'BooleanValue' &&
cancelFirstArg.value
? 'first'
: 'last'
const matchArg = dedupeDirective.arguments?.find(
(arg) => arg.name.value === 'match'
)

dedupe = {
cancel:
cancelFirstArg &&
cancelFirstArg.value.kind === 'BooleanValue' &&
cancelFirstArg.value
? 'first'
: 'last',
match:
matchArg && matchArg.value.kind === 'EnumValue'
? (matchArg.value.value as DedupeMatchModes)
: DedupeMatchMode.Operation,
}
}

// use this selection set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,11 @@ test('paginate over unions', async function () {
},
"pluginData": {},
"dedupe": "last",
"dedupe": {
"cancel": "last",
"match": "Variables"
},
"input": {
"fields": {
Expand Down Expand Up @@ -4932,7 +4936,11 @@ describe('mutation artifacts', function () {
},
"pluginData": {},
"dedupe": "last",
"dedupe": {
"cancel": "last",
"match": "Variables"
},
"input": {
"fields": {
Expand Down Expand Up @@ -5185,7 +5193,11 @@ describe('mutation artifacts', function () {
},
"pluginData": {},
"dedupe": "last",
"dedupe": {
"cancel": "last",
"match": "Variables"
},
"input": {
"fields": {
Expand Down Expand Up @@ -7774,7 +7786,12 @@ test('persists dedupe which', async function () {
},
"pluginData": {},
"dedupe": "last",
"dedupe": {
"cancel": "last",
"match": "Operation"
},
"policy": "CacheOrNetwork",
"partial": false
};
Expand Down Expand Up @@ -7846,11 +7863,172 @@ test('persists dedupe first', async function () {
},
"pluginData": {},
"dedupe": "first",
"dedupe": {
"cancel": "first",
"match": "Operation"
},
"policy": "CacheOrNetwork",
"partial": false
};
"HoudiniHash=3dfb64916aa4359cf85f08b3544bbc7382fd818935c5a0e92f324a2d2519c227";
`)
})

describe('Parses the correct matching mode', function () {
test('match mode variables', async function () {
// the config to use in tests
const config = testConfig()
// the documents to test
const docs: Document[] = [
mockCollectedDoc(`
query FindUser @dedupe(match: Variables) {
usersByOffset {
name
}
}
`),
]

// execute the generator
await runPipeline(config, docs)

// load the contents of the file
expect(docs[0]).toMatchInlineSnapshot(`
export default {
"name": "FindUser",
"kind": "HoudiniQuery",
"hash": "63be02f78e12d6dd155da0aac94892e700a5be1eeb66dfc2305740ce2464dd3b",
"raw": \`query FindUser {
usersByOffset {
name
id
}
}
\`,
"rootType": "Query",
"stripVariables": [],
"selection": {
"fields": {
"usersByOffset": {
"type": "User",
"keyRaw": "usersByOffset",
"selection": {
"fields": {
"name": {
"type": "String",
"keyRaw": "name",
"visible": true
},
"id": {
"type": "ID",
"keyRaw": "id",
"visible": true
}
}
},
"visible": true
}
}
},
"pluginData": {},
"dedupe": {
"cancel": "last",
"match": "Variables"
},
"policy": "CacheOrNetwork",
"partial": false
};
"HoudiniHash=f3faa6e93bde578b11490f9a32518e410a47ec242b0ef94331fc4fb5b01ace20";
`)
})

test('match mode operation', async function () {
// the config to use in tests
const config = testConfig()
// the documents to test
const docs: Document[] = [
mockCollectedDoc(`
query FindUser @dedupe(match: Operation) {
usersByOffset {
name
}
}
`),
]

// execute the generator
await runPipeline(config, docs)

// load the contents of the file
expect(docs[0]).toMatchInlineSnapshot(`
export default {
"name": "FindUser",
"kind": "HoudiniQuery",
"hash": "63be02f78e12d6dd155da0aac94892e700a5be1eeb66dfc2305740ce2464dd3b",
"raw": \`query FindUser {
usersByOffset {
name
id
}
}
\`,
"rootType": "Query",
"stripVariables": [],
"selection": {
"fields": {
"usersByOffset": {
"type": "User",
"keyRaw": "usersByOffset",
"selection": {
"fields": {
"name": {
"type": "String",
"keyRaw": "name",
"visible": true
},
"id": {
"type": "ID",
"keyRaw": "id",
"visible": true
}
}
},
"visible": true
}
}
},
"pluginData": {},
"dedupe": {
"cancel": "last",
"match": "Operation"
},
"policy": "CacheOrNetwork",
"partial": false
};
"HoudiniHash=1e1c9cd888a109d85a8bda7c3470aeb645b25678fa17916a3b016816b7a9d783";
`)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,11 @@ test('cursor as scalar gets the right pagination query argument types', async fu
},
"pluginData": {},
"dedupe": "last",
"dedupe": {
"cancel": "last",
"match": "Variables"
},
"input": {
"fields": {
Expand Down
14 changes: 14 additions & 0 deletions packages/houdini/src/codegen/generators/definitions/enums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ test('generates runtime definitions for each enum', async function () {
expect(parsedQuery).toMatchInlineSnapshot(`
type ValuesOf<T> = T[keyof T]
export declare const DedupeMatchMode: {
readonly Variables: "Variables";
readonly Operation: "Operation";
readonly None: "None";
}
export type DedupeMatchMode$options = ValuesOf<typeof DedupeMatchMode>
/** Documentation of testenum1 */
export declare const TestEnum1: {
/** Documentation of Value1 */
Expand Down Expand Up @@ -72,5 +80,11 @@ test('generates runtime definitions for each enum', async function () {
"Value3": "Value3",
"Value2": "Value2"
};
export const DedupeMatchMode = {
"Variables": "Variables",
"Operation": "Operation",
"None": "None"
};
`)
})
Loading

0 comments on commit d2dbcd2

Please sign in to comment.