This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
/
main.ts
191 lines (176 loc) · 4.28 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import GraphQLGenie from './GraphQLGenie';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { SchemaLink } from 'apollo-link-schema';
import gql from 'graphql-tag';
const typeDefs = `
interface Submission {
id: ID! @isUnique
title: String!
text: String
}
type Post implements Submission @model {
id: ID! @isUnique
title: String!
text: String
author: User @relation(name: "WrittenSubmissions")
likedBy: [User!]! @relation(name: "LikedPosts")
comments: [Comment] @relation(name: "CommentsOnPost")
}
type Comment implements Submission @model {
id: ID! @isUnique
title: String!
text: String
author: User @relation(name: "WrittenSubmissions")
post: Post @relation(name: "CommentsOnPost")
}
type User @model {
id: ID! @isUnique
name : String!
address: Address @relation(name: "UserAddress")
writtenSubmissions: [Submission!]! @relation(name: "WrittenSubmissions")
likedPosts: [Post!]! @relation(name: "LikedPosts")
}
type Address @model {
id: ID! @isUnique
city: String!
user: User @relation(name: "UserAddress")
}
`;
const fortuneOptions = { settings: { enforceLinks: true } };
const start = Date.now();
console.log('GraphQL Genie Started');
const genie = new GraphQLGenie({ typeDefs, fortuneOptions});
const buildClient = async (genie: GraphQLGenie) => {
const schema = await genie.getSchema();
console.log('GraphQL Genie Completed', Date.now() - start);
// const resolverMap = {
// Query: {
// allTestings: (_obj, { _name }, _context) => {
// return state.testings;
// },
// },
// Mutation: {
// addTesting: (_, { name }, _context) => {
// const testing = { name: name };
// state.testings.push(testing);
// return testing;
// },
// },
// };
// // for (const [name, resolve] of newQueryResolvers) {
// // resolverMap.Query[name] = resolve;
// // }
// const resolversMap = new Map<string, GraphQLFieldResolver<any, any>>();
// resolversMap.set('args', (
// _root: any,
// _args: { [key: string]: any },
// _context: any,
// _info: GraphQLResolveInfo,
// ): any => {
// const selections = computeIncludes(_info.operation.selectionSet.selections[0], 'GraphQLDirective');
// console.info('selections');
// console.info(selections);
// console.info(JSON.stringify(selections));
// console.log(_root);
// console.log(_args);
// console.log(_context);
// console.log(_info);
// });
// addResolvers('GraphQLDirective', resolversMap);
const client = new ApolloClient({
link: new SchemaLink({ schema: schema }),
cache: new InMemoryCache(),
connectToDevTools: true
});
client.initQueryManager();
const createPost = gql`
mutation createPost($title: String!) {
createPost(title: $title) {
id
}
}
`;
const createUser = gql`
mutation createUser($name: String!) {
createUser(name: $name) {
id
}
}
`;
const createAddress = gql`
mutation createAddress($city: String!) {
createAddress(city: $city) {
id
}
}
`;
const post = await client.mutate({
mutation: createPost,
variables: { title: 'bam post' }
});
const user = await client.mutate({
mutation: createUser,
variables: { name: 'Corey' }
});
const address = await client.mutate({
mutation: createAddress,
variables: { city: 'Eau Claire' }
});
console.log('post', post.data.createPost.id,
'user', user.data.createUser.id,
'address', address.data.createAddress.id);
// client.mutate({
// mutation: gql`mutation {
// setUserAddress (addressAddressId:"9gFXv_BuLNPW19q", userUserId:"M4AQV51bbQgqvD9") {
// addressAddress {
// id
// city
// user {
// name
// }
// }
// userUser {
// id
// name
// address {
// city
// }
// }
// }
// }`
// })
// mutation {
// addToWrittenPosts(writtenPostsPostId: "rMxFhntFzZWTNU1" authorUserId: "QRX3X1pER7bPJFT") {
// writtenPostsPost{
// title
// author {
// name
// }
// }
// authorUser {
// name
// writtenPosts {
// title
// }
// }
// }
// }
// mutation {
// addToLikedPosts(likedPostsPostId: "AIl4McpfFIKvQ4G", likedByUserId: "QOK0khLNpq3eX0F") {
// likedPostsPost {
// title
// likedBy {
// name
// }
// }
// likedByUser {
// name
// likedPosts {
// title
// }
// }
// }
// }
};
buildClient(genie);