-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
85 lines (75 loc) · 2.44 KB
/
index.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
import { PluginFunction } from "@graphql-codegen/plugin-helpers";
import { RawTypesConfig } from "@graphql-codegen/visitor-plugin-common";
const pascalCase = (str: string) =>
str.length > 1
? str.charAt(0).toUpperCase() + str.slice(1)
: str.toUpperCase();
const queryTemplate = (
operation: "Query" | "Mutation" | "Subscription",
fieldName: string
) =>
`export const create${operation}${pascalCase(
fieldName
)}Resolver = createResolver('${operation}', '${fieldName}')`;
export const plugin: PluginFunction<RawTypesConfig> = (schema) => {
// Query
const queryType = schema.getQueryType();
const queries =
queryType && queryType.astNode && queryType.astNode.fields
? queryType.astNode.fields.map((field) =>
queryTemplate("Query", field.name.value)
)
: [];
// Mutation
const mutationType = schema.getMutationType();
const mutations =
mutationType && mutationType.astNode && mutationType.astNode.fields
? mutationType.astNode.fields.map((field) =>
queryTemplate("Mutation", field.name.value)
)
: [];
// Subscriptions
const subscriptionType = schema.getSubscriptionType();
const subscriptions =
subscriptionType &&
subscriptionType.astNode &&
subscriptionType.astNode.fields
? subscriptionType.astNode.fields.map((field) =>
queryTemplate("Subscription", field.name.value)
)
: [];
return {
prepend: [
`import { Resolver, ResolverProps } from '@aws-cdk/aws-appsync';
import { Construct } from '@aws-cdk/core';
const pascalCase = (str: string) => str.length > 1 ? str.charAt(0).toUpperCase() + str.slice(1) : str.toUpperCase();
export type NameFunction = (defaultName: string) => string;
export type Props = Omit<ResolverProps, "typeName" | "fieldName">;
const createResolver = (typeName: string, fieldName: string) => (
scope: Construct,
nameOrProps: string | NameFunction | Props,
props?: Props
) => {
let _name = "AppSyncResolver" + typeName + pascalCase(fieldName);
let _props: Props;
if (typeof nameOrProps === "string") {
_name = nameOrProps;
_props = props!;
} else if (typeof nameOrProps === "function") {
_name = nameOrProps(_name);
_props = props!;
} else {
_props = nameOrProps;
}
const resolver = new Resolver(scope, _name, {
..._props,
typeName,
fieldName,
});
return resolver;
};
`,
],
content: [...queries, ...mutations, ...subscriptions].join("\n"),
};
};