-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathschema.ts
221 lines (204 loc) · 6.54 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
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
import { readFileSync } from 'fs';
import { Lazy } from '@aws-cdk/core';
import { CfnGraphQLSchema } from './appsync.generated';
import { GraphqlApi } from './graphqlapi';
import { SchemaMode, shapeAddition } from './private';
import { IIntermediateType } from './schema-base';
import { ResolvableField } from './schema-field';
import { ObjectType } from './schema-intermediate';
/**
* The options for configuring a schema
*
* If no options are specified, then the schema will
* be generated code-first.
*/
export interface SchemaOptions {
/**
* The file path for the schema. When this option is
* configured, then the schema will be generated from an
* existing file from disk.
*
* @default - schema not configured through disk asset
*/
readonly filePath?: string,
};
/**
* The Schema for a GraphQL Api
*
* If no options are configured, schema will be generated
* code-first.
*/
export class Schema {
/**
* Generate a Schema from file
*
* @returns `SchemaAsset` with immutable schema defintion
* @param filePath the file path of the schema file
*/
public static fromAsset(filePath: string): Schema {
return new Schema({ filePath });
}
/**
* The definition for this schema
*/
public definition: string;
private query?: ObjectType;
private mutation?: ObjectType;
private subscription?: ObjectType;
private schema?: CfnGraphQLSchema;
private mode: SchemaMode;
private types: IIntermediateType[];
public constructor(options?: SchemaOptions) {
if (options?.filePath) {
this.mode = SchemaMode.FILE;
this.definition = readFileSync(options.filePath).toString('utf-8');
} else {
this.mode = SchemaMode.CODE;
this.definition = '';
}
this.types = [];
}
/**
* Called when the GraphQL Api is initialized to allow this object to bind
* to the stack.
*
* @param api The binding GraphQL Api
*/
public bind(api: GraphqlApi): CfnGraphQLSchema {
if (!this.schema) {
this.schema = new CfnGraphQLSchema(api, 'Schema', {
apiId: api.apiId,
definition: this.mode === SchemaMode.CODE ?
Lazy.string({
produce: () => this.types.reduce((acc, type) => { return `${acc}${type._bindToGraphqlApi(api).toString()}\n`; },
`${this.declareSchema()}${this.definition}`),
})
: this.definition,
});
}
return this.schema;
}
/**
* Escape hatch to add to Schema as desired. Will always result
* in a newline.
*
* @param addition the addition to add to schema
* @param delimiter the delimiter between schema and addition
* @default - ''
*
* @experimental
*/
public addToSchema(addition: string, delimiter?: string): void {
if (this.mode !== SchemaMode.CODE) {
throw new Error('API cannot append to schema because schema definition mode is not configured as CODE.');
}
const sep = delimiter ?? '';
this.definition = `${this.definition}${sep}${addition}\n`;
}
/**
* Add a query field to the schema's Query. CDK will create an
* Object Type called 'Query'. For example,
*
* type Query {
* fieldName: Field.returnType
* }
*
* @param fieldName the name of the query
* @param field the resolvable field to for this query
*/
public addQuery(fieldName: string, field: ResolvableField): ObjectType {
if (this.mode !== SchemaMode.CODE) {
throw new Error(`Unable to add query. Schema definition mode must be ${SchemaMode.CODE}. Received: ${this.mode}`);
}
if (!this.query) {
this.query = new ObjectType('Query', { definition: {} });
this.addType(this.query);
};
this.query.addField({ fieldName, field });
return this.query;
}
/**
* Add a mutation field to the schema's Mutation. CDK will create an
* Object Type called 'Mutation'. For example,
*
* type Mutation {
* fieldName: Field.returnType
* }
*
* @param fieldName the name of the Mutation
* @param field the resolvable field to for this Mutation
*/
public addMutation(fieldName: string, field: ResolvableField): ObjectType {
if (this.mode !== SchemaMode.CODE) {
throw new Error(`Unable to add mutation. Schema definition mode must be ${SchemaMode.CODE}. Received: ${this.mode}`);
}
if (!this.mutation) {
this.mutation = new ObjectType('Mutation', { definition: {} });
this.addType(this.mutation);
};
this.mutation.addField({ fieldName, field });
return this.mutation;
}
/**
* Add a subscription field to the schema's Subscription. CDK will create an
* Object Type called 'Subscription'. For example,
*
* type Subscription {
* fieldName: Field.returnType
* }
*
* @param fieldName the name of the Subscription
* @param field the resolvable field to for this Subscription
*/
public addSubscription(fieldName: string, field: ResolvableField): ObjectType {
if (this.mode !== SchemaMode.CODE) {
throw new Error(`Unable to add subscription. Schema definition mode must be ${SchemaMode.CODE}. Received: ${this.mode}`);
}
if (!this.subscription) {
this.subscription = new ObjectType('Subscription', { definition: {} });
this.addType(this.subscription);
}
const directives = field.fieldOptions?.directives?.filter((directive) => directive.mutationFields);
if (directives && directives.length > 1) {
throw new Error(`Subscription fields must not have more than one @aws_subscribe directives. Received: ${directives.length}`);
}
this.subscription.addField({ fieldName, field });
return this.subscription;
}
/**
* Add type to the schema
*
* @param type the intermediate type to add to the schema
*
* @experimental
*/
public addType(type: IIntermediateType): IIntermediateType {
if (this.mode !== SchemaMode.CODE) {
throw new Error('API cannot add type because schema definition mode is not configured as CODE.');
}
this.types.push(type);
return type;
}
/**
* Set the root types of this schema if they are defined.
*
* For example:
* schema {
* query: Query
* mutation: Mutation
* subscription: Subscription
* }
*/
private declareSchema(): string {
if (!this.query && !this.mutation && !this.subscription) {
return '';
}
type root = 'mutation' | 'query' | 'subscription';
const list: root[] = ['query', 'mutation', 'subscription'];
return shapeAddition({
prefix: 'schema',
fields: list.map((key: root) => this[key] ? `${key}: ${this[key]?.name}` : '')
.filter((field) => field != ''),
}) + '\n';
}
}