-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
219 lines (199 loc) · 5.67 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import {
GraphQLOutputType,
GraphQLSchema
} from 'graphql';
import { Example, determineGraphQLType, exampleMaker } from './util';
interface RouteMap {
[operation: string]: {
tags: {
name: string;
description: string;
};
};
}
interface OpenApiSchemaOptions {
serverUrl: string;
title: string;
openapi: string;
version: string;
summary: string;
description: string;
exampleValues: object;
routeMap?: RouteMap;
}
const defaultOptions: OpenApiSchemaOptions = {
serverUrl: '/graphql',
title: 'GraphQL API',
openapi: '3.0.3',
version: '1.0.0',
summary: 'GraphQL Endpoint',
description: 'Endpoint for all GraphQL queries and mutations',
exampleValues: {}
};
interface OpenApiOperation {
post: {
operationId?: string;
'x-openai-isConsequential'?: boolean;
summary: string;
tags?: string[],
description: string;
requestBody: {
description: string;
required: boolean;
content: {
'application/json': {
schema: {
type: 'object';
properties: {
query: {
type: 'string';
example?: string;
};
variables?: {
type: 'object';
additionalProperties: boolean;
example?: any;
};
operationName?: {
type: 'string';
example?: string;
};
};
required: string[];
};
};
};
};
responses: {
[statusCode: string]: {
description: string;
content?: {
[contentType: string]: {
schema: {
type: string;
properties?: {
[propName: string]: {
type: string;
// Add other schema properties as needed
};
};
// Add other schema fields as needed
};
};
};
};
};
};
}
type Paths = Record<string, OpenApiOperation>;
function createResponseSchema(schemaRef: string): Record<string, any> {
return {
'200': {
description: 'Successful GraphQL response',
content: {
'application/json': {
schema: {
$ref: `#/components/schemas/${schemaRef}`
}
}
}
}
};
}
function getGraphQLFieldType(schema: GraphQLSchema, operationType: string, fieldName: string): GraphQLOutputType {
let rootType;
if (operationType === 'query') {
rootType = schema.getQueryType();
} else if (operationType === 'mutation') {
rootType = schema.getMutationType();
} else {
throw new Error(`Unknown operation type: ${operationType}`);
}
if (!rootType) {
throw new Error(`Root type for operation ${operationType} not found`);
}
const field = rootType.getFields()[fieldName];
if (!field) {
throw new Error(`Field ${fieldName} not found in operation ${operationType}`);
}
return field.type;
}
export const generateOpenAPISchema = (
graphQLSchema: GraphQLSchema,
options: Partial<OpenApiSchemaOptions> = {}
) => {
const { serverUrl, title, openapi, version, summary, description, exampleValues, routeMap } = { ...defaultOptions, ...options };
const examples = exampleMaker(graphQLSchema, exampleValues);
let paths: Paths = {};
let components: { schemas: Record<string, any> } = { schemas: {} };
for (const [exampleName, exampleData] of Object.entries(examples)) {
const example = exampleData as Example;
// Define a schema for each response in the components section
components.schemas[`${exampleName}Response`] = {
type: 'object',
properties: {
data: {
type: 'object',
properties: Object.keys(example.response.data).reduce((acc, key) => {
const fieldType = getGraphQLFieldType(graphQLSchema, example.operation, key);
acc[key] = { type: determineGraphQLType(fieldType) };
return acc;
}, {} as Record<string, { type: string }>)
}
}
};
const operationTags = routeMap?.[exampleName]?.tags ? [routeMap[exampleName].tags.name] : [];
paths[`/${exampleName}`] = {
post: {
...(operationTags.length > 0 && { tags: operationTags }),
operationId: exampleName,
'x-openai-isConsequential': false,
summary: example.summary,
description: `Example for ${exampleName}`,
requestBody: {
description: `Request for ${exampleName}`,
required: true,
content: {
'application/json': {
schema: {
type: 'object',
properties: {
query: {
type: 'string',
example: example.value.query
},
...(example.value.variables && {
variables: {
type: 'object',
additionalProperties: true,
example: example.value.variables
}
}),
...(example.value.operationName && {
operationName: {
type: 'string',
example: example.value.operationName
}
})
},
required: ['query']
}
}
}
},
responses: createResponseSchema(`${exampleName}Response`)
}
};
}
return {
openapi,
info: {
title,
version,
},
servers: [{ url: serverUrl }],
paths: paths,
tags: routeMap ? Object.values(routeMap).map(route => route.tags) : undefined,
components: components
};
};