Skip to content

Commit

Permalink
fix rest of examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamsi committed Jun 30, 2024
1 parent b7cf1ef commit 303b060
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 43 deletions.
5 changes: 0 additions & 5 deletions examples/document-field/next.config.js

This file was deleted.

16 changes: 16 additions & 0 deletions examples/document-field/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
experimental: {
// Experimental ESM Externals
// https://nextjs.org/docs/messages/import-esm-externals
// required to fix build admin ui issues related to "react-day-picker" and "date-fn"
esmExternals: 'loose',
// without this, 'Error: Expected Upload to be a GraphQL nullable type.'
serverComponentsExternalPackages: ['graphql'],
},
typescript: {
ignoreBuildErrors: true,
},
eslint: {
ignoreDuringBuilds: true,
},
}
27 changes: 14 additions & 13 deletions examples/document-field/src/app/(admin)/author/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ const author = data?.author
)
}

export async function getStaticPaths (): Promise<GetStaticPathsResult> {
const data = await fetchGraphQL(gql`
query {
authors {
id
}
}
`)
return {
paths: data.authors.map((post: any) => ({ params: { id: post.id } })),
fallback: 'blocking',
}
}
// TODO - CAN NOT use this in app router properly
// export async function getStaticPaths (): Promise<GetStaticPathsResult> {
// const data = await fetchGraphQL(gql`
// query {
// authors {
// id
// }
// }
// `)
// return {
// paths: data.authors.map((post: any) => ({ params: { id: post.id } })),
// fallback: 'blocking',
// }
// }
27 changes: 14 additions & 13 deletions examples/document-field/src/app/(admin)/post/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,17 @@ export default async function Post ({ params }: { params: any }) {
)
}

export async function getStaticPaths (): Promise<GetStaticPathsResult> {
const data = await fetchGraphQL(gql`
query {
posts {
slug
}
}
`)
return {
paths: data.posts.map((post: any) => ({ params: { slug: post.slug } })),
fallback: 'blocking',
}
}
// TODO - CAN NOT use this in app router properly
// export async function getStaticPaths (): Promise<GetStaticPathsResult> {
// const data = await fetchGraphQL(gql`
// query {
// posts {
// slug
// }
// }
// `)
// return {
// paths: data.posts.map((post: any) => ({ params: { slug: post.slug } })),
// fallback: 'blocking',
// }
// }
3 changes: 2 additions & 1 deletion packages/core/src/admin-ui/context.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { notFound } from 'next/navigation'
import React, { type ReactNode, createContext, useContext, useMemo } from 'react'
import { Center } from '@keystone-ui/core'
import { ToastProvider } from '@keystone-ui/toast'
Expand Down Expand Up @@ -164,6 +165,6 @@ export const useList = (key: string) => {
if (lists[key]) {
return lists[key]
} else {
throw new Error(`Invalid list key provided to useList: ${key}`)
return notFound()
}
}
3 changes: 2 additions & 1 deletion packages/core/src/admin-ui/system/generateAdminUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export async function generateAdminUI (
graphQLSchema: GraphQLSchema,
adminMeta: AdminMetaRootVal,
projectAdminPath: string,
hasSrc: boolean,
isLiveReload: boolean
) {
// Write out the files configured by the user
Expand All @@ -76,7 +77,7 @@ export async function generateAdminUI (

// Write out the built-in admin UI files. Don't overwrite any user-defined pages.
const configFileExists = getDoesAdminConfigExist(projectAdminPath)
let adminFiles = writeAdminFiles(config, graphQLSchema, adminMeta, configFileExists)
let adminFiles = writeAdminFiles(config, graphQLSchema, adminMeta, configFileExists, hasSrc)

adminFiles = adminFiles.filter(
x => !uniqueFiles.has(Path.normalize(Path.join(projectAdminPath, x.outputPath)))
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/admin-ui/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export const writeAdminFiles = (
config: __ResolvedKeystoneConfig,
graphQLSchema: GraphQLSchema,
adminMeta: AdminMetaRootVal,
configFileExists: boolean
configFileExists: boolean,
srcExists: boolean,
): AdminFileToWrite[] => {
const ext = config.ui?.tsx ? 'tsx' : 'js'
return [
{
mode: 'write',
src: nextConfigTemplate(config.ui?.basePath),
outputPath: '../../next.config.mjs',
src: nextConfigTemplate(),
outputPath: `${srcExists ? '../' : ''}../../next.config.mjs`,
},
{ mode: 'write', src: noAccessTemplate(config.session), outputPath: `no-access/page.${ext}` },
{ mode: 'write', src: adminLayoutTemplate(), outputPath: `layout.${ext}` },
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/admin-ui/templates/next-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const nextConfigTemplate = (basePath?: string) =>
export const nextConfigTemplate = () =>
`export default {
experimental: {
// Experimental ESM Externals
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/lib/createSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function getSystemPaths (cwd: string, config: KeystoneConfig | __Resolved
prisma: builtPrismaPath,
graphql: builtGraphqlPath,
},
hasSrc,
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function build (

console.log('✨ Generating Admin UI code')
const paths = system.getPaths(cwd)
await generateAdminUI(system.config, system.graphQLSchema, system.adminMeta, paths.admin, false)
await generateAdminUI(system.config, system.graphQLSchema, system.adminMeta, paths.admin, paths.hasSrc, false)

console.log('✨ Building Admin UI')
await nextBuild(
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export async function dev (
}

console.log('✨ Generating Admin UI code')
await generateAdminUI(system.config, system.graphQLSchema, system.adminMeta, paths.admin, false)
await generateAdminUI(system.config, system.graphQLSchema, system.adminMeta, paths.admin, paths.hasSrc, false)

console.log('✨ Preparing Admin UI app')
nextApp = next({ dev: true, dir: cwd })
Expand Down Expand Up @@ -338,7 +338,7 @@ export async function dev (
}

await generateTypes(cwd, newSystem)
await generateAdminUI(newSystem.config, newSystem.graphQLSchema, newSystem.adminMeta, paths.admin, true)
await generateAdminUI(newSystem.config, newSystem.graphQLSchema, newSystem.adminMeta, paths.admin, paths.hasSrc, true)
if (prismaClientModule) {
if (server && lastApolloServer) {
const { context: newContext } = newSystem.getKeystone(prismaClientModule)
Expand Down
4 changes: 3 additions & 1 deletion tests/admin-ui-tests/live-reloading.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ let ksProcess: ExecaChildProcess = undefined as any
let page: Page = undefined as any
let browser: Browser = undefined as any

jest.setTimeout(300000) // testing why this one fail

test('start keystone', async () => {
// just in case a previous failing test run messed things up, let's reset it
await replaceSchema('initial.ts');
Expand Down Expand Up @@ -54,7 +56,7 @@ test('Creating an item with the GraphQL API and navigating to the item page for
expect(value).toBe('blah')
})

test('api routes written with getAdditionalFiles containing [...rest] work', async () => {
test.only('api routes written with getAdditionalFiles containing [...rest] work', async () => {
expect(
await fetch('http://localhost:3000/api/blah/asdasdas/das/da/sdad').then(x => x.text())
).toEqual('something')
Expand Down
5 changes: 3 additions & 2 deletions tests/test-projects/live-reloading/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export default config({
() => [
{
mode: 'write',
src: "export default function(req,res) {res.send('something')}",
outputPath: 'pages/api/blah/[...rest].js',
src: "export async function GET() { return new Response('something')}",
overwrite: true,
outputPath: 'api/blah/[...rest]/route.js',
},
],
],
Expand Down

0 comments on commit 303b060

Please sign in to comment.