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

Component query types #610

Merged
merged 9 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
failing tests
  • Loading branch information
AlecAivazis committed Oct 18, 2022
commit 0c1310b032793b823642a1dfd808a7dd17863707
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import path from 'path'
import { test, expect } from 'vitest'

import generate from '..'
import { type_route_dir } from '../../kit'

const config = testConfig()
const plugin_root = config.pluginDirectory('test-plugin')

test('generates type defintions for non-route components in their local ./$houdini', async function () {
const component_dir = path.join(process.cwd(), 'src', 'routes', 'foo.svelte')
const component_dir = path.join(process.cwd(), 'src', 'lib', 'foo.svelte')

await fs.mock({
[component_dir]: `
Expand All @@ -27,7 +26,7 @@ test('generates type defintions for non-route components in their local ./$houdi

// load the contents of the file
const typedef = await fs.readFile(
path.join(path.join(config.typeRootDir, 'src', 'routes', '$houdini.d.ts'))
path.join(path.join(config.typeRootDir, 'src', 'lib', '$houdini.d.ts'))
)
expect(typedef).toBeTruthy()

Expand All @@ -43,6 +42,6 @@ test('generates type defintions for non-route components in their local ./$houdi

export type PageData = {
MyInlineQuery2: MyInlineQuery2Store
};
};a
`)
})
35 changes: 34 additions & 1 deletion packages/houdini-svelte/src/plugin/codegen/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
import { GenerateHookInput } from 'houdini'
import { promisify } from 'util'

export default async function componentTypesGenerator({ config }: GenerateHookInput) {}
import { Framework } from '../../kit'

export default async function componentTypesGenerator(
framework: Framework,
{ config }: GenerateHookInput
) {
// in order to generate types for the component queries in the project we need to:
// - look at all of the files included in the project
// - in kit, exclude the route directory
// - group the files by directory
// - look for inline queries for every file in the directory and generate ./$houdini
let matches = await config.sourceFiles()

// if we are in kit, don't consider the source directory
if (framework === 'kit') {
matches = matches.filter((match) => !match.startsWith(config.routesDir))
}

// group the files by directory
const files: { [filename: string]: { queries: string[]; props: {} } } = {}

// put every file we found in the right place
for (const file of matches) {
// walk down the path
let target = files
const parts = file.split('/')
for (const [i, path] of parts.entries()) {
// if we are
if (!target[path]) {
}
}
}
}
8 changes: 1 addition & 7 deletions packages/houdini-svelte/src/plugin/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { GenerateHookInput, fs, Config } from 'houdini'

import { stores_directory, type_route_dir } from '../kit'
import adapter from './adapter'
import components from './components'
import kit from './routes'
import stores from './stores'

Expand All @@ -14,12 +13,7 @@ export default async function (input: PluginGenerateInput) {
])

// generate the files
await Promise.all([
adapter(input),
kit(input.framework, input),
stores(input),
components(input),
])
await Promise.all([adapter(input), kit(input.framework, input), stores(input.framework, input)])
}

export type PluginGenerateInput = Omit<GenerateHookInput, 'config'> & {
Expand Down
2 changes: 2 additions & 0 deletions packages/houdini/src/codegen/generators/indexFile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export default async function writeIndexFile(config: Config, docs: CollectedGrap
export_default_as,
export_star_from,
plugin_root: config.pluginDirectory(plugin.name),
typedef: false,
documents: docs,
})
}

Expand Down
2 changes: 2 additions & 0 deletions packages/houdini/src/codegen/generators/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export default async function typescriptGenerator(
export_default_as,
export_star_from,
plugin_root: config.pluginDirectory(plugin.name),
typedef: true,
documents: docs,
})

// if the plugin generated a runtime
Expand Down
27 changes: 1 addition & 26 deletions packages/houdini/src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,32 +168,7 @@ export async function runPipeline(config: Config, docs: CollectedGraphQLDocument

async function collectDocuments(config: Config): Promise<CollectedGraphQLDocument[]> {
// the first step we have to do is grab a list of every file in the source tree
let sourceFiles = [
...new Set(
(
await Promise.all(
config.include.map((filepath) =>
promisify(glob)(path.join(config.projectRoot, filepath))
)
)
)
.flat()
.filter((filepath) => config.includeFile(filepath))
// don't include the schema path as a source file
.filter((filepath) => {
const prefix = config.schemaPath?.startsWith('./') ? './' : ''

return (
!config.schemaPath ||
!minimatch(
prefix +
path.relative(config.projectRoot, filepath).replaceAll('\\', '/'),
config.schemaPath
)
)
})
),
]
let sourceFiles = await config.sourceFiles()

// the list of documents we found
const documents: DiscoveredDoc[] = []
Expand Down
31 changes: 31 additions & 0 deletions packages/houdini/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,35 @@ export class Config {
)
}

async sourceFiles() {
return [
...new Set(
(
await Promise.all(
this.include.map((filepath) =>
promisify(glob)(path.join(this.projectRoot, filepath))
)
)
)
.flat()
.filter((filepath) => this.includeFile(filepath))
// don't include the schema path as a source file
.filter((filepath) => {
const prefix = this.schemaPath?.startsWith('./') ? './' : ''

return (
!this.schemaPath ||
!minimatch(
prefix +
path.relative(this.projectRoot, filepath).replaceAll('\\', '/'),
this.schemaPath
)
)
})
),
]
}

/*
Directory structure
Expand Down Expand Up @@ -900,6 +929,8 @@ type ModuleIndexTransform = (arg: {
export_default_as(args: { module: string; as: string }): string
export_star_from(args: { module: string }): string
plugin_root: string
typedef: boolean
documents: CollectedGraphQLDocument[]
}) => string

export type GenerateHook = (args: GenerateHookInput) => Promise<void> | void
Expand Down