-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
107 lines (100 loc) · 2.93 KB
/
utils.ts
File metadata and controls
107 lines (100 loc) · 2.93 KB
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
var pluralize = require('pluralize');
import { ObjectType } from '@aws-cdk/aws-appsync';
import { int, string, required_string, required_boolean } from './scalar-types';
export const args = {
after: string,
first: int,
before: string,
last: int,
};
export const PageInfo = new ObjectType('PageInfo', {
definition: {
hasNextPage: required_boolean,
hasPreviousPage: required_boolean,
startCursor: string,
endCursor: string,
},
});
/**
* The base options for creating an edge or connection
*
* If base is Foo and target is Bar:
*
* `FooBarsEdge | FooBarsConnection`
*
* @option base - the prefix for this Object Type
* @option target - the Object Type that the prefix is connected to
*/
export interface baseOptions {
/**
* the prefix for this Object Type
*/
readonly base: ObjectType;
/**
* the Object Type that the prefix is connected to
*/
readonly target: ObjectType;
};
/**
* Utility Function to obtain the name of an Edge or Connection Type
*
* @param suffix the end of the name (i.e. Edge or Connection)
* @param options the options associated with this name
*/
function obtainName(suffix: string, options: baseOptions): string {
// If base and target are the same, do not have prefix
const isSame: boolean = options.base == options.target;
const prefix = isSame ? '' : options.base.name;
const target = pluralize(options.target.name);
return `${prefix}${target}${suffix}`;
}
/**
* Generate and xxxXxxEdge Object Type
*
* @param options.base the base object type
* @param options.target the target object type
*/
export function generateEdge(options: baseOptions): ObjectType {
const name = obtainName('Edge', options);
return new ObjectType(name, {
definition:{
node: options.target.attribute(),
cursor: required_string,
}
});
};
/**
* Generate and xxxXxxConnection Object Type
*
* @param edge the edge associated with this connection
* @param options.base the base object type
* @param options.target the target object type
*/
export function generateConnection(edge: ObjectType, options: baseOptions): ObjectType {
const name = obtainName('Connection', options);
const plural = pluralize(options.target.name).toLowerCase();
return new ObjectType(name, {
definition:{
pageInfo: PageInfo.attribute({ isRequired: true }),
edges: edge.attribute({ isList: true }),
totalCount: int,
[plural]: options.target.attribute({ isList: true }),
}
});
};
/**
* Generates both an edge and connection between two object types.
*
* @param options.base the base object type
* @param options.target the target object type
*
* @returns - `{ edge: ObjectType, connection: ObjectType}`
*/
export function generateConnectionAndEdge(options: baseOptions): { [key: string]: ObjectType } {
const edge = generateEdge(options);
const connection = generateConnection(edge, options);
return {
edge: edge,
connection: connection,
};
};