-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimpleGraphqlApi.ts
executable file
·147 lines (136 loc) · 3.57 KB
/
SimpleGraphqlApi.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { PubSub, PubSubEngine } from "apollo-server";
import gql from "graphql-tag";
import { IWebSocketOverHttpGraphqlSubscriptionContext } from "../subscriptions-transport-ws-over-http/WebSocketOverHttpGraphqlContext";
import { IContextForPublishingWithEpcp } from "../subscriptions-transport-ws-over-http/WebSocketOverHttpGraphqlContext";
import { WebSocketOverHttpPubSubMixin } from "../subscriptions-transport-ws-over-http/WebSocketOverHttpPubSubMixin";
interface IPost {
/** post author */
author: string;
/** post body text */
comment: string;
}
interface IPostController {
/** add a post */
addPost(post: IPost): IPost;
/** get all posts */
posts(): IPost[];
}
/** Constructor for a PostController that stores post in memory */
const PostController = (): IPostController => {
const postStorage: IPost[] = [];
const addPost = (post: IPost): IPost => {
postStorage.push(post);
return post;
};
const posts = (): IPost[] => {
return postStorage.slice();
};
return {
addPost,
posts,
};
};
enum SimpleGraphqlApiPubSubTopic {
POST_ADDED = "POST_ADDED",
}
/** Common Subscription queries that can be passed to ApolloClient.subscribe() */
export const SimpleGraphqlApiSubscriptions = {
postAdded() {
return {
query: gql`
subscription {
postAdded {
author
comment
}
}
`,
variables: {},
};
},
};
/** Factories for common mutations that can be passed to apolloClient.mutate() */
export const SimpleGraphqlApiMutations = {
addPost(post: IPost) {
return {
mutation: gql`
mutation AddPost($author: String!, $comment: String!) {
addPost(author: $author, comment: $comment) {
author
comment
}
}
`,
variables: post,
};
},
};
interface ISimpleGraphqlApiOptions {
/** controller that will handle storing/retrieving posts */
postController?: IPostController;
/** pubsub implementation that will be used by the gql schema resolvers */
pubsub?: PubSubEngine;
}
/**
* A GraphQL API from the apollo-server subscriptions docs: https://www.apollographql.com/docs/apollo-server/features/subscriptions/
*/
export const SimpleGraphqlApi = ({
pubsub = new PubSub(),
postController = PostController(),
}: ISimpleGraphqlApiOptions = {}) => {
const typeDefs = gql`
type Subscription {
postAdded: Post
}
type Query {
posts: [Post]
}
type Mutation {
addPost(author: String, comment: String): Post
}
type Post {
author: String
comment: String
}
`;
const resolvers = {
Mutation: {
async addPost(
root: any,
args: any,
context: IContextForPublishingWithEpcp,
) {
await WebSocketOverHttpPubSubMixin(context)(pubsub).publish(
SimpleGraphqlApiPubSubTopic.POST_ADDED,
{
postAdded: args,
},
);
return postController.addPost(args);
},
},
Query: {
posts(root: any, args: any, context: any) {
return postController.posts();
},
},
Subscription: {
postAdded: {
// Additional event labels can be passed to asyncIterator creation
subscribe(
rootValue: any,
args: object,
context: any | IWebSocketOverHttpGraphqlSubscriptionContext,
) {
return WebSocketOverHttpPubSubMixin(context)(pubsub).asyncIterator([
SimpleGraphqlApiPubSubTopic.POST_ADDED,
]);
},
},
},
};
return {
resolvers,
typeDefs,
};
};