-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.ts
38 lines (34 loc) · 921 Bytes
/
utils.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
import { TypeNode, NamedTypeNode } from 'graphql';
export function isListType(type: TypeNode): boolean {
switch (type.kind) {
case 'NamedType':
return false;
case 'ListType':
return true;
case 'NonNullType':
return isListType(type.type);
default:
throw new Error(`unsupported type: ${(type as TypeNode).kind}`);
}
}
export function oneOrMany<T>(xs: T[], one: boolean): T | T[] {
return one ? xs[0] : xs;
}
export function unwrapCompositeType(type: TypeNode): NamedTypeNode {
switch (type.kind) {
case 'NamedType':
return type;
case 'ListType':
case 'NonNullType':
return unwrapCompositeType(type.type);
default:
throw new Error(`unsupported type: ${(type as TypeNode).kind}`);
}
}
export function ensureArray<T>(obj: T | Array<T>): Array<T> {
if (Array.isArray(obj)) {
return obj;
} else {
return obj ? [obj] : [];
}
}