From db67e822fcec2362538bda548d058fa7e3397ffa Mon Sep 17 00:00:00 2001 From: Alec Aivazis Date: Sun, 13 Nov 2022 20:13:38 -0800 Subject: [PATCH] Fix double import in generated subscription stores (#689) * fix double import of subscription store * add happy path tests for generated stores * changeset * fix comment --- .changeset/funny-kings-explode.md | 5 ++ .../plugin/codegen/stores/fragment.test.ts | 62 +++++++++++++++++++ .../src/plugin/codegen/stores/fragment.ts | 2 +- .../plugin/codegen/stores/mutation.test.ts | 56 +++++++++++++++++ .../src/plugin/codegen/stores/query.test.ts | 2 +- .../codegen/stores/subscription.test.ts | 59 ++++++++++++++++++ .../src/plugin/codegen/stores/subscription.ts | 1 - 7 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 .changeset/funny-kings-explode.md create mode 100644 packages/houdini-svelte/src/plugin/codegen/stores/fragment.test.ts create mode 100644 packages/houdini-svelte/src/plugin/codegen/stores/mutation.test.ts create mode 100644 packages/houdini-svelte/src/plugin/codegen/stores/subscription.test.ts diff --git a/.changeset/funny-kings-explode.md b/.changeset/funny-kings-explode.md new file mode 100644 index 0000000000..0f66c84c3b --- /dev/null +++ b/.changeset/funny-kings-explode.md @@ -0,0 +1,5 @@ +--- +'houdini-svelte': patch +--- + +Fix duplicate import for generated subscription stores diff --git a/packages/houdini-svelte/src/plugin/codegen/stores/fragment.test.ts b/packages/houdini-svelte/src/plugin/codegen/stores/fragment.test.ts new file mode 100644 index 0000000000..dd41efc341 --- /dev/null +++ b/packages/houdini-svelte/src/plugin/codegen/stores/fragment.test.ts @@ -0,0 +1,62 @@ +import { fs, CollectedGraphQLDocument, path } from 'houdini' +import { mockCollectedDoc } from 'houdini/test' +import * as recast from 'recast' +import * as typeScriptParser from 'recast/parsers/typescript' +import { test, expect } from 'vitest' + +import runPipeline from '..' +import '../..' +import { test_config } from '../../../test' +import { stores_directory } from '../../kit' + +test('generates a store for every query', async function () { + const config = await test_config() + const plugin_root = config.pluginDirectory('test-plugin') + + // the documents to test + const docs: CollectedGraphQLDocument[] = [ + mockCollectedDoc(`fragment TestFragment1 on User { id }`), + mockCollectedDoc(`fragment TestFragment2 on User { id }`), + ] + + // execute the generator + await runPipeline({ config, documents: docs, plugin_root, framework: 'kit' }) + + // look up the files in the artifact directory + const files = await fs.readdir(stores_directory(plugin_root)) + + // and they have the right names + expect(files).toEqual(expect.arrayContaining(['TestFragment1.js', 'TestFragment2.js'])) + // and type definitions exist + expect(files).toEqual(expect.arrayContaining(['TestFragment1.d.ts', 'TestFragment2.d.ts'])) + + const contents = await fs.readFile(path.join(stores_directory(plugin_root), 'TestFragment1.js')) + const parsed = recast.parse(contents!, { + parser: typeScriptParser, + }).program + + await expect(parsed).toMatchInlineSnapshot( + ` + import { FragmentStore } from '$houdini/plugins/houdini-svelte/runtime/stores' + import artifact from '$houdini/artifacts/TestFragment1' + + + // create the fragment store + + export class TestFragment1Store extends FragmentStore { + constructor() { + super({ + artifact, + storeName: "TestFragment1Store", + variables: true, + + }) + } + } + + export const GQL_TestFragment1 = new TestFragment1Store() + + export default GQL_TestFragment1 + ` + ) +}) diff --git a/packages/houdini-svelte/src/plugin/codegen/stores/fragment.ts b/packages/houdini-svelte/src/plugin/codegen/stores/fragment.ts index 730a4e56bd..b013352334 100644 --- a/packages/houdini-svelte/src/plugin/codegen/stores/fragment.ts +++ b/packages/houdini-svelte/src/plugin/codegen/stores/fragment.ts @@ -38,7 +38,7 @@ ${ : '' } -// create the query store +// create the fragment store export class ${storeName} extends ${store_class} { constructor() { diff --git a/packages/houdini-svelte/src/plugin/codegen/stores/mutation.test.ts b/packages/houdini-svelte/src/plugin/codegen/stores/mutation.test.ts new file mode 100644 index 0000000000..b1c24113be --- /dev/null +++ b/packages/houdini-svelte/src/plugin/codegen/stores/mutation.test.ts @@ -0,0 +1,56 @@ +import { fs, CollectedGraphQLDocument, path } from 'houdini' +import { mockCollectedDoc } from 'houdini/test' +import * as recast from 'recast' +import * as typeScriptParser from 'recast/parsers/typescript' +import { test, expect } from 'vitest' + +import runPipeline from '..' +import '../..' +import { test_config } from '../../../test' +import { stores_directory } from '../../kit' + +test('generates a store for every query', async function () { + const config = await test_config() + const plugin_root = config.pluginDirectory('test-plugin') + + // the documents to test + const docs: CollectedGraphQLDocument[] = [ + mockCollectedDoc(`mutation TestMutation1 { updateUser { id } }`), + mockCollectedDoc(`mutation TestMutation2 { updateUser { id } }`), + ] + + // execute the generator + await runPipeline({ config, documents: docs, plugin_root, framework: 'kit' }) + + // look up the files in the artifact directory + const files = await fs.readdir(stores_directory(plugin_root)) + + // and they have the right names + expect(files).toEqual(expect.arrayContaining(['TestMutation1.js', 'TestMutation2.js'])) + // and type definitions exist + expect(files).toEqual(expect.arrayContaining(['TestMutation1.d.ts', 'TestMutation2.d.ts'])) + + const contents = await fs.readFile(path.join(stores_directory(plugin_root), 'TestMutation1.js')) + const parsed = recast.parse(contents!, { + parser: typeScriptParser, + }).program + + await expect(parsed).toMatchInlineSnapshot( + ` + import artifact from '$houdini/artifacts/TestMutation1' + import { MutationStore } from '$houdini/plugins/houdini-svelte/runtime/stores' + + export class TestMutation1Store extends MutationStore { + constructor() { + super({ + artifact, + }) + } + } + + export const GQL_TestMutation1 = new TestMutation1Store() + + export default GQL_TestMutation1 + ` + ) +}) diff --git a/packages/houdini-svelte/src/plugin/codegen/stores/query.test.ts b/packages/houdini-svelte/src/plugin/codegen/stores/query.test.ts index de54f53ca4..2b65dfd39b 100644 --- a/packages/houdini-svelte/src/plugin/codegen/stores/query.test.ts +++ b/packages/houdini-svelte/src/plugin/codegen/stores/query.test.ts @@ -9,7 +9,7 @@ import '../..' import { pipeline_test, test_config } from '../../../test' import { stores_directory } from '../../kit' -test('generates a store for every query', async function () { +test('generates a query store for every query', async function () { const config = await test_config() const plugin_root = config.pluginDirectory('test-plugin') diff --git a/packages/houdini-svelte/src/plugin/codegen/stores/subscription.test.ts b/packages/houdini-svelte/src/plugin/codegen/stores/subscription.test.ts new file mode 100644 index 0000000000..98ff681d4c --- /dev/null +++ b/packages/houdini-svelte/src/plugin/codegen/stores/subscription.test.ts @@ -0,0 +1,59 @@ +import { fs, CollectedGraphQLDocument, path } from 'houdini' +import { mockCollectedDoc } from 'houdini/test' +import * as recast from 'recast' +import * as typeScriptParser from 'recast/parsers/typescript' +import { test, expect } from 'vitest' + +import runPipeline from '..' +import '../..' +import { test_config } from '../../../test' +import { stores_directory } from '../../kit' + +test('generates a store for every query', async function () { + const config = await test_config() + const plugin_root = config.pluginDirectory('test-plugin') + + // the documents to test + const docs: CollectedGraphQLDocument[] = [ + mockCollectedDoc(`subscription TestSubscription1 { newUser { id } }`), + mockCollectedDoc(`subscription TestSubscription2 { newUser { id } }`), + ] + + // execute the generator + await runPipeline({ config, documents: docs, plugin_root, framework: 'kit' }) + + // look up the files in the artifact directory + const files = await fs.readdir(stores_directory(plugin_root)) + + // and they have the right names + expect(files).toEqual(expect.arrayContaining(['TestSubscription1.js', 'TestSubscription2.js'])) + // and type definitions exist + expect(files).toEqual( + expect.arrayContaining(['TestSubscription1.d.ts', 'TestSubscription2.d.ts']) + ) + + const contents = await fs.readFile( + path.join(stores_directory(plugin_root), 'TestSubscription1.js') + ) + const parsed = recast.parse(contents!, { + parser: typeScriptParser, + }).program + + await expect(parsed).toMatchInlineSnapshot( + ` + import { SubscriptionStore } from '$houdini/plugins/houdini-svelte/runtime/stores' + + export class TestSubscription1Store extends SubscriptionStore { + constructor() { + super({ + artifact, + }) + } + } + + export const GQL_TestSubscription1 = new TestSubscription1Store() + + export default GQL_TestSubscription1 + ` + ) +}) diff --git a/packages/houdini-svelte/src/plugin/codegen/stores/subscription.ts b/packages/houdini-svelte/src/plugin/codegen/stores/subscription.ts index 4fefa8cb53..4927f1e199 100644 --- a/packages/houdini-svelte/src/plugin/codegen/stores/subscription.ts +++ b/packages/houdini-svelte/src/plugin/codegen/stores/subscription.ts @@ -17,7 +17,6 @@ export async function subscriptionStore( // the content of the store const storeContent = `${statement} -import { SubscriptionStore } from '../runtime/stores' export class ${storeName} extends ${store_class} { constructor() {