new graphql pipeline

This commit is contained in:
2021-12-22 23:25:13 +01:00
parent deb89701f7
commit 44224ffb85
28 changed files with 13826 additions and 2020 deletions

View File

@@ -3,6 +3,7 @@
import { page } from '$app/stores'
import '../app.css'
import '$lib/actions/cloudinary'
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
@@ -13,7 +14,7 @@
import Progress from '$lib/components/Progress.svelte'
let wrapper: HTMLDivElement
let main: HTMLDivElement
let main: HTMLElement
function resize() {
wrapper.style.height = `${window.innerHeight}px`

View File

@@ -14,10 +14,11 @@
<script lang="ts">
import WPAdapter from '$lib/components/WPAdapter.svelte'
import SimplePage from '$lib/components/SimplePage.svelte'
import type { Page, MediaItem } from '$lib/api'
import type { GQLBaseMediaItemFragment, GQLBasePageFragment } from '$lib/gql/gen'
import { cdn } from '$lib/actions/cloudinary'
export let data: Page
export let image: MediaItem
export let data: GQLBasePageFragment
export let image: GQLBaseMediaItemFragment
</script>
<svelte:head>
@@ -27,7 +28,7 @@
<SimplePage title={data.title} expanded={false}>
{#if data.content}
<WPAdapter content={data.content} />
<img srcset={image.srcSet} alt="decoration" />
<img use:cdn={image.sourceUrl} alt="decoration" />
{/if}
</SimplePage>

View File

@@ -1,155 +1,55 @@
import type { RequestHandler } from '@sveltejs/kit'
import { SDK } from '$lib/gql'
import type { ServerRequest } from '@sveltejs/kit/types/hooks'
import {
BaseAttributes,
Call,
gql,
MediaItem,
MediaItemFragment,
Page,
Post,
PostFragment,
Project,
ProjectFragment,
WorkFragment,
} from '$lib/api'
export const get: RequestHandler = async (args) => {
export async function get(args: ServerRequest) {
const { type, slug } = args.params
const allChar = '*'
const all = slug === allChar
const all = slug === '*'
switch (type) {
case 'pages': {
if (all) {
const data = await Call<{ pages: { nodes: Page[] } }>(gql`
query {
pages(where: {status: PUBLISH}) {
nodes {
${BaseAttributes}
}
}
}
`)
return { body: data.pages.nodes }
const data = await SDK.PagesMany()
return { body: data.pages?.nodes }
} else {
const data = await Call<{ page: Page }>(
gql`
query ($slug: ID!) {
page(id: $slug, idType: URI) {
${BaseAttributes}
}
}
`,
{ slug: '/' + slug }
)
const data = await SDK.PagesOne({ slug })
return { body: data.page }
}
}
case 'works': {
if (all) {
const query = gql`
${MediaItemFragment}
${WorkFragment}
query {
works(where: { status: PUBLISH }) {
nodes {
...WorkFragment
}
}
}
`
const data = await Call<{ works: { nodes: MediaItem[] } }>(query)
return { body: data.works.nodes }
const data = await SDK.WorksMany()
return { body: data.works?.nodes }
} else {
const data = await Call<{ work: MediaItem }>(
gql`
${MediaItemFragment}
${WorkFragment}
query ($slug: ID!) {
work(id: $slug, idType: SLUG) {
...WorkFragment
}
}
`,
{ slug }
)
const data = await SDK.WorksOne({ slug })
return { body: data.work }
}
}
case 'projects': {
if (all) {
const data = await Call<{ projects: { nodes: Project[] } }>(
gql`
${ProjectFragment}
query {
projects(where: { status: PUBLISH }) {
nodes {
...ProjectFragment
}
}
}
`
)
return { body: data.projects.nodes }
const data = await SDK.ProjectsMany()
return { body: data.projects?.nodes }
} else {
const data = await Call<{ project: Project }>(
gql`
${ProjectFragment}
query ($slug: ID!) {
project(id: $slug, idType: SLUG) {
...ProjectFragment
}
}
`,
{ slug }
)
const data = await SDK.ProjectsOne({ slug })
return { body: data.project }
}
}
case 'media': {
const data = await Call<{ mediaItem: MediaItem }>(
gql`
${MediaItemFragment}
query ($slug: ID!) {
mediaItem(id: $slug, idType: SLUG) {
...MediaItemFragment
}
}
`,
{ slug }
)
return { body: data.mediaItem }
if (all) {
const data = await SDK.MediaItemsMany()
return { body: data.mediaItems?.nodes }
} else {
const data = await SDK.MediaItemsOne({ slug })
return { body: data.mediaItem }
}
}
case 'posts': {
if (all) {
const data = await Call<{ posts: { nodes: Post[] } }>(gql`
${PostFragment}
${MediaItemFragment}
{
posts(where: { status: PUBLISH }, first: 1000000000) {
nodes {
...PostFragment
}
}
}
`)
return { body: data.posts.nodes }
const data = await SDK.PostsMany()
return { body: data.posts?.nodes }
} else {
const data = await Call<{ post: Post }>(
gql`
${PostFragment}
${MediaItemFragment}
query ($slug: ID!) {
post(id: $slug, idType: SLUG) {
...PostFragment
}
}
`,
{ slug }
)
const data = await SDK.PostsOne({ slug })
return { body: data.post }
}
}

View File

@@ -1,52 +1,51 @@
import lunr from 'lunr'
import type { RequestHandler } from '@sveltejs/kit'
import { BaseAttributes, Call, gql, Page } from '$lib/api'
// import { BaseAttributes, Call, gql, Page } from '$lib/api'
function removeHTML(s: string) {
return s.replace(/<.*?>|\s+|&#\d+;/g, ' ').trim()
}
// function removeHTML(s: string) {
// return s.replace(/<.*?>|\s+|&#\d+;/g, ' ').trim()
// }
function convertForIdx(type: string, items: Page[]) {
const keys: (keyof Page)[] = ['title', 'content', 'slug']
return items.map((item) => ({
url: `${type}/${item.slug}`,
data: keys.map((field) => removeHTML(item[field] ?? '')).join(' '),
}))
}
// function convertForIdx(type: string, items: Page[]) {
// const keys: (keyof Page)[] = ['title', 'content', 'slug']
// return items.map((item) => ({
// url: `${type}/${item.slug}`,
// data: keys.map((field) => removeHTML(item[field] ?? '')).join(' '),
// }))
// }
async function getAll() {
const all = await Call<Record<'posts' | 'projects' | 'works', { nodes: Page[] }>>(gql`
query {
posts(first: 1000) {
nodes {
${BaseAttributes}
}
}
projects(first: 1000) {
nodes {
${BaseAttributes}
}
}
works(first: 1000) {
nodes {
${BaseAttributes}
}
}
}
`)
return all
}
// async function getAll() {
// const all = await Call<Record<'posts' | 'projects' | 'works', { nodes: Page[] }>>(gql`
// query {
// posts(first: 1000) {
// nodes {
// ${BaseAttributes}
// }
// }
// projects(first: 1000) {
// nodes {
// ${BaseAttributes}
// }
// }
// works(first: 1000) {
// nodes {
// ${BaseAttributes}
// }
// }
// }
// `)
// return all
// }
export const get: RequestHandler = async () => {
const all = await getAll()
const converted = Object.entries(all)
.map(([type, data]) => convertForIdx(type, data.nodes))
.flat()
export const get = async () => {
// const all = await getAll()
// const converted = Object.entries(all)
// .map(([type, data]) => convertForIdx(type, data.nodes))
// .flat()
const idx = lunr(function () {
this.ref('url')
this.field('data')
converted.forEach((doc) => this.add(doc))
// converted.forEach((doc) => this.add(doc))
})
return { body: idx }

View File

@@ -11,12 +11,12 @@
</script>
<script lang="ts">
import type { Post } from '$lib/api'
import SimplePage from '$lib/components/SimplePage.svelte'
import PostAttributes from '$lib/components/PostAttributes.svelte'
import WpAdapter from '$lib/components/WPAdapter.svelte'
import type { GQLBasePostFragment } from '$lib/gql/gen'
export let data: Post
export let data: GQLBasePostFragment
</script>
<svelte:head>

View File

@@ -12,11 +12,11 @@
</script>
<script lang="ts">
import type { Post } from '$lib/api'
import SimplePage from '$lib/components/SimplePage.svelte'
import PostPreview from '$lib/components/PostPreview.svelte'
import type { GQLBasePostFragment } from '$lib/gql/gen'
export let data: Post[]
export let data: GQLBasePostFragment[]
</script>
<svelte:head>

View File

@@ -1,10 +1,10 @@
<script lang="ts" context="module">
import type { MediaItem } from '$lib/api'
import type { GQLBaseMediaItemFragment } from '$lib/gql/gen'
type Data = Record<'signature' | 'home', MediaItem>
type Data = Record<'signature' | 'home', GQLBaseMediaItemFragment>
export const load: Load = async ({ fetch }) => {
const signature: MediaItem = await fetch('/api/media/signature.json').then((r) => r.json())
const home: MediaItem = await fetch('/api/media/home.json').then((r) => r.json())
const signature: GQLBaseMediaItemFragment = await fetch('/api/media/signature.json').then((r) => r.json())
const home: GQLBaseMediaItemFragment = await fetch('/api/media/home.json').then((r) => r.json())
return { props: { data: { signature, home } } }
}
</script>
@@ -12,6 +12,7 @@
<script lang="ts">
import SpacedLetters from '$lib/components/SpacedLetters.svelte'
import type { Load } from '@sveltejs/kit'
import { cdn } from '$lib/actions/cloudinary'
export let data: Data
</script>

View File

@@ -13,9 +13,9 @@
<script lang="ts">
import WPAdapter from '$lib/components/WPAdapter.svelte'
import SimplePage from '$lib/components/SimplePage.svelte'
import type { Page } from '$lib/api'
import type { GQLBasePageFragment } from '$lib/gql/gen'
export let data: Page
export let data: GQLBasePageFragment
</script>
<svelte:head>

View File

@@ -11,11 +11,11 @@
</script>
<script lang="ts">
import type { Project as TProject } from '$lib/api'
import SimplePage from '$lib/components/SimplePage.svelte'
import Project from '$lib/components/Project.svelte'
import type { GQLBaseProjectFragment } from '$lib/gql/gen'
export let data: TProject[]
export let data: GQLBaseProjectFragment[]
</script>
<svelte:head>

View File

@@ -11,11 +11,11 @@
</script>
<script lang="ts">
import type { Work as TWork } from '$lib/api'
import SimplePage from '$lib/components/SimplePage.svelte'
import Work from '$lib/components/Work.svelte'
import type { GQLBaseWorkFragment } from '$lib/gql/gen'
export let data: TWork
export let data: GQLBaseWorkFragment
</script>
<svelte:head>

View File

@@ -11,11 +11,11 @@
</script>
<script lang="ts">
import type { Work as TWork } from '$lib/api'
import type { GQLBaseWorkFragment } from '$lib/gql/gen'
import SimplePage from '$lib/components/SimplePage.svelte'
import Work from '$lib/components/Work.svelte'
export let data: TWork[]
export let data: GQLBaseWorkFragment[]
</script>
<svelte:head>