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

Rename .keystone/types to .keystone and add graphql export to .keystone #7733

Closed
wants to merge 7 commits into from
Closed
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/bright-pots-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': minor
---

Renames `.keystone/types` to `.keystone` and adds `graphql` export to `.keystone` that is the same as the `graphql` export from `@keystone-6/core` but uses the specific generated context type for the project
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ To create and edit blog records in Keystone’s Admin UI, add a `keystone.ts` [c

import { config, list } from '@keystone-6/core';
import { text } from '@keystone-6/core/fields';
import { Lists } from '.keystone/types';
import { Lists } from '.keystone';

const Post: Lists.Post = list({
fields: {
Expand Down Expand Up @@ -186,7 +186,7 @@ import Link from 'next/link';

// Import the generated Lists API and types from Keystone
import { query } from '.keystone/api';
import { Lists } from '.keystone/types';
import { Lists } from '.keystone';

type Post = {
id: string;
Expand Down Expand Up @@ -236,7 +236,7 @@ import { GetStaticPathsResult, GetStaticPropsContext, InferGetStaticPropsType }
import Link from 'next/link';

import { query } from '.keystone/api';
import { Lists } from '.keystone/types';
import { Lists } from '.keystone';

type Post = {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '@keystone-6/core/fields';
import { document } from '@keystone-6/fields-document';
import { v4 } from 'uuid';
import * as Keystone from '.keystone/types';
import * as Keystone from '.keystone';

type AccessArgs = {
session?: {
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { config } from '@keystone-6/core';
import { lists } from './schema';
import { insertSeedData } from './seed-data';
import { Context } from '.keystone/types';
import { Context } from '.keystone';

export default config({
db: {
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/seed-data/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { authors, posts } from './data';
import { Context } from '.keystone/types';
import { Context } from '.keystone';

type AuthorProps = {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion examples/default-values/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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';
import { Lists } from '.keystone';

export const lists: Lists = {
Task: list({
Expand Down
2 changes: 1 addition & 1 deletion examples/ecommerce/mutations/addToCart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Session } from '../types';
import { Context } from '.keystone/types';
import { Context } from '.keystone';

async function addToCart(
root: any,
Expand Down
2 changes: 1 addition & 1 deletion examples/ecommerce/mutations/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context } from '.keystone/types';
import { Context } from '.keystone';

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

Expand Down
2 changes: 1 addition & 1 deletion examples/ecommerce/schemas/Order.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { integer, text, relationship, virtual } from '@keystone-6/core/fields';
import { list, graphql } from '@keystone-6/core';
import { isSignedIn, rules } from '../access';
import { Lists } from '.keystone/types';
import { Lists } from '.keystone';

const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
Expand Down
2 changes: 1 addition & 1 deletion examples/ecommerce/tests/mutations.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { setupTestRunner } from '@keystone-6/core/testing';
import config from '../keystone';
import { Context } from '.keystone/types';
import { Context } from '.keystone';

const FAKE_ID = 'cinjfgbkjnfg';

Expand Down
11 changes: 2 additions & 9 deletions examples/ecommerce/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { KeystoneListsAPI } from '@keystone-6/core/types';

// NOTE -- these types are commented out in main because they aren't generated by the build (yet)
// To get full List and GraphQL API type support, uncomment them here and use them below
// import type { KeystoneListsTypeInfo } from '.keystone/types';

import type { Permission } from './schemas/fields';
export type { Permission } from './schemas/fields';

export type { Permission };

export type Session = {
itemId: string;
Expand All @@ -21,8 +16,6 @@ export type Session = {
};
};

export type ListsAPI = KeystoneListsAPI<any /* KeystoneListsTypeInfo */>;

export type AccessArgs = {
session?: Session;
item?: any;
Expand Down
92 changes: 0 additions & 92 deletions examples/extend-graphql-schema-graphql-ts/custom-schema.ts

This file was deleted.

3 changes: 1 addition & 2 deletions examples/extend-graphql-schema-graphql-ts/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { config } from '@keystone-6/core';
import { lists } from './schema';
import { extendGraphqlSchema } from './custom-schema';
import { lists, extendGraphqlSchema } from './schema';

export default config({
db: {
Expand Down
88 changes: 87 additions & 1 deletion examples/extend-graphql-schema-graphql-ts/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { list } from '@keystone-6/core';
import { select, relationship, text, timestamp } from '@keystone-6/core/fields';
import { Lists, graphql } from '.keystone';

export const lists = {
export const lists: Lists = {
Post: list({
fields: {
title: text({ validation: { isRequired: true } }),
Expand All @@ -25,3 +26,88 @@ export const lists = {
},
}),
};

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) {
return context.query.Post.count({
where: { author: { id: { equals: authorId } }, status: { equals: 'draft' } },
});
},
}),
published: graphql.field({
type: graphql.Int,
resolve({ authorId }, args, 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) {
const [post] = await context.db.Post.findMany({
take: 1,
orderBy: { publishDate: 'desc' },
where: { author: { id: { equals: authorId } } },
});
return post;
},
}),
},
});
return {
mutation: {
publishPost: graphql.field({
// base.object will return an object type from the existing schema
// 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) {
// 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
// when accessing the fields in your GraphQL client.
return context.db.Post.updateOne({
where: { id },
data: { status: 'published', publishDate: new Date().toISOString() },
});
},
}),
},
query: {
recentPosts: graphql.field({
type: graphql.list(graphql.nonNull(base.object('Post'))),
args: {
id: graphql.arg({ type: graphql.nonNull(graphql.ID) }),
days: graphql.arg({ type: graphql.nonNull(graphql.Int), defaultValue: 7 }),
},
resolve(source, { id, days }, context) {
// Create a date string <days> in the past from now()
const cutoff = new Date(
new Date().setUTCDate(new Date().getUTCDate() - days)
).toISOString();

// 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
// when accessing the fields in your GraphQL client.
return context.db.Post.findMany({
where: { author: { id: { equals: id } }, publishDate: { gt: cutoff } },
});
},
}),
stats: graphql.field({
type: Statistics,
args: { id: graphql.arg({ type: graphql.nonNull(graphql.ID) }) },
resolve(source, { id }) {
return { authorId: id };
},
}),
},
};
});
2 changes: 1 addition & 1 deletion examples/extend-graphql-schema/custom-schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { graphQLSchemaExtension } from '@keystone-6/core';
import { Context } from '.keystone/types';
import { Context } from '.keystone';

export const extendGraphqlSchema = graphQLSchemaExtension<Context>({
typeDefs: `
Expand Down
2 changes: 1 addition & 1 deletion examples/rest-api/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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';
import { Context } from '.keystone';

/*
A quick note on types: normally if you're adding custom properties to your
Expand Down
2 changes: 1 addition & 1 deletion 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 { Context } from '.keystone/types';
import type { Context } from '.keystone';

/*
This example route handler gets all the tasks in the database and returns
Expand Down
2 changes: 1 addition & 1 deletion examples/rest-api/seed-data/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { persons, tasks } from './data';
import { Context } from '.keystone/types';
import { Context } from '.keystone';

type PersonProps = {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion examples/task-manager/keystone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { config } from '@keystone-6/core';
import { lists } from './schema';
import { insertSeedData } from './seed-data';
import { Context } from '.keystone/types';
import { Context } from '.keystone';

export default config({
db: {
Expand Down
2 changes: 1 addition & 1 deletion examples/task-manager/seed-data/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { persons, tasks } from './data';
import { Context } from '.keystone/types';
import { Context } from '.keystone';

type PersonProps = {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion examples/testing/example.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { setupTestEnv, setupTestRunner, TestEnv } from '@keystone-6/core/testing';
import config from './keystone';
import { Context } from '.keystone/types';
import { Context } from '.keystone';

// Setup a test runner which will provide a clean test environment
// with access to our GraphQL API for each test.
Expand Down
2 changes: 1 addition & 1 deletion examples/testing/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { list } from '@keystone-6/core';
import { checkbox, password, relationship, text, timestamp } from '@keystone-6/core/fields';
import { select } from '@keystone-6/core/fields';
import { Lists } from '.keystone/types';
import { Lists } from '.keystone';

export const lists: Lists = {
Task: list({
Expand Down
Loading