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

Add examples/reuse example, showing how to add fields to Lists in a re-usable way #8837

Merged
merged 7 commits into from
Sep 26, 2023
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
5 changes: 5 additions & 0 deletions .changeset/add-field-hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/core": minor
---

Add type `FieldHooks` to `@keystone-6/core/types` exports
2 changes: 1 addition & 1 deletion .changeset/fix-id-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@keystone-6/core": patch
---

Fixes `Input error: only a int can be passed to id filters` for AdminUI
Fix `Input error: only a int can be passed to id filters` for AdminUI
2 changes: 1 addition & 1 deletion .changeset/odd-lemons-hide.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@keystone-6/core': patch
---

Fixes `hooks.validateInput` argument types for update operations
Fix `hooks.validateInput` argument types for update operations
2 changes: 1 addition & 1 deletion .changeset/strong-swans-destroy.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@keystone-6/core': patch
---

Removed deprecated `experimental.appDir` flag from generated next.config
Remove deprecated `experimental.appDir` flag from generated next.config
10 changes: 5 additions & 5 deletions examples/default-values/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## Feature Example - Default Values

This project demonstrates how to use default values for fields.
It builds on the [Task Manager](../task-manager) starter project.
This example demonstrates how to use default values for fields.

This example builds on the [TODO](../usecase-todo) starter project.

## Instructions

Expand All @@ -11,10 +12,9 @@ To run this project, clone the Keystone repository locally, run `pnpm` at the ro
pnpm dev
```

This will start the Admin UI at [localhost:3000](http://localhost:3000).
You can use the Admin UI to create items in your database.
This will start Keystone’s Admin UI at [localhost:3000](http://localhost:3000), where you can add items to an empty database.

You can also access a GraphQL Playground at [localhost:3000/api/graphql](http://localhost:3000/api/graphql), which allows you to directly run GraphQL queries and mutations.
When `NODE_ENV` is not equal to `production`, by default you can play with the GraphQL Playground at [localhost:3000/api/graphql](http://localhost:3000/api/graphql).

## Features

Expand Down
1 change: 1 addition & 0 deletions examples/default-values/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ type Person {

input PersonWhereUniqueInput {
id: ID
name: String
}

input PersonWhereInput {
Expand Down
2 changes: 1 addition & 1 deletion examples/default-values/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ model Task {

model Person {
id String @id @default(cuid())
name String @default("")
name String @unique @default("")
tasks Task[] @relation("Task_assignedTo")
}
2 changes: 1 addition & 1 deletion examples/default-values/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const lists: Lists = {
Person: list({
access: allowAll,
fields: {
name: text({ validation: { isRequired: true } }),
name: text({ validation: { isRequired: true }, isIndexed: 'unique' }),
tasks: relationship({ ref: 'Task.assignedTo', many: true }),
},
}),
Expand Down
6 changes: 3 additions & 3 deletions examples/hooks/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ const readOnly = {
},
ui: {
createView: {
fieldMode: (args: unknown) => 'hidden' as const,
fieldMode: () => 'hidden' as const,
},
itemView: {
fieldMode: (args: unknown) => 'read' as const,
fieldMode: () => 'read' as const,
},
listView: {
fieldMode: (args: unknown) => 'read' as const,
fieldMode: () => 'read' as const,
},
},
};
Expand Down
22 changes: 22 additions & 0 deletions examples/reuse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Base Project - Reuse

This example demonstrates how to reuse lists, fields, hooks and other functions.
Navigatingn the types of these primitives can be difficult without experience, so hopefully this project helps you understand how you can apply this to your project.

Reuse is not encouraged typically, but it can be helpful in projects that have grown beyond having everything inline.

## Instructions

To run this project, clone the Keystone repository locally, run `pnpm` at the root of the repository then navigate to this directory and run:

```shell
pnpm dev
```

This will start Keystone’s Admin UI at [localhost:3000](http://localhost:3000), where you can add items to an empty database.

When `NODE_ENV` is not equal to `production`, by default you can play with the GraphQL Playground at [localhost:3000/api/graphql](http://localhost:3000/api/graphql).

## Try it out in CodeSandbox 🧪

You can play with this example online in a web browser using the free [codesandbox.io](https://codesandbox.io/) service. To launch this example, open the URL <https://githubbox.com/keystonejs/keystone/tree/main/examples/reuse>. You can also fork this sandbox to make your own changes.
14 changes: 14 additions & 0 deletions examples/reuse/keystone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { config } from '@keystone-6/core';
import { fixPrismaPath } from '../example-utils';
import { lists } from './schema';

export default config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',

// WARNING: this is only needed for our monorepo examples, don't do this
...fixPrismaPath,
},
lists,
});
20 changes: 20 additions & 0 deletions examples/reuse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@keystone-6/example-reuse",
"version": "0.0.1",
"private": true,
"license": "MIT",
"scripts": {
"dev": "keystone dev",
"start": "keystone start",
"build": "keystone build",
"postinstall": "keystone postinstall"
},
"dependencies": {
"@keystone-6/core": "^5.0.0",
"@prisma/client": "^4.16.2"
},
"devDependencies": {
"prisma": "^4.16.2",
"typescript": "~5.0.0"
}
}
7 changes: 7 additions & 0 deletions examples/reuse/sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"template": "node",
"container": {
"startScript": "keystone dev",
"node": "18"
}
}
Loading