This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
executable file
·122 lines (108 loc) · 2.57 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// # Posts and Authors example from graphql-tools docs
// This project was created with [Apollo Launchpad](https://launchpad.graphql.com)
// You can see the original pad at [https://launchpad.graphql.com/1jzxrj179](https://launchpad.graphql.com/1jzxrj179)
import { find, filter } from 'lodash';
import { makeExecutableSchema } from 'graphql-tools';
import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import bodyParser from 'body-parser';
import cors from 'cors';
const authors = [
{ id: 1, firstName: 'Tom', lastName: 'Coleman' },
{ id: 2, firstName: 'Sashko', lastName: 'Stubailo' },
{ id: 3, firstName: 'Mikhail', lastName: 'Novikov' }
];
const posts = [
{ id: 1, authorId: 1, title: 'Introduction to GraphQL', votes: 2 },
{ id: 2, authorId: 2, title: 'Welcome to Apollo', votes: 3 },
{ id: 3, authorId: 2, title: 'Advanced GraphQL', votes: 1 },
{ id: 4, authorId: 3, title: 'Launchpad is Cool', votes: 7 }
];
const typeDefs = `
type Author {
id: Int!
firstName: String
lastName: String
posts: [Post] # the list of Posts by this author
}
type Post {
id: Int!
title: String
author: Author
votes: Int
}
# the schema allows the following query:
type Query {
posts: [Post]
author(id: Int!): Author
}
# this schema allows the following mutation:
type Mutation {
upvotePost (
postId: Int!
): Post
}
`;
const resolvers = {
Query: {
posts: () => posts,
author: (_, { id }) => find(authors, { id })
},
Mutation: {
upvotePost: (_, { postId }) => {
const post = find(posts, { id: postId });
if (!post) {
throw new Error(`Couldn't find post with id ${postId}`);
}
post.votes += 1;
return post;
}
},
Author: {
posts: author => filter(posts, { authorId: author.id })
},
Post: {
author: post => find(authors, { id: post.authorId })
}
};
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
const PORT = 1337;
const server = express();
server.use(
'/graphql',
cors(),
bodyParser.json(),
graphqlExpress(() => ({
schema
}))
);
server.use(
'/graphiql',
graphiqlExpress({
endpointURL: '/graphql',
query: `query PostsForAuthor {
author(id: 1) {
firstName
posts {
title
votes
}
}
}
mutation UpvotePost {
upvotePost(postId: 1) {
id
votes
}
}`
})
);
server.listen(PORT, () => {
console.log(
`GraphQL Server is now running on http://localhost:${PORT}/graphql`
);
console.log(`View GraphiQL at http://localhost:${PORT}/graphiql`);
});