/
apollo.js
38 lines (33 loc) · 1.28 KB
/
apollo.js
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 { ApolloServer } from '@apollo/server';
import { WebApp } from 'meteor/webapp';
import { getUser } from 'meteor/apollo';
import { LinksCollection } from '/imports/api/links';
import typeDefs from '/imports/apollo/schema.graphql';
import express from 'express';
import { expressMiddleware } from '@apollo/server/express4';
import { json } from 'body-parser';
const resolvers = {
Query: {
getLink: async (obj, { id }) => LinksCollection.findOne(id),
getLinks: async () => LinksCollection.find().fetch()
}
};
const context = async ({ req }) => ({
user: await getUser(req.headers.authorization)
})
const server = new ApolloServer({
cache: 'bounded',
typeDefs,
resolvers,
});
export async function startApolloServer() {
await server.start();
WebApp.connectHandlers.use(
'/graphql', // Configure the path as you want.
express() // Create new Express router.
.disable('etag') // We don't server GET requests, so there's no need for that.
.disable('x-powered-by') // A small safety measure.
.use(json()) // From `body-parser`.
.use(expressMiddleware(server, { context })), // From `@apollo/server/express4`.
)
}