forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-schema.ts
More file actions
86 lines (84 loc) · 3.14 KB
/
custom-schema.ts
File metadata and controls
86 lines (84 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { graphql } from '@keystone-6/core';
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 };
},
}),
},
};
});