-
Notifications
You must be signed in to change notification settings - Fork 0
/
apolloServer.ts
38 lines (35 loc) · 1.37 KB
/
apolloServer.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
import { makeExecutableSchema } from "@graphql-tools/schema";
import { applyMiddleware } from "graphql-middleware";
import { ApolloServer, ExpressContext } from "apollo-server-express";
import { ApolloServerPluginLandingPageLocalDefault } from "apollo-server-core";
import DataLoader from "dataloader";
import authMiddleware from "./middlewares/auth";
import typeDefs from "./schemas/typeDefs";
import resolvers from "./schemas/resolvers";
import { fetchNewsByPublisher, fetchNewsByCategory } from "./loaders/news";
import { fetchPublisherByNews } from "./loaders/publisher";
import { fetchCategoryByNews } from "./loaders/category";
interface Context extends ExpressContext {
loaders: Record<string, typeof DataLoader>
}
const server = new ApolloServer({
schema: applyMiddleware(
makeExecutableSchema({ typeDefs, resolvers }),
authMiddleware
),
csrfPrevention: true,
plugins: [ApolloServerPluginLandingPageLocalDefault({ embed: true })],
introspection: true,
context: (context: Context) => {
Object.assign(context, {
loaders: {
newsLoaderByPublisher: new DataLoader(fetchNewsByPublisher),
newsLoaderByCategory: new DataLoader(fetchNewsByCategory),
publisherLoaderByNews: new DataLoader(fetchPublisherByNews),
categoryLoaderByNews: new DataLoader(fetchCategoryByNews),
},
});
return context;
},
});
export { server };