forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.ts
112 lines (100 loc) · 3.27 KB
/
schema.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { list } from '@keystone-6/core';
import type { GraphQLSchema } from 'graphql';
import { mergeSchemas } from '@graphql-tools/schema';
import { select, relationship, text, timestamp } from '@keystone-6/core/fields';
import { allowAll } from '@keystone-6/core/access';
import { pubSub } from './websocket';
import type { Lists } from '.keystone/types';
export const lists: Lists = {
Post: list({
access: allowAll,
hooks: {
// this hook publishes posts to the 'POST_UPDATED' channel when a post mutated
afterOperation: async ({ item }) => {
// WARNING: passing this item directly to pubSub bypasses any contextual access control
// if you want access control, you need to use a different architecture
//
// tl;dr Keystone access filters are not respected in this scenario
console.log('POST_UPDATED', { id: item?.id });
pubSub.publish('POST_UPDATED', {
postUpdated: item,
});
},
},
fields: {
title: text({ validation: { isRequired: true } }),
status: select({
type: 'enum',
options: [
{ label: 'Draft', value: 'draft' },
{ label: 'Published', value: 'published' },
],
}),
content: text(),
publishDate: timestamp(),
author: relationship({ ref: 'Author.posts', many: false }),
},
}),
Author: list({
access: allowAll,
fields: {
name: text({ validation: { isRequired: true } }),
email: text({ isIndexed: 'unique', validation: { isRequired: true } }),
posts: relationship({ ref: 'Post.author', many: true }),
},
}),
};
export const extendGraphqlSchema = (schema: GraphQLSchema) =>
mergeSchemas({
schemas: [schema],
typeDefs: `
type Mutation {
""" Publish a post """
publishPost(id: ID!): Post
}
type Time {
iso: String!
}
type Subscription {
postPublished: Post
postUpdated: Post
time: Time
}`,
resolvers: {
Mutation: {
// custom mutation to publish a post
publishPost: async (root, { id }, context) => {
// we use `context.db.Post`, not `context.query.Post`
// as this matches the type needed for GraphQL resolvers
const post = context.db.Post.updateOne({
where: { id },
data: { status: 'published', publishDate: new Date().toISOString() },
});
console.log('POST_PUBLISHED', { id });
// WARNING: passing this item directly to pubSub bypasses any contextual access control
// if you want access control, you need to use a different architecture
//
// tl;dr Keystone access filters are not respected in this scenario
pubSub.publish('POST_PUBLISHED', {
postPublished: post,
});
return post;
},
},
// add the subscription resolvers
Subscription: {
time: {
// @ts-ignore
subscribe: () => pubSub.asyncIterator(['TIME']),
},
postPublished: {
// @ts-ignore
subscribe: () => pubSub.asyncIterator(['POST_PUBLISHED']),
},
postUpdated: {
// @ts-ignore
subscribe: () => pubSub.asyncIterator(['POST_UPDATED']),
},
},
},
});