-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (64 loc) · 1.7 KB
/
index.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
const { ApolloServer, gql } = require("apollo-server");
import USERS from "./constants/users";
import PICTURES from "./constants/pictures";
import LIKES from "./constants/likes";
const typeDefs = gql`
type User {
id: ID
firstName: String
lastName: String
email: String
likes: [Like]
pictures: [Picture]
}
type Picture {
id: ID
name: String
url: String
ownerId: ID
owner: User
likes: [Like]
}
type Like {
id: ID
pictureId: ID
picture: Picture
likerId: ID
liker: User
}
type Query {
picture(id: String!): Picture
pictures: [Picture]
user(id: String!): User
users: [User]
like(id: String!): Like
likes: [Like]
}
`;
const resolvers = {
Picture: {
owner: picture => USERS.find(user => user.id === picture.ownerId),
likes: picture => LIKES.filter(like => like.pictureId === picture.id)
},
Like: {
picture: like => PICTURES.find(picture => picture.id === like.pictureId),
liker: like => USERS.find(user => user.id === like.likerId)
},
User: {
likes: user => LIKES.filter(like => like.likerId === user.id),
pictures: user => PICTURES.filter(picture => picture.ownerId === user.id)
},
Query: {
picture: (root, args, context, info) =>
PICTURES.find(file => file.id == args.id),
pictures: () => PICTURES,
like: (root, args, context, info) => LIKES.find(like => like.id == args.id),
likes: () => LIKES,
user: (root, args, context, info) => USERS.find(user => user.id == args.id),
users: () => USERS
}
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen({ port: 4001 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});