Skip to content

Commit

Permalink
No query stores generated by default (HoudiniGraphql#852)
Browse files Browse the repository at this point in the history
* ✨ NEW: global stores no query

* 👌 FIX: lint

* ♻️ UPDATE: lock

* ⚡ UPDATE: to major

* 🚸 IMPROVE: api

* ✅ NEW: tests

* 🐛 FIX: casing

* 🐛 FIX: last chapter of a page is now searchable

* 👌 UPDATE: lint

* 👌 FIX: yoga error

* rename storesToGenerate to generate

* fix tests

---------

Co-authored-by: Alec Aivazis <alec@aivazis.com>
  • Loading branch information
jycouet and AlecAivazis committed Feb 10, 2023
1 parent fed9873 commit 73e965e
Show file tree
Hide file tree
Showing 18 changed files with 8,501 additions and 11,750 deletions.
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': major
---

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': {
generate: ['query', 'mutation', 'subscription', 'fragment']
},
'houdini-svelte': {}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { CachePolicy, paginatedFragment, graphql } from '$houdini';
import { CachePolicy, graphql, paginatedFragment } from '$houdini';
const queryResult = graphql(`
query UserFragmentBidirectionalCursorQuery @load {
Expand All @@ -10,6 +10,7 @@
`);
const fragmentResult = paginatedFragment(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$queryResult.data!.user!,
graphql(`
fragment BidirectionalCursorFragment on User {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { expect, test } from '@playwright/test';
import { test } from '@playwright/test';
import { routes } from '../../../../lib/utils/routes.js';
import {
expect_1_gql,
expect_0_gql,
expectToBe,
expectToContain,
expect_1_gql,
goto
} from '../../../../lib/utils/testsHelper.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { expect, test } from '@playwright/test';
import { test } from '@playwright/test';
import { routes } from '../../../../lib/utils/routes.js';
import {
expect_1_gql,
expect_0_gql,
expectToBe,
expectToContain,
expect_1_gql,
goto
} from '../../../../lib/utils/testsHelper.js';

Expand Down
4 changes: 1 addition & 3 deletions e2e/sveltekit/src/routes/stores/ssr-[userId]/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ test.describe('SSR-[userId] Page', () => {

// 4 Check id 77 (that doens't exist)
response = await expect_1_gql(page, `button[id="refresh-77"]`);
expect(response).toBe(
'{"data":null,"errors":[{"message":"User not found","locations":[{"line":2,"column":3}],"path":["user"]}]}'
);
expect(response).toBe('{"data":null,"errors":[{"message":"User not found","path":["user"]}]}');
});

test('Check that variables order doesnt matter', async ({ page }) => {
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_',
generate: ['mutation', 'subscription', 'fragment']
},
'houdini-svelte': {}
}
Expand All @@ -54,8 +55,9 @@ export default config;

```

One configuration option is available:
The following configuration options are available:
- `prefix` (optional, default: `GQL_`): The default prefix of your global stores. This lets your editor provide autocompletion with just a few characters.
- `generate` (optional, default: `['mutation', 'subscription', 'fragment']`). Note that by default, 'Query' is omitted on purpose. You can also pass `"all"` to generate all stores.


## Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,29 @@ test('global fragment type', async function () {
`
)
})

test('no fragment', async function () {
const docs = [`fragment TestFragment1 on User { id }`, `fragment TestFragment2 on User { id }`]

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

const contents = await fs.readFile(
path.join(global_stores_directory(pluginRoot), 'TestFragment1.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
@@ -1,14 +1,39 @@
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'

const is_store_needed = (
kindExpected: ArtifactKind,
kindDocument: ArtifactKind,
generate: ('query' | 'mutation' | 'subscription' | 'fragment')[] | 'all'
) => {
if (kindExpected === kindDocument) {
// build association between ArtifactKind and Literal
const kindLiteral: Record<
ArtifactKind,
'query' | 'mutation' | 'subscription' | 'fragment'
> = {
HoudiniQuery: 'query',
HoudiniMutation: 'mutation',
HoudiniSubscription: 'subscription',
HoudiniFragment: 'fragment',
}

if (generate === 'all' || generate.includes(kindLiteral[kindExpected])) {
return true
}
}
return false
}

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

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

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

if (doc.kind === ArtifactKind.Query) {
if (is_store_needed(ArtifactKind.Query, doc.kind, generate)) {
listOfStores.push(await queryStore(input, doc))
} else if (doc.kind === ArtifactKind.Mutation) {
} else if (is_store_needed(ArtifactKind.Mutation, doc.kind, generate)) {
listOfStores.push(await mutationStore(input, doc))
} else if (doc.kind === ArtifactKind.Subscription) {
} else if (is_store_needed(ArtifactKind.Subscription, doc.kind, generate)) {
listOfStores.push(await subscriptionStore(input, doc))
} else if (doc.kind === ArtifactKind.Fragment) {
} else if (is_store_needed(ArtifactKind.Fragment, doc.kind, generate)) {
listOfStores.push(await fragmentStore(input, doc))
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,32 @@ test('generates a store for every mutation', async function () {
`
)
})

test('no mutation', async function () {
const docs = [
`mutation TestMutation1 { updateUser { id } }`,
`mutation TestMutation2 { updateUser { id } }`,
]

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

const contents = await fs.readFile(
path.join(global_stores_directory(pluginRoot), 'TestMutation1.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 @@ -14,6 +14,7 @@ test('change prefix to "yop___"', async function () {
'houdini-svelte': {},
'houdini-plugin-svelte-global-stores': {
prefix: 'yop___',
generate: ['query'],
},
},
})
Expand Down Expand Up @@ -43,6 +44,62 @@ test('change prefix to ""', async function () {
'houdini-svelte': {},
'houdini-plugin-svelte-global-stores': {
prefix: '',
generate: ['query'],
},
},
})

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

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

// check the file contents
expect(parsed).toMatchInlineSnapshot(`
import { TestQueryStore } from '../../houdini-svelte/stores'
export const TestQuery = new TestQueryStore()
`)
})

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

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

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

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

// check the file contents
expect(parsed).toMatchInlineSnapshot('null')
})

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

const { pluginRoot } = await pipeline_test(docs, {
plugins: {
'houdini-svelte': {},
'houdini-plugin-svelte-global-stores': {
prefix: '',
generate: 'all',
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,32 @@ test('generates a store for every subscription', async function () {
`
)
})

test('no subscription', async function () {
const docs = [
`subscription TestSubscription1 { newUser { user { id } } }`,
`subscription TestSubscription2 { newUser { user { id } } }`,
]

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

const contents = await fs.readFile(
path.join(global_stores_directory(pluginRoot), 'TestSubscription1.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 @@ -65,4 +65,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']
*/
generate?: ('query' | 'mutation' | 'subscription' | 'fragment')[] | 'all'
}
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_',
generate: ['mutation', 'subscription', 'fragment'],
...cfg,
}
}
1 change: 0 additions & 1 deletion packages/houdini/src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ export async function runPipeline(config: Config, docs: Document[]) {
docs
)
} catch (e) {
console.log(e)
error = e as Error
}

Expand Down
Loading

0 comments on commit 73e965e

Please sign in to comment.