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

[legacy-framework] Implement ssrQuery #136

Merged
merged 1 commit into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 additions & 0 deletions examples/store/app/products/pages/products/ssr.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {Link, BlitzPage, GetServerSideProps, ssrQuery} from '@blitzjs/core'
import getProducts from 'app/products/queries/getProducts'
import {Product} from 'db'

type PageProps = {
products: Product[]
}

export const getServerSideProps: GetServerSideProps<PageProps> = async ({req, res}) => {
const products = await ssrQuery(getProducts, undefined, {req, res})

return {
props: {products},
}
}

const Page: BlitzPage<PageProps> = function ({products}) {
return (
<div>
<h1>Products</h1>
<div>
{products.map((product) => (
<p key={product.id}>
<Link href="/products/[handle]" as={`/products/${product.handle}`}>
<a>{product.name}</a>
</Link>
</p>
))}
</div>
</div>
)
}
export default Page
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export {default as Link} from 'next/link'
export {default as Router, useRouter, withRouter} from 'next/router'

export * from './useQuery'
export * from './ssrQuery'
export * from './rpc'
19 changes: 19 additions & 0 deletions packages/core/src/ssrQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {IncomingMessage, OutgoingMessage} from 'http'

type QueryFn = (...args: any) => Promise<any>

type SsrQueryContext = {
req: IncomingMessage
res: OutgoingMessage
}

export async function ssrQuery<T extends QueryFn>(
queryFn: T,
params: any,
// @ts-ignore unused
context: SsrQueryContext,
): Promise<ReturnType<T>> {
const data = await queryFn(params)

return data
}