-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
140 lines (122 loc) · 4.12 KB
/
util.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
import {
GraphQLScalarType,
GraphQLNonNull,
GraphQLList,
GraphQLOutputType,
GraphQLObjectType,
GraphQLSchema,
GraphQLEnumType
} from 'graphql';
export interface Example {
summary: string;
value: {
query: string;
variables?: Record<string, any>;
operationName?: string;
};
response: any;
operation: string;
}
function createMockResponse(returnType: GraphQLOutputType, fieldName: string): object {
if (isScalarType(returnType)) {
return { data: { [fieldName]: createScalarMock(returnType) } };
}
// If the returnType is an object type, create a mock object
if (returnType instanceof GraphQLObjectType) {
let mockObject: Record<string, any> = {};
const fields = returnType.getFields();
for (const [fieldKey, field] of Object.entries(fields)) {
// For simplicity, assume field types are scalar
mockObject[fieldKey] = createScalarMock(field.type);
}
return { data: mockObject };
}
// Fallback for other types (lists, enums, etc.)
return { data: { [fieldName]: {} } };
}
export function exampleMaker(schema: GraphQLSchema, exampleValues: any) {
const typeMap = schema.getTypeMap();
let examples: Record<string, Example> = {};
Object.values(typeMap).forEach((type) => {
if (type instanceof GraphQLObjectType && (type.name === 'Query' || type.name === 'Mutation')) {
const fields = type.getFields();
Object.values(fields).forEach((field) => {
let args = field.args.map(arg => `${arg.name}: $${arg.name}`);
let vars = field.args.map(arg => `$${arg.name}: ${arg.type}`);
let operation = type.name === 'Query' ? 'query' : 'mutation';
let query = `${operation} ${field.name}${vars.length > 0 ? '(' + vars.join(', ') + ')' : ''} { ${field.name}${args.length > 0 ? '(' + args.join(', ') + ')' : ''} }`;
let exampleVariables = field.args.reduce((acc, arg) => {
// @ts-ignore
acc[arg.name] = exampleValues[arg.name] || 'value'; // Default value
return acc;
}, {});
let mockResponse = createMockResponse(field.type, field.name);
examples[field.name] = {
summary: `Example ${type.name}`,
value: {
query: query,
variables: exampleVariables
},
response: mockResponse,
operation: operation
};
});
}
});
return examples;
}
export function createScalarMock(type: GraphQLOutputType): any {
if (type instanceof GraphQLScalarType || type instanceof GraphQLNonNull || type instanceof GraphQLList) {
switch (type.toString()) {
case 'String':
return 'Sample string';
case 'Int':
return 123;
case 'Float':
return 123.45;
case 'Boolean':
return true;
default:
return null;
}
}
return null;
}
export function isScalarType(type: GraphQLOutputType): boolean {
return (
type instanceof GraphQLScalarType ||
(type instanceof GraphQLNonNull && isScalarType(type.ofType)) ||
(type instanceof GraphQLList && isScalarType(type.ofType))
);
}
export function determineGraphQLType(type: GraphQLOutputType): string {
if (type instanceof GraphQLScalarType) {
switch (type.name) {
case 'String':
return 'string';
case 'Int':
return 'integer';
case 'Float':
return 'number';
case 'Boolean':
return 'boolean';
case 'ID':
return 'string'; // ID is often represented as a string in OpenAPI
default:
return 'unknown'; // Consider handling custom scalar types here
}
} else if (type instanceof GraphQLNonNull) {
// Non-null type, check the inner type
return determineGraphQLType(type.ofType);
} else if (type instanceof GraphQLList) {
// For lists, specify the type of items in the list
return 'array';
} else if (type instanceof GraphQLObjectType) {
// Handle object types, possibly by referencing a defined schema or inline defining the object structure
return 'object';
} else if (type instanceof GraphQLEnumType) {
// Handle enum types
return 'enum';
}
return 'unknown'; // Fallback for types not covered above
}