-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstarwars-dynamic-stack.ts
224 lines (196 loc) · 6.57 KB
/
starwars-dynamic-stack.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const pluralize = require('pluralize');
import * as path from 'path';
import * as cdk from '@aws-cdk/core';
import * as appsync from '@aws-cdk/aws-appsync';
import * as schema from './dynamic-implementation/index';
/**
* A placeholder request mapping template
*/
const dummyRequest = appsync.MappingTemplate.fromFile(path.join(__dirname, "mapping-templates", "empty-request.vtl"));
/**
* A placeholder response mapping template
*/
const dummyResponse = appsync.MappingTemplate.fromFile(path.join(__dirname, "mapping-templates", "empty-response.vtl"));
export class StarwarsCodeFirstDynamicStack extends cdk.Stack {
/**
* Types used by other types (i.e. interface Node, type PageInfo)
*/
protected globals: { [key: string]: appsync.ObjectType | appsync.InterfaceType };
/**
* Object Types (i.e. Film, Person, Starship, etc.)
*/
protected objectTypes: { [key: string]: appsync.ObjectType };
/**
* Edges between two Object Types (i.e. FilmStarshipEdge, etc.)
*/
protected edges: appsync.ObjectType[];
/**
* Connections between two Object Types (i.e. FilmStarshipsEdges, etc.)
*/
protected connections: appsync.ObjectType[];
/**
* The Root type for Queries
*/
protected root: appsync.ObjectType;
/**
* the GraphQL Api for this
*/
public api: appsync.GraphqlApi;
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.api = new appsync.GraphqlApi(this, 'SWAPI', {
name: "SWAPI",
});
/**
* A placeholder data source
*/
const dummy = this.api.addNoneDataSource('DummyDS');
this.globals = {
Node: schema.Node,
PageInfo: schema.PageInfo,
};
this.objectTypes = {
Film: schema.Film,
Planet: schema.Planet,
Starship: schema.Starship,
Vehicle: schema.Vehicle,
Species: schema.Species,
Person: schema.Person,
};
this.edges = [];
this.connections = [];
/**
* An array for object type connections
*
* base: the base object type
* targets: the targets to connect between
*/
const objectTargets: {base: appsync.ObjectType, targets: appsync.ObjectType[]}[] = [
{
base: schema.Film,
targets: [schema.Species, schema.Starship, schema.Vehicle, schema.Person, schema.Planet],
},
{
base: schema.Planet,
targets: [schema.Person, schema.Film],
},
{
base: schema.Starship,
targets: [schema.Person, schema.Film],
},
{
base: schema.Vehicle,
targets: [schema.Person, schema.Film],
},
{
base: schema.Species,
targets: [schema.Person, schema.Film],
},
{
base: schema.Person,
targets: [schema.Film, schema.Starship, schema.Vehicle],
},
];
// Generating all Connections, Edges and their Resolvers
objectTargets.map((connection) => {
this.generateTargets(connection.base, dummy, connection.targets);
});
//
this.api.addToSchema('schema {\n query: Root\n}');
// Creating the Root Object Type (our query)
this.root = new appsync.ObjectType('Root', {
definition: {
node: new appsync.ResolvableField({
returnType: this.globals.Node.attribute(),
args: { id: schema.required_id },
dataSource: dummy,
requestMappingTemplate: dummyRequest,
responseMappingTemplate: dummyResponse,
})
},
});
this.api.addType(this.root);
// Generate the fields for Root
Object.keys(this.objectTypes).forEach((type) => {
const objectType = this.objectTypes[type];
const fieldName = type.toLowerCase();
// Generate `TypesConnection` and `TypesEdge` and its resolvable fields
// i.e. allFilms(...): FilmsConnection
this.generateAndAppendConnection(this.root, dummy, {
base: objectType,
target: objectType,
});
// Create the Resolvable Field for base object type
const field = new appsync.ResolvableField({
returnType: objectType.attribute(),
args: {
id: schema.id,
[`${fieldName}ID`]: schema.id,
},
dataSource: dummy,
requestMappingTemplate: dummyRequest,
responseMappingTemplate: dummyResponse,
});
// Generate single type queries: type(id: ID, typeID: ID): Type
// i.e. film(id: ID, filmID: ID): Film
this.root.addField({ fieldName, field });
});
this.appendAllToSchema();
}
/**
* Generate all connection and edges for a base object type
*
* @param base the base object type
* @param dataSource the data source linking these types
* @param targets a list of targets object types
*/
private generateTargets(base: appsync.ObjectType, dataSource: appsync.BaseDataSource, targets: appsync.ObjectType[]): void{
targets.map((target) => {
this.generateAndAppendConnection(base, dataSource, {
base: base,
target: target,
});
});
}
/**
* Generate a Connection, Edge and Resolver for a given base and target pair
* @param base the base object type
* @param dataSource the data source linking these types
* @param options a list of targets object types
*/
private generateAndAppendConnection(base: appsync.ObjectType, dataSource: appsync.BaseDataSource, options: schema.baseOptions): void{
// Create a the Object Types for Edge and Connection
const link = schema.generateConnectionAndEdge(options);
// Determine Field Name
const fieldName = base == this.root ?
`all${pluralize(options.target.name)}` :
`${options.target.name.toLowerCase()}Connection`;
// Create the Resolvable Field for base object type
const field = new appsync.ResolvableField({
returnType: link.connection.attribute(),
args: schema.args,
dataSource,
requestMappingTemplate: dummyRequest,
responseMappingTemplate: dummyResponse,
});
// Create Resolver and add field to base Object Type
base.addField({ fieldName, field });
// Push Edges and Connections to class member variable
this.edges.push(link.edge);
this.connections.push(link.connection);
}
/**
* Append all schema-related objects to schem
*/
private appendAllToSchema(): void{
// Utility Functions
const map = (dict: { [key: string]: appsync.IIntermediateType } ) => {
Object.keys(dict).forEach((key) => { this.api.addType(dict[key]); })
};
// Appending to schema
map(this.globals);
map(this.objectTypes);
this.edges.map((t) => this.api.addType(t));
this.connections.map((t) => this.api.addType(t));
}
}