Skip to content

Commit

Permalink
Fix types of lists and db.lists APIs on KeystoneContext (#5907)
Browse files Browse the repository at this point in the history
* Fix types of `lists` and `db.lists` APIs on `KeystoneContext`

* Fix things

* Fix things
  • Loading branch information
emmatown authored Jun 21, 2021
1 parent 3b9cdc2 commit 0df3734
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
15 changes: 15 additions & 0 deletions .changeset/honest-cats-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@keystone-next/types': patch
---

Fixed `lists` and `db.lists` APIs on `KeystoneContext` to have improved types. If you're using the generated `KeystoneListsTypeInfo` type like this:

```ts
const lists: KeystoneListsAPI<KeystoneListsTypeInfo> = context.lists;
```

You will have to change it to use `as` like this:

```ts
const lists = context.lists as KeystoneListsAPI<KeystoneListsTypeInfo>;
```
4 changes: 2 additions & 2 deletions docs/pages/guides/virtual-fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default config({
field: schema.field({
async resolve(item, args, context) {
const { author } = await context.lists.Post.findOne({
where: { id: item.id },
where: { id: item.id.toString() },
query: 'author { name }',
});
return author && author.name;
Expand Down Expand Up @@ -281,7 +281,7 @@ export const lists = createSchema({
type: lists.Post.types.output,
async resolve(item, args, context) {
const { posts } = await context.lists.Author.findOne({
where: { id: item.id },
where: { id: item.id.toString() },
query: `posts(
orderBy: { publishDate: desc }
first: 1
Expand Down
2 changes: 1 addition & 1 deletion examples-staging/basic/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export const extendGraphqlSchema = graphQLSchemaExtension({
const data = Array.from({ length: 238 }).map((x, i) => ({ data: { title: `Post ${i}` } }));
// note this usage of the type is important because it tests that the generated
// KeystoneListsTypeInfo extends Record<string, BaseGeneratedListTypes>
const lists: KeystoneListsAPI<KeystoneListsTypeInfo> = context.lists;
const lists = context.lists as KeystoneListsAPI<KeystoneListsTypeInfo>;
return lists.Post.createMany({ data });
},
},
Expand Down
4 changes: 2 additions & 2 deletions examples/virtual-field/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const lists = createSchema({
type: schema.String,
async resolve(item, args, context) {
const { author } = await context.lists.Post.findOne({
where: { id: item.id },
where: { id: item.id.toString() },
query: 'author { name }',
});
return author && author.name;
Expand All @@ -99,7 +99,7 @@ export const lists = createSchema({
type: lists.Post.types.output,
async resolve(item, args, context) {
const { posts } = await context.lists.Author.findOne({
where: { id: item.id },
where: { id: item.id.toString() },
query: `posts(
orderBy: { publishDate: desc }
first: 1
Expand Down
4 changes: 2 additions & 2 deletions packages-next/types/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type { BaseGeneratedListTypes, GqlNames } from './utils';

export type KeystoneContext = {
req?: IncomingMessage;
db: { lists: KeystoneDbAPI<any> };
lists: KeystoneListsAPI<any>;
db: { lists: KeystoneDbAPI<Record<string, BaseGeneratedListTypes>> };
lists: KeystoneListsAPI<Record<string, BaseGeneratedListTypes>>;
graphql: KeystoneGraphQLAPI<any>;
sudo: () => KeystoneContext;
exitSudo: () => KeystoneContext;
Expand Down

1 comment on commit 0df3734

@vercel
Copy link

@vercel vercel bot commented on 0df3734 Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.