forked from clayallsopp/graphqlhub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.js
196 lines (182 loc) · 4.21 KB
/
github.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import {
getUser,
getReposForUser,
getCommitsForRepo,
getRepoForUser,
getIssuesForRepo,
getCommentsForIssue,
} from '../apis/github';
import {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLNonNull,
GraphQLEnumType,
GraphQLInt,
GraphQLList,
GraphQLUnionType,
} from 'graphql';
import { isObject, isString } from 'lodash';
let CommitAuthorType = new GraphQLObjectType({
name : 'GithubCommitAuthor',
description : 'Commit author that is not associated with a Github acount',
fields: {
email : { type : GraphQLString },
name : { type : GraphQLString },
}
});
let UserType = new GraphQLObjectType({
name : 'GithubUser',
fields() {
return {
login : { type : GraphQLString },
id : { type : GraphQLInt },
company : { type : GraphQLString },
avatar_url : { type : GraphQLString },
repos : {
type : new GraphQLList(RepoType),
resolve(user) {
return getReposForUser(user.login);
}
},
};
}
});
let UserOrCommitAuthorType = new GraphQLUnionType({
name: 'UserOrStringType',
resolveType: (author) => {
if (isObject(author) && author.login) {
return UserType;
}
return CommitAuthorType;
},
types: [ CommitAuthorType, UserType ]
});
let CommitType = new GraphQLObjectType({
name : 'GithubCommit',
fields() {
return {
sha : { type : GraphQLString },
author : {
type : UserOrCommitAuthorType,
},
message : {
type : GraphQLString,
resolve(commit) {
return commit.commit && commit.commit.message;
}
}
};
}
});
let IssueCommentType = new GraphQLObjectType({
name : 'GithubIssueCommentType',
fields : {
id : { type : GraphQLInt },
body : { type : GraphQLString },
user : {
type : UserType,
resolve(issueComment) {
return issueComment.user;
}
}
},
});
let grabUsernameAndReponameFromURL = (url) => {
let array = url.split('/repos/')[1].split('/issues')[0].split('/');
return {
username : array[0],
reponame : array[1],
};
}
let IssueType = new GraphQLObjectType({
name : 'GithubIssue',
fields : {
id : { type : GraphQLInt },
title : { type : GraphQLString },
body : { type : GraphQLString },
commentCount : {
type : GraphQLInt,
resolve(issue) {
return issue.comments;
}
},
comments : {
type : new GraphQLList(IssueCommentType),
resolve(issue) {
let { username, reponame } = grabUsernameAndReponameFromURL(issue.url);
return getCommentsForIssue(username, reponame, issue);
}
}
}
});
let RepoType = new GraphQLObjectType({
name : 'GithubRepo',
fields : {
id : { type : GraphQLInt },
name : { type : GraphQLString },
commits : {
type : new GraphQLList(CommitType),
resolve(repo) {
return getCommitsForRepo(repo.owner.login, repo.name);
}
},
issues : {
type : new GraphQLList(IssueType),
args : {
limit : { type : GraphQLInt }
},
resolve(repo, { limit }) {
return getIssuesForRepo(repo.owner.login, repo.name).then((issues) => {
if (limit) {
return issues.slice(0, limit);
}
return issues;
});
}
}
}
});
let githubType = new GraphQLObjectType({
name : 'GithubAPI',
description : 'The Github API',
fields : {
user : {
type: UserType,
args : {
username : {
description : 'Username of the user',
type: new GraphQLNonNull(GraphQLString),
}
},
resolve: function(root, { username }) {
return getUser(username);
}
},
repo : {
type : RepoType,
args : {
name : {
description : 'Name of the repo',
type: new GraphQLNonNull(GraphQLString),
},
ownerUsername : {
description : 'Username of the owner',
type: new GraphQLNonNull(GraphQLString),
}
},
resolve: function(root, { ownerUsername, name }) {
return getRepoForUser(ownerUsername,name);
}
}
}
});
export const Schema = {
query : {
type : githubType,
resolve() {
return {};
}
}
};