Skip to content

Commit

Permalink
Fix double import in generated subscription stores (#689)
Browse files Browse the repository at this point in the history
* fix double import of subscription store

* add happy path tests for generated stores

* changeset

* fix comment
  • Loading branch information
AlecAivazis authored Nov 14, 2022
1 parent 36401fa commit db67e82
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-kings-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini-svelte': patch
---

Fix duplicate import for generated subscription stores
62 changes: 62 additions & 0 deletions packages/houdini-svelte/src/plugin/codegen/stores/fragment.test.ts
Original file line number Diff line number Diff line change
@@ -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
`
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ${
: ''
}
// create the query store
// create the fragment store
export class ${storeName} extends ${store_class} {
constructor() {
Expand Down
56 changes: 56 additions & 0 deletions packages/houdini-svelte/src/plugin/codegen/stores/mutation.test.ts
Original file line number Diff line number Diff line change
@@ -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
`
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Original file line number Diff line number Diff line change
@@ -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
`
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

2 comments on commit db67e82

@vercel
Copy link

@vercel vercel bot commented on db67e82 Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on db67e82 Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs-next – ./site

docs-next-houdinigraphql.vercel.app
docs-next-git-main-houdinigraphql.vercel.app
docs-next-kohl.vercel.app

Please sign in to comment.