Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ NEW: global stores - no query stores by default #852

Merged
merged 14 commits into from
Jan 31, 2023
5 changes: 5 additions & 0 deletions .changeset/poor-hats-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini-plugin-svelte-global-stores': patch
jycouet marked this conversation as resolved.
Show resolved Hide resolved
---

by default global stores of type 'query' will not be generated. You have to opt-in to generate them in houdini.config.js
4 changes: 3 additions & 1 deletion e2e/sveltekit/houdini.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const config = {
}
},
plugins: {
'houdini-plugin-svelte-global-stores': {},
'houdini-plugin-svelte-global-stores': {
storesToGenerate: ['Query', 'Mutation', 'Subscription', 'Fragment']
jycouet marked this conversation as resolved.
Show resolved Hide resolved
},
'houdini-svelte': {}
}
};
Expand Down
6 changes: 4 additions & 2 deletions packages/houdini-plugin-svelte-global-stores/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const config = {

plugins: {
'houdini-plugin-svelte-global-stores': {
prefix: 'G_'
prefix: 'GQL_',
storesToGenerate: ['Mutation', 'Subscription', 'Fragment']
},
'houdini-svelte': {}
}
Expand All @@ -54,8 +55,9 @@ export default config;

```

One configuration option is available:
Configuration option is available:
- `prefix` (optional, default: `GQL_`): The default prefix of your global stores. This lets your editor provide autocompletion with just a few characters.
- `storesToGenerate` (optional, default: `['Mutation', 'Subscription', 'Fragment']`). Note that by default, 'Query' is omitted on purpose.


## Usage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { GenerateHookInput } from 'houdini'
import { cleanupFiles, fs, ArtifactKind, path } from 'houdini'
import { ArtifactKind, cleanupFiles, fs, path } from 'houdini'

import { global_stores_directory } from '../../kit'
import { global_stores_directory, plugin_config } from '../../kit'
import { fragmentStore } from './fragment'
import { mutationStore } from './mutation'
import { queryStore } from './query'
import { subscriptionStore } from './subscription'

export default async function storesGenerator(input: GenerateHookInput) {
const { documents } = input
const { documents, config } = input
const storesToGenerate = plugin_config(config).storesToGenerate

const listOfStores: (string | null)[] = []

Expand All @@ -19,13 +20,22 @@ export default async function storesGenerator(input: GenerateHookInput) {
return
}

if (doc.kind === ArtifactKind.Query) {
if (doc.kind === ArtifactKind.Query && storesToGenerate.includes('Query')) {
listOfStores.push(await queryStore(input, doc))
} else if (doc.kind === ArtifactKind.Mutation) {
} else if (
doc.kind === ArtifactKind.Mutation &&
storesToGenerate.includes('Mutation')
) {
listOfStores.push(await mutationStore(input, doc))
} else if (doc.kind === ArtifactKind.Subscription) {
} else if (
doc.kind === ArtifactKind.Subscription &&
storesToGenerate.includes('Subscription')
) {
listOfStores.push(await subscriptionStore(input, doc))
} else if (doc.kind === ArtifactKind.Fragment) {
} else if (
doc.kind === ArtifactKind.Fragment &&
storesToGenerate.includes('Fragment')
) {
listOfStores.push(await fragmentStore(input, doc))
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test('change prefix to "yop___"', async function () {
'houdini-svelte': {},
'houdini-plugin-svelte-global-stores': {
prefix: 'yop___',
storesToGenerate: ['Query'],
},
},
})
Expand Down Expand Up @@ -43,6 +44,7 @@ test('change prefix to ""', async function () {
'houdini-svelte': {},
'houdini-plugin-svelte-global-stores': {
prefix: '',
storesToGenerate: ['Query'],
},
},
})
Expand All @@ -63,3 +65,28 @@ test('change prefix to ""', async function () {
export const TestQuery = new TestQueryStore()
`)
})

test('no query', async function () {
const docs = [`query TestQuery { version }`]

const { plugin_root } = await pipeline_test(docs, {
plugins: {
'houdini-svelte': {},
'houdini-plugin-svelte-global-stores': {
prefix: '',
},
},
})

const contents = await fs.readFile(
path.join(global_stores_directory(plugin_root), 'TestQuery.js')
)

// parse the contents
const parsed = recast.parse(contents!, {
parser: typeScriptParser,
}).program

// check the file contents
expect(parsed).toMatchInlineSnapshot('null')
})
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ export type HoudiniPluginSvelteGlobalStoresConfig = {
* @default GQL_
*/
prefix?: string

/**
* Types of stores to generate.
*
* _Note: by default, 'Query' is omitted on purpose._
* @default ['Mutation', 'Subscription', 'Fragment']
*/
storesToGenerate?: ('Query' | 'Mutation' | 'Subscription' | 'Fragment')[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function plugin_config(config: Config): Required<HoudiniPluginSvelteGloba

return {
prefix: 'GQL_',
storesToGenerate: ['Mutation', 'Subscription', 'Fragment'],
...cfg,
}
}
Loading