Skip to content

Commit 320e8d1

Browse files
committed
Easier context binding
1 parent 40f473c commit 320e8d1

File tree

16 files changed

+113
-118
lines changed

16 files changed

+113
-118
lines changed

examples/auth-magic-link/schema.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { g, list } from '@keystone-6/core'
1+
import { initG, list } from '@keystone-6/core'
22
import { allowAll, denyAll } from '@keystone-6/core/access'
33
import { password, text, timestamp } from '@keystone-6/core/fields'
4-
import type { Lists, Context } from '.keystone/types'
4+
import type { Lists, TypeInfo } from '.keystone/types'
55

66
import { randomBytes } from 'node:crypto'
77

8+
const g = initG<TypeInfo>()
9+
type g<T> = initG<T>
10+
811
export type Session = {
912
itemId: string
1013
}
@@ -107,7 +110,7 @@ export const extendGraphqlSchema = g.extend(base => {
107110
type: g.nonNull(g.Boolean), // always true
108111
args: { userId: g.arg({ type: g.nonNull(g.String) }) },
109112

110-
async resolve(args, { userId }, context: Context) {
113+
async resolve(args, { userId }, context) {
111114
// run asynchronously to reduce timing attacks
112115
;(async function () {
113116
const ott = randomBytes(16).toString('base64url')
@@ -141,7 +144,7 @@ export const extendGraphqlSchema = g.extend(base => {
141144
token: g.arg({ type: g.nonNull(g.String) }),
142145
},
143146

144-
async resolve(args, { userId, token }, context: Context) {
147+
async resolve(args, { userId, token }, context) {
145148
if (!context.sessionStrategy)
146149
throw new Error('No session implementation available on context')
147150

examples/extend-graphql-schema-graphql-ts/schema.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { g, list } from '@keystone-6/core'
1+
import { initG, list } from '@keystone-6/core'
22
import { allowAll } from '@keystone-6/core/access'
33
import { select, relationship, text, timestamp } from '@keystone-6/core/fields'
4-
import { type Context, type Lists } from '.keystone/types'
4+
import type { TypeInfo, Lists } from '.keystone/types'
5+
6+
const g = initG<TypeInfo>()
7+
type g<T> = initG<T>
58

69
export const lists = {
710
Post: list({
@@ -37,23 +40,23 @@ export const extendGraphqlSchema = g.extend(base => {
3740
fields: {
3841
draft: g.field({
3942
type: g.Int,
40-
resolve({ authorId }, args, context: Context) {
43+
resolve({ authorId }, args, context) {
4144
return context.query.Post.count({
4245
where: { author: { id: { equals: authorId } }, status: { equals: 'draft' } },
4346
})
4447
},
4548
}),
4649
published: g.field({
4750
type: g.Int,
48-
resolve({ authorId }, args, context: Context) {
51+
resolve({ authorId }, args, context) {
4952
return context.query.Post.count({
5053
where: { author: { id: { equals: authorId } }, status: { equals: 'published' } },
5154
})
5255
},
5356
}),
5457
latest: g.field({
5558
type: base.object('Post'),
56-
async resolve({ authorId }, args, context: Context) {
59+
async resolve({ authorId }, args, context) {
5760
const [post] = await context.db.Post.findMany({
5861
take: 1,
5962
orderBy: { publishDate: 'desc' },
@@ -72,7 +75,7 @@ export const extendGraphqlSchema = g.extend(base => {
7275
// with the name provided or throw if it doesn't exist
7376
type: base.object('Post'),
7477
args: { id: g.arg({ type: g.nonNull(g.ID) }) },
75-
resolve(source, { id }, context: Context) {
78+
resolve(source, { id }, context) {
7679
// Note we use `context.db.Post` here as we have a return type
7780
// of Post, and this API provides results in the correct format.
7881
// If you accidentally use `context.query.Post` here you can expect problems
@@ -90,7 +93,7 @@ export const extendGraphqlSchema = g.extend(base => {
9093
banPost: g.field({
9194
type: base.object('Post'),
9295
args: { id: g.arg({ type: g.nonNull(g.ID) }) },
93-
resolve(source, { id }, context: Context) {
96+
resolve(source, { id }, context) {
9497
return context.db.Post.updateOne({
9598
where: { id },
9699
data: { status: 'banned' },
@@ -107,7 +110,7 @@ export const extendGraphqlSchema = g.extend(base => {
107110
id: g.arg({ type: g.nonNull(g.ID) }),
108111
seconds: g.arg({ type: g.nonNull(g.Int), defaultValue: 600 }),
109112
},
110-
resolve(source, { id, seconds }, context: Context) {
113+
resolve(source, { id, seconds }, context) {
111114
const cutoff = new Date(Date.now() - seconds * 1000)
112115

113116
// Note we use `context.db.Post` here as we have a return type

examples/graphql-codegen/schema.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
/* eslint-disable @typescript-eslint/consistent-type-imports */
2-
import { list, g } from '@keystone-6/core'
2+
import { list, initG } from '@keystone-6/core'
33
import { relationship, text, timestamp, virtual } from '@keystone-6/core/fields'
44
import { allowAll } from '@keystone-6/core/access'
55
import { graphql } from './gql'
66

7-
import type { Lists } from '.keystone/types'
7+
import type { Lists, TypeInfo } from '.keystone/types'
8+
9+
const g = initG<TypeInfo>()
10+
type g<T> = initG<T>
811

912
const LatestPostQuery = graphql(/* GraphQL */ `
1013
query LastestPostQuery($id: ID!) {

examples/graphql-gql.tada/schema.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { list, g } from '@keystone-6/core'
1+
import { initG, list } from '@keystone-6/core'
22
import { relationship, text, timestamp, virtual } from '@keystone-6/core/fields'
33
import { allowAll } from '@keystone-6/core/access'
44
import { graphql } from './tada'
5-
import type { Lists } from '.keystone/types'
5+
import type { Lists, TypeInfo } from '.keystone/types'
66

77
const LatestPostQuery = graphql(`
88
query LastestPostQuery($id: ID!) {
@@ -15,6 +15,9 @@ const LatestPostQuery = graphql(`
1515
}
1616
`)
1717

18+
const g = initG<TypeInfo>()
19+
type g<T> = initG<T>
20+
1821
export const lists: Lists = {
1922
Post: list({
2023
access: allowAll,

examples/graphql-ts-gql/schema.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable @typescript-eslint/consistent-type-imports */
2-
import { list, g } from '@keystone-6/core'
2+
import { list, initG } from '@keystone-6/core'
33
import { relationship, text, timestamp, virtual } from '@keystone-6/core/fields'
44
import { allowAll } from '@keystone-6/core/access'
55
import { gql } from '@ts-gql/tag/no-transform'
66

7-
import type { Lists } from '.keystone/types'
7+
import type { Lists, TypeInfo } from '.keystone/types'
88

99
const LatestPostQuery = gql`
1010
query LastestPostQuery($id: ID!) {
@@ -18,6 +18,9 @@ const LatestPostQuery = gql`
1818
}
1919
` as import('./__generated__/ts-gql/LastestPostQuery').type
2020

21+
const g = initG<TypeInfo>()
22+
type g<T> = initG<T>
23+
2124
export const lists = {
2225
Post: list({
2326
access: allowAll,

examples/number-fields/schema.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { list, g } from '@keystone-6/core'
1+
import { list, initG } from '@keystone-6/core'
22
import { bigInt, float, integer, virtual } from '@keystone-6/core/fields'
33
import { allowAll } from '@keystone-6/core/access'
4-
import type { Lists } from '.keystone/types'
4+
import type { Lists, TypeInfo } from '.keystone/types'
5+
6+
const g = initG<TypeInfo>()
7+
type g<T> = initG<T>
58

69
export const lists = {
710
Example: list({

examples/transactions/schema.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import { g, list } from '@keystone-6/core'
1+
import { initG, list } from '@keystone-6/core'
22
import { allowAll } from '@keystone-6/core/access'
33
import { text, integer, relationship, timestamp, virtual } from '@keystone-6/core/fields'
4-
import { type Context, type Lists } from '.keystone/types'
4+
import type { Lists, TypeInfo } from '.keystone/types'
5+
6+
const g = initG<TypeInfo>()
7+
type g<T> = initG<T>
58

69
export const extendGraphqlSchema = g.extend(base => {
710
return {
811
mutation: {
912
submitOrder: g.field({
1013
type: base.object('Order'),
1114
args: {},
12-
async resolve(source, {}, context: Context) {
15+
async resolve(source, {}, context) {
1316
// TODO: this should come from GraphQL arguments
1417
const orderInput = [
1518
{ sku: '123', count: 1 },

examples/usecase-relationship-union/schema.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { list, group, g } from '@keystone-6/core'
1+
import { list, group, initG } from '@keystone-6/core'
22
import { allowAll } from '@keystone-6/core/access'
33
import { text, relationship, virtual } from '@keystone-6/core/fields'
4-
import type { Lists } from '.keystone/types'
4+
import type { Lists, TypeInfo } from '.keystone/types'
55

66
function ifUnsetHideUI<Key extends string>(field: Key) {
77
return {
@@ -15,6 +15,9 @@ function ifUnsetHideUI<Key extends string>(field: Key) {
1515
}
1616
}
1717

18+
const g = initG<TypeInfo>()
19+
type g<T> = initG<T>
20+
1821
export const lists: Lists = {
1922
Post: list({
2023
access: allowAll,

examples/virtual-field/schema.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { list, g } from '@keystone-6/core'
1+
import { list, initG } from '@keystone-6/core'
22
import { text, checkbox, virtual } from '@keystone-6/core/fields'
33
import { allowAll } from '@keystone-6/core/access'
44

5-
import type { Lists } from '.keystone/types'
5+
import type { Lists, TypeInfo } from '.keystone/types'
6+
7+
const g = initG<TypeInfo>()
8+
type g<T> = initG<T>
69

710
export const lists = {
811
Post: list({

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@
235235
"@aws-sdk/lib-storage": "^3.83.0",
236236
"@aws-sdk/s3-request-presigner": "^3.83.0",
237237
"@babel/runtime": "^7.24.7",
238-
"@graphql-ts/extend": "https://pkg.pr.new/Thinkmill/graphql-ts/@graphql-ts/extend@5eafeea",
239-
"@graphql-ts/schema": "https://pkg.pr.new/Thinkmill/graphql-ts/@graphql-ts/schema@5eafeea",
238+
"@graphql-ts/extend": "0.0.0-test-20250311072637",
239+
"@graphql-ts/schema": "0.0.0-test-20250311072637",
240240
"@graphql-typed-document-node/core": "^3.1.2",
241241
"@hapi/iron": "^7.0.0",
242242
"@internationalized/date": "^3.5.5",

0 commit comments

Comments
 (0)