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

Update the types in examples #7743

Merged
merged 8 commits into from
Jul 28, 2022
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
6 changes: 3 additions & 3 deletions docs/pages/docs/apis/access-control.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ If access is **denied** due to any of the access control methods, the following
Operation-level access control lets you control which operations can be accessed by a user based on the `session` and `context`.
Individual functions can be provided for each of the operations.

?> Each operation type *defaults to public* unless configured.
?> Each operation type _defaults to public_ unless configured.

!> These functions must return `true` if the operation is allowed, or `false` if it is not allowed.

Expand Down Expand Up @@ -93,7 +93,7 @@ Filter-level access control lets you restrict which items can be operated on by
In general, the filter access control functions will return GraphQL filters.
They can also return boolean values `true` or `false` to match or exclude all items.

?> Each `filter` type *defaults to an empty filter*, unless configured.
?> Each `filter` type _defaults to an empty filter_, unless configured.

```typescript
import { config, list } from '@keystone-6/core';
Expand Down Expand Up @@ -138,7 +138,7 @@ Individual functions can be provided for each of the operations, with each funct
- the **input data** of the mutation (for `create` and `update` operations), and/or
- the **existing item** in the database (for `update` and `delete` operations).

?> Each `item` access type *defaults to public* unless configured.
?> Each `item` access type _defaults to public_ unless configured.

!> These functions must return `true` if the operation is allowed, or `false` if it is not allowed.

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/docs/apis/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ S3 options:
- `pathPrefix`: The prefix for the file, used to set the subfolder of your bucket files will be stored in.
- `endpoint`: The endpoint to use - if provided, this endpoint will be used instead of the default amazon s3 endpoint
- `forcePathStyle`: Force the old pathstyle of using the bucket name after the host
- `signed.expiry`: Use S3 URL signing to keep S3 assets private. `expiry` is in seconds
- `signed.expiry`: Use S3 URL signing to keep S3 assets private. `expiry` is in seconds

```typescript
import { config } from '@keystone-6/core';
Expand Down
1 change: 1 addition & 0 deletions docs/pages/releases/2022-06-30.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Keystone now supports MySQL by setting `mysql` in your `db.provider` [see pull r
- Fixes the generation of an invalid Prisma schema when `{field}.isIndexed: true` and `{field}.db.map` are set - [#7666](https://github.com/keystonejs/keystone/pull/7666) Thanks [@TonnyORG](https://github.com/TonnyORG)!

## Acknowledgements <Emoji symbol="💙" alt="Blue heart" />

Big shoutout to the following community members for their help in improving our documentation with their contributions:

- [#7665](https://github.com/keystonejs/keystone/pull/7665) - [@EvonuX](https://github.com/EvonuX)
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/why-keystone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ export default function WhyKeystonePage() {
</Type>
<Type as="p" look="body18">
<a
href="https://github.com/keystonejs/keystone/tree/main/examples-staging/ecommerce"
href="https://github.com/keystonejs/keystone/tree/main/examples/ecommerce"
target="_blank"
rel="noopener noreferrer"
>
Expand Down
9 changes: 0 additions & 9 deletions examples-staging/README.md

This file was deleted.

9 changes: 0 additions & 9 deletions examples-staging/ecommerce/lib/formatMoney.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"engines": {
"node": "^14.15 || ^16.13"
},
"repository": "https://github.com/keystonejs/keystone/tree/main/examples-staging/auth"
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/auth"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
"engines": {
"node": "^14.15 || ^16.13"
},
"repository": "https://github.com/keystonejs/keystone/tree/main/examples-staging/basic"
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/basic"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion examples/blog/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { config } from '@keystone-6/core';
import { lists } from './schema';
import { insertSeedData } from './seed-data';
import { Context } from '.keystone/types';

export default config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
async onConnect(context) {
async onConnect(context: Context) {
if (process.argv.includes('--seed-data')) {
await insertSeedData(context);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/seed-data/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ export const posts = [
content:
'Dorothy lived in the midst of the great Kansas prairies, with Uncle Henry, who was a farmer, and Aunt Em, who was the farmer’s wife. Their house was small, for the lumber to build it had to be carried by wagon many miles. There were four walls, a floor and a roof, which made one room; and this room contained a rusty looking cookstove, a cupboard for the dishes, a table, three or four chairs, and the beds. Uncle Henry and Aunt Em had a big bed in one corner, and Dorothy a little bed in another corner. There was no garret at all, and no cellar—except a small hole dug in the ground, called a cyclone cellar, where the family could go in case one of those great whirlwinds arose, mighty enough to crush any building in its path. It was reached by a trap door in the middle of the floor, from which a ladder led down into the small, dark hole.',
},
];
] as const;
11 changes: 5 additions & 6 deletions examples/blog/seed-data/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeystoneContext } from '@keystone-6/core/types';
import { authors, posts } from './data';
import { Context } from '.keystone/types';

type AuthorProps = {
name: string;
Expand All @@ -8,13 +8,13 @@ type AuthorProps = {

type PostProps = {
title: string;
status: string;
status: 'draft' | 'published';
publishDate: string;
author: Object;
author: string;
content: string;
};

export async function insertSeedData(context: KeystoneContext) {
export async function insertSeedData(context: Context) {
console.log(`🌱 Inserting seed data`);

const createAuthor = async (authorData: AuthorProps) => {
Expand All @@ -37,9 +37,8 @@ export async function insertSeedData(context: KeystoneContext) {
query: 'id',
});

postData.author = { connect: { id: authors[0].id } };
await context.query.Post.createOne({
data: postData,
data: { ...postData, author: { connect: { id: authors[0].id } } },
query: 'id',
});
};
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-session-validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ export default myAuth(

## 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/custom-session-validation. You can also fork this sandbox to make your own changes.
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/custom-session-validation>. You can also fork this sandbox to make your own changes.
3 changes: 2 additions & 1 deletion examples/default-values/schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { list } from '@keystone-6/core';
import { checkbox, relationship, text, timestamp } from '@keystone-6/core/fields';
import { select } from '@keystone-6/core/fields';
import { Lists } from '.keystone/types';

export const lists = {
export const lists: Lists = {
Task: list({
fields: {
label: text({ validation: { isRequired: true } }),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { KeystoneContext } from '@keystone-6/core/types';
import { Session } from '../types';
import { Context } from '.keystone/types';

async function addToCart(
root: any,
{ productId }: { productId: string },
context: KeystoneContext
context: Context
): Promise<any> {
console.log('ADDING TO CART!');
// 1. Query the current user see if they are signed in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeystoneContext } from '@keystone-6/core/types';
import { Context } from '.keystone/types';

// import stripeConfig from '../lib/stripe';

Expand All @@ -8,7 +8,7 @@ interface Arguments {
token: string;
}

async function checkout(root: any, { token }: Arguments, context: KeystoneContext): Promise<any> {
async function checkout(root: any, { token }: Arguments, context: Context): Promise<any> {
// 1. Make sure they are signed in
const userId = context.session.itemId;
if (!userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
"engines": {
"node": "^14.15 || ^16.13"
},
"repository": "https://github.com/keystonejs/keystone/tree/main/examples-staging/ecommerce"
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/ecommerce"
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { integer, text, relationship, virtual } from '@keystone-6/core/fields';
import { list, graphql } from '@keystone-6/core';
import { isSignedIn, rules } from '../access';
import formatMoney from '../lib/formatMoney';
import { Lists } from '.keystone/types';

export const Order = list({
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});

export default function formatMoney(cents: number | null) {
if (cents === null) return 'Unset';
const dollars = cents / 100;
return formatter.format(dollars);
}

// we add the type here, so `item` below is typed
export const Order: Lists.Order = list({
access: {
operation: {
create: isSignedIn,
Expand All @@ -17,7 +29,7 @@ export const Order = list({
field: graphql.field({
type: graphql.String,
resolve(item) {
return `${formatMoney((item as any).total)}`;
return formatMoney(item.total);
},
}),
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { KeystoneContext } from '@keystone-6/core/types';
import { setupTestRunner } from '@keystone-6/core/testing';
import config from '../keystone';
import { Context } from '.keystone/types';

const FAKE_ID = 'cinjfgbkjnfg';

const asUser = (context: KeystoneContext, itemId?: string) =>
context.withSession({ itemId, data: {} });

const runner = setupTestRunner({ config });
const asUser = (context: Context, itemId?: string) => context.withSession({ itemId, data: {} });
const runner = setupTestRunner<Context>({ config });

describe(`Custom mutations`, () => {
describe('checkout(token)', () => {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"engines": {
"node": "^14.15 || ^16.13"
},
"repository": "https://github.com/keystonejs/keystone/tree/main/examples-staging/embedded-nextjs"
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/embedded-nextjs"
}
16 changes: 11 additions & 5 deletions examples/extend-graphql-schema-graphql-ts/custom-schema.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { graphql } from '@keystone-6/core';
import { Context } from '.keystone/types';

export const extendGraphqlSchema = graphql.extend(base => {
const Statistics = graphql.object<{ authorId: string }>()({
name: 'Statistics',
fields: {
draft: graphql.field({
type: graphql.Int,
resolve({ authorId }, args, context) {
resolve({ authorId }, args, _context) {
const context = _context as Context;
return context.query.Post.count({
where: { author: { id: { equals: authorId } }, status: { equals: 'draft' } },
});
},
}),
published: graphql.field({
type: graphql.Int,
resolve({ authorId }, args, context) {
resolve({ authorId }, args, _context) {
const context = _context as Context;
return context.query.Post.count({
where: { author: { id: { equals: authorId } }, status: { equals: 'published' } },
});
},
}),
latest: graphql.field({
type: base.object('Post'),
async resolve({ authorId }, args, context) {
async resolve({ authorId }, args, _context) {
const context = _context as Context;
const [post] = await context.db.Post.findMany({
take: 1,
orderBy: { publishDate: 'desc' },
Expand All @@ -40,7 +44,8 @@ export const extendGraphqlSchema = graphql.extend(base => {
// with the name provided or throw if it doesn't exist
type: base.object('Post'),
args: { id: graphql.arg({ type: graphql.nonNull(graphql.ID) }) },
resolve(source, { id }, context) {
resolve(source, { id }, _context) {
const context = _context as Context;
// Note we use `context.db.Post` here as we have a return type
// of Post, and this API provides results in the correct format.
// If you accidentally use `context.query.Post` here you can expect problems
Expand All @@ -59,7 +64,8 @@ export const extendGraphqlSchema = graphql.extend(base => {
id: graphql.arg({ type: graphql.nonNull(graphql.ID) }),
days: graphql.arg({ type: graphql.nonNull(graphql.Int), defaultValue: 7 }),
},
resolve(source, { id, days }, context) {
resolve(source, { id, days }, _context) {
const context = _context as Context;
// Create a date string <days> in the past from now()
const cutoff = new Date(
new Date().setUTCDate(new Date().getUTCDate() - days)
Expand Down
2 changes: 1 addition & 1 deletion examples/extend-graphql-schema-nexus/nexus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export const nexusSchema = makeSchema({
// Keystone `db` and `query` args, as well as the prisma client
contextType: {
module: path.join(process.cwd(), 'node_modules', '.keystone', 'types.d.ts'),
export: 'KeystoneContext',
export: 'Context',
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"@keystone-6/core": "^2.1.0",
"@keystone-6/fields-document": "^4.0.0"
},
"repository": "https://github.com/keystonejs/keystone/tree/main/examples-staging/graphql-api-endpoint"
"repository": "https://github.com/keystonejs/keystone/tree/main/examples/graphql-api-endpoint"
}
4 changes: 3 additions & 1 deletion examples/rest-api/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { config } from '@keystone-6/core';
import { lists } from './schema';
import { insertSeedData } from './seed-data';
import { getTasks } from './routes/tasks';
import { Context } from '.keystone/types';

/*
A quick note on types: normally if you're adding custom properties to your
Expand All @@ -14,7 +15,7 @@ export default config({
db: {
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
async onConnect(context) {
async onConnect(context: Context) {
if (process.argv.includes('--seed-data')) {
await insertSeedData(context);
}
Expand All @@ -35,6 +36,7 @@ export default config({
(req as any).context = await createContext(req, res);
next();
});

app.get('/rest/tasks', getTasks);
},
},
Expand Down
5 changes: 3 additions & 2 deletions examples/rest-api/routes/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Request, Response } from 'express';
import type { KeystoneContext } from '@keystone-6/core/types';
import type { Context } from '.keystone/types';

/*
This example route handler gets all the tasks in the database and returns
Expand All @@ -13,7 +13,8 @@ import type { KeystoneContext } from '@keystone-6/core/types';

export async function getTasks(req: Request, res: Response) {
// This was added by the context middleware in ../keystone.ts
const context = (req as any).context as KeystoneContext;
const { context } = req as typeof req & { context: Context };

// Let's map the `complete` query param to a where filter
let isComplete;
if (req.query.complete === 'true') {
Expand Down
Loading