forked from chenkie/graphql-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (62 loc) · 1.61 KB
/
server.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
65
66
67
68
69
70
71
72
73
74
75
76
77
require('dotenv').config();
const express = require('express');
const graphqlHTTP = require('express-graphql');
const { makeExecutableSchema } = require('graphql-tools');
const { directiveResolvers, attachDirectives } = require('./directives');
const { attachUserToContext } = require('./middleware');
const { getArticlesForAuthor, addArticle } = require('./controllers');
const { checkAuthAndResolve, checkScopesAndResolve } = require('./resolvers');
const app = express();
let ARTICLES = require('./data/articles');
const port = 8080;
const typeDefs = `
directive @isAuthenticated on QUERY | FIELD
directive @hasScope(scope: [String]) on QUERY | FIELD
type Article {
id: ID!
authorId: ID!
authorName: String!
articleName: String!
link: String!
review: Review
}
type Review {
rating: Int
comment: String
}
input ArticleInput {
authorId: ID!
authorName: String!
articleName: String!
link: String!
}
type Query {
allArticles: [Article]
}
type Mutation {
addArticle(input: ArticleInput): Article
}
`;
const resolvers = {
Query: {
allArticles: (_, args, context) =>
checkAuthAndResolve(context, getArticlesForAuthor)
},
Mutation: {
addArticle: (_, { input }, context) => {
checkScopesAndResolve(context, ['write:articles'], addArticle, input);
}
}
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
attachDirectives(schema);
// app.use(attachUserToContext);
app.use(
'/graphql',
graphqlHTTP({
schema,
graphiql: true
})
);
app.listen(port);
console.log(`App listening on localhost:${port}`);