Skip to content

Commit

Permalink
Improve generated types for non-fetching routes (#702)
Browse files Browse the repository at this point in the history
* FIX: type imports not generated correctly for non-fetching endpoints

* FIX: route queries break without associated script

* FIX: add +page stubs to ensure tests pass

* FIX: Spoof route file in test, fix mock file contents

* FIX: error message

* modified fsPatch to include query files, improved e2e coverage, removed unnecessary mock fom kit.test.ts

* Removed incorrect comment.

* Remove commented mocks that are not longer needed

* FIX: Remove unncessary checks in statSync
  • Loading branch information
sjcobb2022 authored Nov 17, 2022
1 parent be0d880 commit 83d9340
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-years-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini-svelte': patch
---

FIX: type imports not generated correctly for non-fetching endpoints
5 changes: 5 additions & 0 deletions e2e/sveltekit/src/routes/stubs/layoutQuery/+layout.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query StubLayoutQuery {
user(id: "1", snapshot: "layout-query") {
id
}
}
4 changes: 4 additions & 0 deletions e2e/sveltekit/src/routes/stubs/layoutQuery/sub/+page.ts
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 4 additions & 0 deletions e2e/sveltekit/src/routes/stubs/layoutScript/+layout.ts
Original file line number Diff line number Diff line change
@@ -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';
5 changes: 5 additions & 0 deletions e2e/sveltekit/src/routes/stubs/pageQuery/+page.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query StubPageQuery {
user(id: "1", snapshot: "page-query") {
id
}
}
4 changes: 4 additions & 0 deletions e2e/sveltekit/src/routes/stubs/pageQuery/sub/+page.ts
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 4 additions & 0 deletions e2e/sveltekit/src/routes/stubs/pageScript/+page.ts
Original file line number Diff line number Diff line change
@@ -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';
22 changes: 15 additions & 7 deletions packages/houdini-svelte/src/plugin/codegen/routes/kit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ type OptionalUnion<U extends Record<string, any>, A extends keyof U = U extends
type PageParentData = EnsureDefined<import('../$types.js').LayoutData>;
export type PageServerData = null;
export type PageData = Expand<PageParentData>;
export type PageLoad<OutputData extends OutputDataShape<PageParentData> = OutputDataShape<PageParentData>> = Kit.Load<RouteParams, PageServerData, PageParentData, OutputData>;
export type PageLoadEvent = Parameters<PageLoad>[0];
export type PageData = Expand<Expand<Omit<PageParentData, keyof PageParentData & EnsureDefined<PageServerData>> & OptionalUnion<EnsureDefined<PageParentData & EnsureDefined<PageServerData>>>> & {
MyPageQuery: MyPageQueryStore
}>;"
`,
},
},
Expand Down Expand Up @@ -180,10 +185,9 @@ type OptionalUnion<U extends Record<string, any>, A extends keyof U = U extends
type PageParentData = EnsureDefined<import("../$houdini").LayoutData>;
type PageParams = PageLoadEvent["params"];
export type PageServerData = null;
export type PageData = Expand<Expand<PageParentData> & {
MyPageQuery: MyPageQueryStore
}>;`)
export type PageLoad<OutputData extends OutputDataShape<PageParentData> = OutputDataShape<PageParentData>> = Kit.Load<RouteParams, PageServerData, PageParentData, OutputData>;
export type PageLoadEvent = Parameters<PageLoad>[0];
`)
})

test('generates types for layout queries', async function () {
Expand Down Expand Up @@ -221,7 +225,9 @@ type LayoutParams = RouteParams & { }
type LayoutParentData = EnsureDefined<{}>;
export type LayoutServerData = null;
export type LayoutData = Expand<LayoutParentData>;
export type LayoutLoad<OutputData extends OutputDataShape<LayoutParentData> = OutputDataShape<LayoutParentData>> = Kit.Load<LayoutParams, LayoutServerData, LayoutParentData, OutputData>;
export type LayoutLoadEvent = Parameters<LayoutLoad>[0];
export type LayoutData = Expand<Omit<LayoutParentData, keyof LayoutParentData & EnsureDefined<LayoutServerData>> & OptionalUnion<EnsureDefined<LayoutParentData & EnsureDefined<LayoutServerData>>>>;
`,
},
},
Expand Down Expand Up @@ -268,8 +274,10 @@ type OptionalUnion<U extends Record<string, any>, A extends keyof U = U extends
type LayoutParams = RouteParams & {};
type LayoutParentData = EnsureDefined<{}>;
export type LayoutServerData = null;
export type LayoutLoad<OutputData extends OutputDataShape<LayoutParentData> = OutputDataShape<LayoutParentData>> = Kit.Load<LayoutParams, LayoutServerData, LayoutParentData, OutputData>;
export type LayoutLoadEvent = Parameters<LayoutLoad>[0];
export type LayoutData = Expand<Expand<LayoutParentData> & {
export type LayoutData = Expand<Expand<Omit<LayoutParentData, keyof LayoutParentData & EnsureDefined<LayoutServerData>> & OptionalUnion<EnsureDefined<LayoutParentData & EnsureDefined<LayoutServerData>>>> & {
MyLayoutQuery: MyLayoutQueryStore
}>;
`)
Expand Down
4 changes: 2 additions & 2 deletions packages/houdini-svelte/src/plugin/fsPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
16 changes: 9 additions & 7 deletions packages/houdini-svelte/src/plugin/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(.*)/
Expand Down

0 comments on commit 83d9340

Please sign in to comment.