houdini icon indicating copy to clipboard operation
houdini copied to clipboard

Preprocessor with external files

Open AlecAivazis opened this issue 3 years ago • 5 comments

At the moment, if a user wants to use the preprocessor to generate a load function they must define their queries inline and use the appropriate "inline" function for the runtime. It would be nice to be able to somehow tell the preprocessor to generate a load function for a store.

An example api:

<script context="module">
    import ViewerProfileStore from '$houdini/stores/ViewerProfile'

    export const routeStore = ViewerProfileStore
</script>

{$ViewerProfileStore.data.viewerProfile.firstName}

AlecAivazis avatar Jun 11 '22 04:06 AlecAivazis

I put my notes here, because I want to explore this later 👍

Inline

It relies on TemplateExpression prefixed with graphql

<script>
    import { query, graphql } from '$houdini'

    const { data } = query(graphql`
        query AllTodoItems {
            items {
                text
            }
        }
    `)
</script>

External

With:

<script>
    import { GQL_AllTodoItems } from '$houdini'

    $: browser && GQL_AllTodoItems.fetch()
</script>

How to identify a store? 1.0 prefix GQL_ 2.1 method .fetch(

  • It's a query
  • having flag SSR?
  • having flag Blocking
  • No need context
  • No need event

2.2 method .mutate(

  • It's a mutation
  • No need context

...

jycouet avatar Jun 19 '22 19:06 jycouet

unforunately matching the GQL_ prefix doesn't work for users that are importing stores directly or creating their own. If we had some kind of module-level variable that we could look for, we could just generate the code to reference that variable so we wouldn't have to be very clever

AlecAivazis avatar Jun 20 '22 07:06 AlecAivazis

I'm not sure what you mean by module-level variable?

Anycase, we need a mechanism to say "Hey, on this store please do this and that preprocessor part". Like an Opti-In preprocessor function.

I think we need a bit of usage to see in what direction to go.

jycouet avatar Jul 04 '22 22:07 jycouet

i meant something like this 👇 the important part being that the variable is exported from the module context. SvelteKit uses this pattern in a few places as a way to provide configuration for the route (for example, when disabling prerendering).

<script context="module">
    import ViewerProfileStore from '$houdini/stores/ViewerProfile'

    export const routeStore = ViewerProfileStore
</script>

The preprocessor would just have to look for this variable and if its defined, it could generate the necessary logic. Because its a fixed name that acts as the API, the generated code does not have to figure out what name to use and instead can just generate code using routeStores

AlecAivazis avatar Jul 04 '22 23:07 AlecAivazis

Just a heads up for people, support for this will be added as part of the upcoming work to support the new KitQL api 👍

AlecAivazis avatar Aug 01 '22 21:08 AlecAivazis