getkirby / kql

Kirby's Query Language API combines the flexibility of Kirby's data structures, the power of GraphQL and the simplicity of REST.
https://getkirby.com
MIT License
144 stars 5 forks source link

TypeScript types #47

Open benwest opened 1 year ago

benwest commented 1 year ago

I've been making some moves on TypeScript typings for KQL queries. It's early days, but I am able to derive response types from queries pretty well already.

I'm testing and developing it in my current project, and I'm keen for anyone else to give it a try or contribute (In particular, the models.ts file is incomplete and needs filling out with the types of all Kirby's built-in stuff).

https://github.com/benwest/kql-ts

import { createClient } from "./kql/client";
import { KQLQueryData } from "./kql/query";

const query = {
  query: "site",
  select: {
    title: true,
    logo: {
      query: "site.logo.toFile",
      select: {
        srcset: true,
        width: true,
        height: true,
        placeholder: "file.resize(5).url",
      },
    },
    pages: {
      query: "site.children",
      select: {
        title: true,
        tags: `page.tags.split(',')`,
      },
    },
  },
} as const; // <- important

type Content = KQLQueryData<typeof query>;
/*   ^^^^^^^
{
  readonly title: string;
  readonly logo: {
      readonly srcset: string;
      readonly width: number;
      readonly height: number;
      readonly placeholder: string;
  } | null;
  readonly pages: {
      readonly title: string;
      readonly tags: string[];
  }[];
} */

const kql = createClient({
  user: KQL_USER,
  password: KQL_PASSWORD,
  url: KQL_URL,
});

const response = await kql(query);
if (response.status === "ok") {
  const data = response.result; // strongly typed!
}
tobimori commented 1 year ago

Just to add: There's also kirby-fest by @johannschopplich.

bastianallgeier commented 1 year ago

While this is really great, it feels like it's the wrong place as an issue. Would you mind checking out @johannschopplich's plugin? I cannot really compare the two sets of types at the moment, but if you feel like yours still adds enough unique value, we could add it to the plugins directory.

tobimori commented 1 year ago

While this is really great, it feels like it's the wrong place as an issue. Would you mind checking out @johannschopplich's plugin? I cannot really compare the two sets of types at the moment, but if you feel like yours still adds enough unique value, we could add it to the plugins directory.

I know there are some query libraries (e.g. for Nuxt) in the plugin directory already, but I don't feel like they actually fit in because they're not installable as "real" Kirby plugin?

There probably needs to be some kind of separate section or category for third party libraries that were especially designed for Kirby or stuff like the Starterkits from Johann.

Otherwise I'd leave them as mentions in this repositories' readme. (https://github.com/getkirby/kql#plugins)

bastianallgeier commented 1 year ago

You are totally right. They don't fit super well in there. I wonder if we could come up with some sort of category for the plugin directory that would make it clearer. Mentioning them in the readmes always feel a bit like we are underrepresenting those projects.

johannschopplich commented 1 year ago

👍 For a separate Kirby plugin page “integration” plugins.