diff --git a/.changeset/moody-years-relax.md b/.changeset/moody-years-relax.md new file mode 100644 index 000000000..534888d56 --- /dev/null +++ b/.changeset/moody-years-relax.md @@ -0,0 +1,5 @@ +--- +'houdini-svelte': patch +--- + +FIX: type imports not generated correctly for non-fetching endpoints diff --git a/e2e/sveltekit/src/routes/stubs/layoutQuery/+layout.gql b/e2e/sveltekit/src/routes/stubs/layoutQuery/+layout.gql new file mode 100644 index 000000000..3a77893b4 --- /dev/null +++ b/e2e/sveltekit/src/routes/stubs/layoutQuery/+layout.gql @@ -0,0 +1,5 @@ +query StubLayoutQuery { + user(id: "1", snapshot: "layout-query") { + id + } +} diff --git a/e2e/sveltekit/src/routes/stubs/layoutQuery/sub/+page.ts b/e2e/sveltekit/src/routes/stubs/layoutQuery/sub/+page.ts new file mode 100644 index 000000000..61864da57 --- /dev/null +++ b/e2e/sveltekit/src/routes/stubs/layoutQuery/sub/+page.ts @@ -0,0 +1,4 @@ +// stub file to ensure that types are generated for non-houdini files +// see: https://github.com/HoudiniGraphql/houdini/issues/700 +// should fail on the end to end type check if not generated properly +import type { PageData } from './$houdini'; diff --git a/e2e/sveltekit/src/routes/stubs/layoutScript/+layout.ts b/e2e/sveltekit/src/routes/stubs/layoutScript/+layout.ts new file mode 100644 index 000000000..ba45b5281 --- /dev/null +++ b/e2e/sveltekit/src/routes/stubs/layoutScript/+layout.ts @@ -0,0 +1,4 @@ +// stub file to ensure that types are generated for non-houdini files +// see: https://github.com/HoudiniGraphql/houdini/issues/700 +// should fail on the end to end type check if not generated properly +import type { LayoutData } from './$houdini'; diff --git a/e2e/sveltekit/src/routes/stubs/pageQuery/+page.gql b/e2e/sveltekit/src/routes/stubs/pageQuery/+page.gql new file mode 100644 index 000000000..ef1c5a9bc --- /dev/null +++ b/e2e/sveltekit/src/routes/stubs/pageQuery/+page.gql @@ -0,0 +1,5 @@ +query StubPageQuery { + user(id: "1", snapshot: "page-query") { + id + } +} diff --git a/e2e/sveltekit/src/routes/stubs/pageQuery/sub/+page.ts b/e2e/sveltekit/src/routes/stubs/pageQuery/sub/+page.ts new file mode 100644 index 000000000..61864da57 --- /dev/null +++ b/e2e/sveltekit/src/routes/stubs/pageQuery/sub/+page.ts @@ -0,0 +1,4 @@ +// stub file to ensure that types are generated for non-houdini files +// see: https://github.com/HoudiniGraphql/houdini/issues/700 +// should fail on the end to end type check if not generated properly +import type { PageData } from './$houdini'; diff --git a/e2e/sveltekit/src/routes/stubs/pageScript/+page.ts b/e2e/sveltekit/src/routes/stubs/pageScript/+page.ts new file mode 100644 index 000000000..61864da57 --- /dev/null +++ b/e2e/sveltekit/src/routes/stubs/pageScript/+page.ts @@ -0,0 +1,4 @@ +// stub file to ensure that types are generated for non-houdini files +// see: https://github.com/HoudiniGraphql/houdini/issues/700 +// should fail on the end to end type check if not generated properly +import type { PageData } from './$houdini'; diff --git a/packages/houdini-svelte/src/plugin/codegen/routes/kit.test.ts b/packages/houdini-svelte/src/plugin/codegen/routes/kit.test.ts index 3c059e2d1..0f89c43f4 100644 --- a/packages/houdini-svelte/src/plugin/codegen/routes/kit.test.ts +++ b/packages/houdini-svelte/src/plugin/codegen/routes/kit.test.ts @@ -132,7 +132,12 @@ type OptionalUnion, A extends keyof U = U extends type PageParentData = EnsureDefined; export type PageServerData = null; -export type PageData = Expand; +export type PageLoad = OutputDataShape> = Kit.Load; +export type PageLoadEvent = Parameters[0]; + +export type PageData = Expand> & OptionalUnion>>> & { + MyPageQuery: MyPageQueryStore +}>;" `, }, }, @@ -180,10 +185,9 @@ type OptionalUnion, A extends keyof U = U extends type PageParentData = EnsureDefined; type PageParams = PageLoadEvent["params"]; export type PageServerData = null; - -export type PageData = Expand & { - MyPageQuery: MyPageQueryStore -}>;`) +export type PageLoad = OutputDataShape> = Kit.Load; +export type PageLoadEvent = Parameters[0]; +`) }) test('generates types for layout queries', async function () { @@ -221,7 +225,9 @@ type LayoutParams = RouteParams & { } type LayoutParentData = EnsureDefined<{}>; export type LayoutServerData = null; -export type LayoutData = Expand; +export type LayoutLoad = OutputDataShape> = Kit.Load; +export type LayoutLoadEvent = Parameters[0]; +export type LayoutData = Expand> & OptionalUnion>>>; `, }, }, @@ -268,8 +274,10 @@ type OptionalUnion, A extends keyof U = U extends type LayoutParams = RouteParams & {}; type LayoutParentData = EnsureDefined<{}>; export type LayoutServerData = null; +export type LayoutLoad = OutputDataShape> = Kit.Load; +export type LayoutLoadEvent = Parameters[0]; -export type LayoutData = Expand & { +export type LayoutData = Expand> & OptionalUnion>>> & { MyLayoutQuery: MyLayoutQueryStore }>; `) diff --git a/packages/houdini-svelte/src/plugin/fsPatch.ts b/packages/houdini-svelte/src/plugin/fsPatch.ts index ae6980091..30805bbb3 100644 --- a/packages/houdini-svelte/src/plugin/fsPatch.ts +++ b/packages/houdini-svelte/src/plugin/fsPatch.ts @@ -173,14 +173,14 @@ filesystem.readdirSync = function ( // if there is a route component but no script, add the script if ( - contains('+page.svelte') && + contains('+page.svelte', '+page.gql') && !contains('+page.js', '+page.ts', '+page.server.js', '+page.server.ts') ) { result.push(virtual_file('+page.js', options)) } // if there is a layout file but no layout.js, we need to make one - if (contains('+layout.svelte') && !contains('+layout.ts', '+layout.js')) { + if (contains('+layout.svelte', '+layout.gql') && !contains('+layout.ts', '+layout.js')) { result.push(virtual_file('+layout.js', options)) } diff --git a/packages/houdini-svelte/src/plugin/kit.ts b/packages/houdini-svelte/src/plugin/kit.ts index 4ec1c4585..b80ba85b5 100644 --- a/packages/houdini-svelte/src/plugin/kit.ts +++ b/packages/houdini-svelte/src/plugin/kit.ts @@ -123,6 +123,8 @@ export async function walk_routes( let pageQueries: graphql.OperationDefinitionNode[] = [] let layoutQueries: graphql.OperationDefinitionNode[] = [] + let validRoute = false + //parse all files and push contents into page/layoutExports, page/layoutQueries for (const child of await fs.readdir(dirpath)) { const childPath = path.join(dirpath, child) @@ -135,6 +137,7 @@ export async function walk_routes( //maybe turn into switch-case statement? if (is_layout_script(framework, childPath)) { + validRoute = true const { houdini_load, exports } = await extract_load_function(config, childPath) // mutate with optional layoutQueries. Takes in array of OperationDefinitionNodes @@ -147,6 +150,7 @@ export async function walk_routes( // push all exports to our layoutExports layoutExports.push(...exports) } else if (is_page_script(framework, childPath)) { + validRoute = true const { houdini_load, exports } = await extract_load_function(config, childPath) // mutate with optional pageQueries. Takes in array of OperationDefinitionNodes @@ -159,6 +163,7 @@ export async function walk_routes( // push all exports to our pageExports pageExports.push(...exports) } else if (is_layout_component(framework, childPath)) { + validRoute = true const contents = await fs.readFile(childPath) if (!contents) { continue @@ -186,6 +191,7 @@ export async function walk_routes( }, }) } else if (is_component(config, framework, child)) { + validRoute = true const contents = await fs.readFile(childPath) if (!contents) { continue @@ -213,6 +219,7 @@ export async function walk_routes( }, }) } else if (child === plugin_config(config).layoutQueryFilename) { + validRoute = true const contents = await fs.readFile(childPath) if (!contents) { continue @@ -228,6 +235,7 @@ export async function walk_routes( throw routeQueryError(childPath) } } else if (child === plugin_config(config).pageQueryFilename) { + validRoute = true const contents = await fs.readFile(childPath) if (!contents) { continue @@ -250,13 +258,7 @@ export async function walk_routes( // if length of any field is greater than 0, we run our route. // pageExports can be defined where queries aren't // e.g. QueryVariables but uses parent dirs layoutQuery (probably a bad idea) - if ( - visitor.route && - (pageQueries.length > 0 || - layoutQueries.length > 0 || - pageExports.length > 0 || - layoutExports.length > 0) - ) { + if (visitor.route && validRoute) { //NOTE: Define sveltekitTypeFilePath here so that we ensure route is valid const relative_path_regex = /src(.*)/