-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
106 lines (88 loc) · 2.75 KB
/
server.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
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import express from 'express';
import cors from 'cors';
import swaggerUi from 'swagger-ui-express';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { gql } from 'graphql-tag';
import { generateOpenAPISchema } from '.';
import { IResolvers } from "@graphql-tools/utils";
const exampleValues: Record<string, any> = {
email: '[email protected]',
};
const typeDefs = gql`
type Query {
findServices: String
}
type Mutation {
submitRequest(email: String!): String
}
`;
const resolvers: IResolvers = {
Query: {
findServices: (): string => 'Service found!',
},
Mutation: {
submitRequest: (_: any, { email }: { email: string }): string => `Request submitted: ${email}`,
}
};
const routeMap = {
'findServices': {
tags: {
name: 'findServices-endpoint',
description: 'Endpoint for finding services'
}
},
'submitRequest': {
tags: {
name: 'submitRequest-endpoint',
description: 'Endpoint for submitting requests'
}
}
};
// Custom Swagger UI configuration
const swaggerOptions = {
swaggerOptions: {
requestInterceptor: (req: any) => {
req.headers['Content-Type'] = 'application/json';
// Add any other necessary headers here
return req;
},
},
customCss: '.swagger-ui .topbar { display: none }'
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const server = new ApolloServer({ schema });
// Initialize Express
const corsOptions = {
origin: ['http://localhost:4000', 'https://chat.openai.com'],
methods: ['GET', 'POST']
};
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors(corsOptions));
const PORT = 4000;
// Start Apollo Server
async function startServer() {
await server.start();
// Apply Apollo Server middleware to Express
app.use('/graphql', expressMiddleware(server, {
context: async ({ req }) => ({ /* your context here */ }),
}));
// Manually define OpenAPI JSON
const openApiSchema = generateOpenAPISchema(schema, { serverUrl: "/graphql", exampleValues, routeMap });
// Serve Swagger UI for OpenAPI documentation
app.use('/docs', swaggerUi.serve, swaggerUi.setup(openApiSchema, swaggerOptions));
// Express route to serve OpenAPI JSON
app.get('/openapi.json', (req, res) => {
res.json(openApiSchema);
});
// Start the Express server
app.listen(PORT, () => {
console.log(`🚀 Server ready at http://localhost:${PORT}/graphql`);
console.log(`🚀 OpenAPI JSON available at http://localhost:${PORT}/openapi.json`);
console.log(`🚀 Swagger UI docs available at http://localhost:${PORT}/docs`);
});
}
startServer();