forked from MichalLytek/type-graphql
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
44 lines (40 loc) · 1.24 KB
/
index.ts
File metadata and controls
44 lines (40 loc) · 1.24 KB
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
import "reflect-metadata";
import path from "node:path";
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { buildSchema } from "type-graphql";
import { Container } from "typedi";
import { type Context } from "./context.type";
import { LoggerMiddleware } from "./logger.middleware";
import { RecipeResolver } from "./resolver";
async function bootstrap() {
// Build TypeGraphQL executable schema
const schema = await buildSchema({
// Array of resolvers
resolvers: [RecipeResolver],
// IOC container
container: Container,
// Global middleware
globalMiddlewares: [LoggerMiddleware],
// Create 'schema.graphql' file with schema definition in current directory
emitSchemaFile: path.resolve(__dirname, "schema.graphql"),
});
// Create GraphQL server
const server = new ApolloServer<Context>({
schema,
});
// Start server
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
// Provide context
context: async () => ({
// Example user
user: {
id: 123,
name: "Sample user",
},
}),
});
console.log(`GraphQL server ready at ${url}`);
}
bootstrap().catch(console.error);