-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
147 lines (133 loc) · 2.98 KB
/
index.js
File metadata and controls
147 lines (133 loc) · 2.98 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const { ApolloServer } = require("apollo-server");
const { GraphQLScalarType } = require("graphql");
const typeDefs = `
scalar DateTime
enum PhotoCategory {
SELFIE
PORTRAIT
ACTION
LANDSCAPE
GRAPHIC
}
type Photo {
id: ID!
url: String!
name: String!
description: String
category: PhotoCategory!
postedBy: User!
taggedUsers: [User!]!
created: DateTime!
}
input PostPhotoInput {
name: String!
category: PhotoCategory=PORTRAIT
description: String
}
type User {
githubLogin: ID!
name: String
avatar: String
postedPhotos: [Photo!]!
inPhotos: [Photo!]!
}
type Query {
totalPhotos: Int!
allPhotos(after: DateTime): [Photo!]!
}
type Mutation {
postPhoto(input: PostPhotoInput!): Photo!
}
`;
var _id = 0;
var photos = [
{
id: '1',
name: 'Dropping The Cake',
description: '설명1',
category: 'ACTION',
githubUser: 'gPlake',
created: "3-28-1977"
},
{
id: '2',
name: 'Dropping The Cake2',
description: '설명2',
category: 'SELFIE',
githubUser: 'mHattrup',
created: "1-2-1985"
},
{
id: '3',
name: 'Dropping The Cake3',
description: '설명1',
category: 'LANDSCAPE',
githubUser: 'mHattrup',
created: "2018-04-15T19:09:57.308Z"
},
];
var users = [
{
githubLogin: 'mHattrup',
name: 'Mike Hattrup'
},
{
githubLogin: 'gPlake',
name: 'Glen Plake'
}
]
var tags = [
{ photoID: '1', userID: 'gPlake' },
{ photoID: '2', userID: 'mHattrup' },
{ photoID: '2', userID: 'gPlake' },
]
const resolvers = {
Query: {
totalPhotos: () => photos.length,
allPhotos: () => photos,
},
Mutation: {
postPhoto(parent, args) {
var newPhoto = {
id: _id++,
...args.input,
created: new Date()
};
photos.push(newPhoto);
return newPhoto;
},
},
Photo: {
url: (parent) => `http://naver.com/img/${parent.id}.jpg`,
postedBy: parent => {
return users.find(u => u.githubLogin === parent.githubUser)
},
taggedUsers: parent => tags
.filter(tag => tag.photoID === parent.id)
.map(tag => tag.userID)
.map(userID => users.find(u => u.githubLogin === userID))
},
User: {
postedPhotos: parent => {
return photos.filter(p => p.githubUser === parent.githubLogin)
},
inPhotos: parent => tags
.filter(tag => tag.userID === parent.id)
.map(tag => tag.photoID)
.map(photoID => photos.fimd(p => p.id === photoID))
},
DateTime: new GraphQLScalarType({
name: 'DateTime',
description: 'A valid date time value.',
parseValue: value => new Date(value),
serialize: value => new Date(value).toISOString(),
parseLiteral: ast => ast.value
})
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
server
.listen()
.then(({ url }) => console.log(`GraphQL Service running on ${url}`));