-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
51 lines (43 loc) · 1.51 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
import { readFileSync } from 'node:fs';
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import chalk from 'chalk';
import { BooksDataSource } from './src/datasources';
import resolvers from './src/resolvers';
// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
const typeDefs = readFileSync('./src/schema.graphql', { encoding: 'utf8' });
export interface MyContext {
dataSources: {
booksAPI: BooksDataSource;
};
}
// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer<MyContext>({
typeDefs,
resolvers,
});
console.log(
chalk.magentaBright('🚀🌌 Apollo GraphQL Server blasting off! 🌌🚀')
);
// Passing an ApolloServer instance to the `startStandaloneServer` function:
// 1. creates an Express app
// 2. installs your ApolloServer instance as middleware
// 3. prepares your app to handle incoming requests
const { url } = await startStandaloneServer(server, {
context: async () => {
return {
// We are using a static data set for this example, but normally
// this would be where you'd add your data source connections
// or your REST API classes.
dataSources: {
booksAPI: new BooksDataSource(),
},
};
},
});
console.log(
chalk.yellowBright(`💫 Apollo GraphQL now live @ `) + chalk.blueBright(url)
);