-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
64 lines (56 loc) · 1.83 KB
/
main.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
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
import {tokenResolver} from "./resolvers/token";
const { ApolloServer } = require('apollo-server');
const { combineResolvers } = require( 'graphql-resolvers');
const { typeDefs } = require( './types' );
const queryResolvers = require('./resolvers/query');
const mutationResolvers = require('./resolvers/mutation');
import _ from 'lodash'
// This is a (sample) collection of books we'll be able to query
// the GraphQL server for. A more complete example might fetch
// from an existing data source like a REST API or database.
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
const isAuthenticated = (root, args, context, info) => {
if (!context.user) {
return new Error('Not authenticated')
}
};
const protectedField = (root, args, context, info) => 'Protected field value';
// Resolvers define the technique for fetching the types in the
// schema. We'll retrieve books from the "books" array above.
const rootResolver = {
Query: {
books: () => books,
token: queryResolvers.token,
protectedField: combineResolvers(
isAuthenticated,
protectedField
)
},
Mutation: {
...mutationResolvers
},
};
const mainResolver = _.merge(rootResolver, {
Token: tokenResolver
})
// In the most basic sense, the ApolloServer can be started
// by passing type definitions (typeDefs) and the resolvers
// responsible for fetching the data for those types.
const server = new ApolloServer({ typeDefs, resolvers: mainResolver,
context: ({ req }) => ({
auth: req.headers.authorization
})});
// This `listen` method launches a web-server. Existing apps
// can utilize middleware options, which we'll discuss later.
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});