-
Notifications
You must be signed in to change notification settings - Fork 33
/
stub.ts
64 lines (59 loc) · 1.46 KB
/
stub.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
import {
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLInputObjectType,
GraphQLString,
GraphQLNamedType,
GraphQLInt,
GraphQLFloat,
GraphQLBoolean,
GraphQLID,
isObjectType,
isInterfaceType,
isInputObjectType,
} from 'graphql';
export function createNamedStub(
name: string,
type: 'object' | 'interface' | 'input'
): GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType {
let constructor: any;
if (type === 'object') {
constructor = GraphQLObjectType;
} else if (type === 'interface') {
constructor = GraphQLInterfaceType;
} else {
constructor = GraphQLInputObjectType;
}
return new constructor({
name,
fields: {
__fake: {
type: GraphQLString,
},
},
});
}
export function isStub(type: GraphQLNamedType): boolean {
if (isObjectType(type) || isInterfaceType(type) || isInputObjectType(type)) {
const fields = type.getFields();
const fieldNames = Object.keys(fields);
return fieldNames.length === 1 && fields[fieldNames[0]].name === '__fake';
}
return false;
}
export function getBuiltInForStub(type: GraphQLNamedType): GraphQLNamedType {
switch (type.name) {
case GraphQLInt.name:
return GraphQLInt;
case GraphQLFloat.name:
return GraphQLFloat;
case GraphQLString.name:
return GraphQLString;
case GraphQLBoolean.name:
return GraphQLBoolean;
case GraphQLID.name:
return GraphQLID;
default:
return type;
}
}